• R/O
  • SSH

silny-kombat: Commit

Mercurial repo for silny-kombat project


Commit MetaInfo

Revisionc71e72ed4f16a39e015e2e0f402225e58b38f7f0 (tree)
Time2022-02-10 05:26:52
AuthorSecT
CommiterSecT

Log Message

Refactoring - using enums as parameters instead of their numerical values

Change Summary

Incremental Difference

diff -r ed3e954454a2 -r c71e72ed4f16 src/character.rs
--- a/src/character.rs Wed Feb 09 20:32:59 2022 +0100
+++ b/src/character.rs Wed Feb 09 21:26:52 2022 +0100
@@ -1,5 +1,8 @@
11 use crate::character_state_machine;
22 use crate::sprite_rendering;
3+
4+use crate::key_processing::SignalForSM;
5+
36 use sfml::{
47 graphics::{
58 Texture
@@ -37,8 +40,8 @@
3740 // self.machine.set_num_state(num_state)
3841 // }
3942
40- pub fn trigger(&mut self, signal: i32) -> i32 {
41- self.machine.trigger(signal)
43+ pub fn trigger(&mut self, signal: SignalForSM) -> i32 {
44+ self.machine.trigger(signal as i32)
4245 }
4346
4447 pub fn update_sprite(&mut self){
diff -r ed3e954454a2 -r c71e72ed4f16 src/controller.rs
--- a/src/controller.rs Wed Feb 09 20:32:59 2022 +0100
+++ b/src/controller.rs Wed Feb 09 21:26:52 2022 +0100
@@ -20,6 +20,7 @@
2020 SfBox
2121 };
2222 use crate::key_processing::{KeyProcessor, SignalForSM, KeyEvents};
23+use sfml::window::Key;
2324
2425 pub struct Controller<'a> {
2526 characterA : Option<&'a Character<'a>>,
@@ -87,27 +88,54 @@
8788 loop {
8889 while let Some(event) = window.poll_event() {
8990 let mut key_result = key_processor.match_key(event);
90- if key_result == 0 {
91- //close program
92- return;
93- }
9491
95- if key_result == 99 {
96- //testing
97- println!("Testing: dump values to check");
98- save_test_data_to_file(groundLevel, &character_A.char_sprite, &character_B.char_sprite);
92+ match key_result {
93+ KeyEvents::Escape => return,
94+ KeyEvents::KeyTesting => {
95+ //testing
96+ println!("Testing: dump values to check");
97+ save_test_data_to_file(groundLevel, &character_A.char_sprite, &character_B.char_sprite);
98+ }
99+ KeyEvents::Char_Left => { process_key_result_for_character(&key_processor, &mut character_A, key_result); }
100+ KeyEvents::Char_Right => { process_key_result_for_character(&key_processor, &mut character_A, key_result); }
101+ KeyEvents::Char_Key_Release => { process_key_result_for_character(&key_processor, &mut character_A, key_result); }
102+ KeyEvents::Char_B_Left => {
103+ //second char
104+ process_key_result_for_character(&key_processor, &mut character_B, KeyEvents::Char_Left);
105+ }
106+ KeyEvents::Char_B_Right => {
107+ //second char
108+ process_key_result_for_character(&key_processor, &mut character_B, KeyEvents::Char_Right);
109+ }
110+ KeyEvents::Char_B_Key_Release => {
111+ //second char
112+ process_key_result_for_character(&key_processor, &mut character_B, KeyEvents::Char_Key_Release);
113+ }
114+ KeyEvents::None => {}
99115 }
100116
101117
102- if key_result < KeyProcessor::KEY_BASE {
103- //first char
104- process_key_result_for_character(&key_processor, &mut character_A, key_result);
105-
106- } else {
107- //second char
108- key_result = key_result - KeyProcessor::KEY_BASE;
109- process_key_result_for_character(&key_processor, &mut character_B, key_result);
110- }
118+ // if key_result == 0 {
119+ // //close program
120+ // return;
121+ // }
122+ //
123+ // if key_result == 99 {
124+ // //testing
125+ // println!("Testing: dump values to check");
126+ // save_test_data_to_file(groundLevel, &character_A.char_sprite, &character_B.char_sprite);
127+ // }
128+ //
129+ //
130+ // if key_result < KeyProcessor::KEY_BASE {
131+ // //first char
132+ // process_key_result_for_character(&key_processor, &mut character_A, key_result);
133+ //
134+ // } else {
135+ // //second char
136+ // key_result = key_result - KeyProcessor::KEY_BASE;
137+ // process_key_result_for_character(&key_processor, &mut character_B, key_result);
138+ // }
111139
112140
113141 // //first char/////////////
@@ -206,14 +234,14 @@
206234 window
207235 }
208236
209-pub fn process_key_result_for_character(key_processor:&key_processing::KeyProcessor, character: & mut Character, key_result:i32)
237+pub fn process_key_result_for_character(key_processor:&key_processing::KeyProcessor, character: & mut Character, key_result:KeyEvents)
210238 {
211- if key_result == KeyEvents::Char_Left as i32
212- || key_result == KeyEvents::Char_Right as i32
213- || key_result == KeyEvents::Char_Key_Release as i32
214- {
239+ // if key_result == KeyEvents::Char_Left as i32
240+ // || key_result == KeyEvents::Char_Right as i32
241+ // || key_result == KeyEvents::Char_Key_Release as i32
242+ //{
215243 character.trigger(key_processor.process_key(key_result));
216- }
244+ //}
217245 }
218246
219247 pub fn save_test_data_to_file(groundLevel:f32, spriteA: &CharacterSprite, spriteB: &CharacterSprite)
diff -r ed3e954454a2 -r c71e72ed4f16 src/key_processing.rs
--- a/src/key_processing.rs Wed Feb 09 20:32:59 2022 +0100
+++ b/src/key_processing.rs Wed Feb 09 21:26:52 2022 +0100
@@ -2,16 +2,22 @@
22 window::{ Event, Key},
33
44 };
5+use crate::key_processing::KeyEvents::{Char_Left, Char_Right, Char_Key_Release};
56
67 //use crate::sprite_rendering;
78
89 pub const KEY_BASE : i32 = 10;
910
1011 pub enum KeyEvents {
12+ None = -1,
1113 Escape,
1214 Char_Left,
1315 Char_Right,
14- Char_Key_Release
16+ Char_Key_Release,
17+ Char_B_Left = 11,
18+ Char_B_Right = 12,
19+ Char_B_Key_Release = 13,
20+ KeyTesting = 99
1521 }
1622
1723 pub enum SignalForSM {
@@ -36,26 +42,34 @@
3642 KeyProcessor {}
3743 }
3844
39- pub fn process_key(&self, key_result: i32) -> i32 {
40- if key_result == KeyEvents::Char_Left as i32
41- {
42- return SignalForSM::MoveToLeft as i32;
43-
45+ pub fn process_key(&self, key_result: KeyEvents) -> SignalForSM {
4446
45- } else if key_result == KeyEvents::Char_Right as i32
46- {
47- return SignalForSM::MoveToRight as i32;
47+ match key_result {
48+ KeyEvents::Char_Left => return SignalForSM::MoveToLeft,
49+ KeyEvents::Char_Right => return SignalForSM::MoveToRight,
50+ KeyEvents::Char_Key_Release => return SignalForSM::KeyRelease,
51+ _ => {return SignalForSM::None}
52+ }
4853
49- } else if key_result == KeyEvents::Char_Key_Release as i32
50- {
51- return SignalForSM::KeyRelease as i32;
52- }
53- return 0
54+ // if key_result == KeyEvents::Char_Left
55+ // {
56+ // return SignalForSM::MoveToLeft;
57+ //
58+ //
59+ // } else if key_result == KeyEvents::Char_Right as i32
60+ // {
61+ // return SignalForSM::MoveToRight;
62+ //
63+ // } else if key_result == KeyEvents::Char_Key_Release as i32
64+ // {
65+ // return SignalForSM::KeyRelease;
66+ // }
67+ // return SignalForSM::None;
5468 }
5569
5670
5771
58- pub fn match_key(&self, event: Event) -> i32 {
72+ pub fn match_key(&self, event: Event) -> KeyEvents {
5973
6074 //TODO: refactor enum->i32 conversions, just return a enum
6175
@@ -63,42 +77,42 @@
6377 Event::Closed
6478 | Event::KeyPressed {
6579 code: Key::ESCAPE, ..
66- } => return KeyEvents::Escape as i32,
80+ } => return KeyEvents::Escape,
6781
6882 //testing
6983 Event::KeyPressed {
7084 code: Key::F4, ..
71- } => return 99,
85+ } => return KeyEvents::KeyTesting,
7286
7387 Event::KeyPressed {
7488 code: Key::LEFT, ..
75- } => return KeyEvents::Char_Left as i32,
89+ } => return KeyEvents::Char_Left,
7690 Event::KeyPressed {
7791 code: Key::RIGHT, ..
78- } => return KeyEvents::Char_Right as i32,
92+ } => return KeyEvents::Char_Right,
7993 Event::KeyReleased {
8094 code:Key::RIGHT, ..
81- } => return KeyEvents::Char_Key_Release as i32,
95+ } => return KeyEvents::Char_Key_Release,
8296 Event::KeyReleased {
8397 code:Key::LEFT, ..
84- } => return KeyEvents::Char_Key_Release as i32,
98+ } => return KeyEvents::Char_Key_Release,
8599
86100
87101 Event::KeyPressed {
88102 code: Key::A, .. //move left for second character
89- } => return KEY_BASE + KeyEvents::Char_Left as i32,
103+ } => return KeyEvents::Char_B_Left,
90104 Event::KeyPressed {
91105 code: Key::D, .. //move right for second character
92- } => return KEY_BASE + KeyEvents::Char_Right as i32,
106+ } => return KeyEvents::Char_B_Right,
93107 Event::KeyReleased {
94108 code:Key::A, ..
95- } => return KEY_BASE + KeyEvents::Char_Key_Release as i32,
109+ } => return KeyEvents::Char_B_Key_Release,
96110 Event::KeyReleased {
97111 code:Key::D, ..
98- } => return KEY_BASE + KeyEvents::Char_Key_Release as i32,
112+ } => return KeyEvents::Char_B_Key_Release,
99113 _ => {}
100114 }
101- return -1;
115+ return KeyEvents::None;
102116 }
103117
104118
Show on old repository browser