Mercurial repo for silny-kombat project
Revision | c71e72ed4f16a39e015e2e0f402225e58b38f7f0 (tree) |
---|---|
Time | 2022-02-10 05:26:52 |
Author | SecT |
Commiter | SecT |
Refactoring - using enums as parameters instead of their numerical values
@@ -1,5 +1,8 @@ | ||
1 | 1 | use crate::character_state_machine; |
2 | 2 | use crate::sprite_rendering; |
3 | + | |
4 | +use crate::key_processing::SignalForSM; | |
5 | + | |
3 | 6 | use sfml::{ |
4 | 7 | graphics::{ |
5 | 8 | Texture |
@@ -37,8 +40,8 @@ | ||
37 | 40 | // self.machine.set_num_state(num_state) |
38 | 41 | // } |
39 | 42 | |
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) | |
42 | 45 | } |
43 | 46 | |
44 | 47 | pub fn update_sprite(&mut self){ |
@@ -20,6 +20,7 @@ | ||
20 | 20 | SfBox |
21 | 21 | }; |
22 | 22 | use crate::key_processing::{KeyProcessor, SignalForSM, KeyEvents}; |
23 | +use sfml::window::Key; | |
23 | 24 | |
24 | 25 | pub struct Controller<'a> { |
25 | 26 | characterA : Option<&'a Character<'a>>, |
@@ -87,27 +88,54 @@ | ||
87 | 88 | loop { |
88 | 89 | while let Some(event) = window.poll_event() { |
89 | 90 | let mut key_result = key_processor.match_key(event); |
90 | - if key_result == 0 { | |
91 | - //close program | |
92 | - return; | |
93 | - } | |
94 | 91 | |
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 => {} | |
99 | 115 | } |
100 | 116 | |
101 | 117 | |
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 | + // } | |
111 | 139 | |
112 | 140 | |
113 | 141 | // //first char///////////// |
@@ -206,14 +234,14 @@ | ||
206 | 234 | window |
207 | 235 | } |
208 | 236 | |
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) | |
210 | 238 | { |
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 | + //{ | |
215 | 243 | character.trigger(key_processor.process_key(key_result)); |
216 | - } | |
244 | + //} | |
217 | 245 | } |
218 | 246 | |
219 | 247 | pub fn save_test_data_to_file(groundLevel:f32, spriteA: &CharacterSprite, spriteB: &CharacterSprite) |
@@ -2,16 +2,22 @@ | ||
2 | 2 | window::{ Event, Key}, |
3 | 3 | |
4 | 4 | }; |
5 | +use crate::key_processing::KeyEvents::{Char_Left, Char_Right, Char_Key_Release}; | |
5 | 6 | |
6 | 7 | //use crate::sprite_rendering; |
7 | 8 | |
8 | 9 | pub const KEY_BASE : i32 = 10; |
9 | 10 | |
10 | 11 | pub enum KeyEvents { |
12 | + None = -1, | |
11 | 13 | Escape, |
12 | 14 | Char_Left, |
13 | 15 | 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 | |
15 | 21 | } |
16 | 22 | |
17 | 23 | pub enum SignalForSM { |
@@ -36,26 +42,34 @@ | ||
36 | 42 | KeyProcessor {} |
37 | 43 | } |
38 | 44 | |
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 { | |
44 | 46 | |
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 | + } | |
48 | 53 | |
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; | |
54 | 68 | } |
55 | 69 | |
56 | 70 | |
57 | 71 | |
58 | - pub fn match_key(&self, event: Event) -> i32 { | |
72 | + pub fn match_key(&self, event: Event) -> KeyEvents { | |
59 | 73 | |
60 | 74 | //TODO: refactor enum->i32 conversions, just return a enum |
61 | 75 |
@@ -63,42 +77,42 @@ | ||
63 | 77 | Event::Closed |
64 | 78 | | Event::KeyPressed { |
65 | 79 | code: Key::ESCAPE, .. |
66 | - } => return KeyEvents::Escape as i32, | |
80 | + } => return KeyEvents::Escape, | |
67 | 81 | |
68 | 82 | //testing |
69 | 83 | Event::KeyPressed { |
70 | 84 | code: Key::F4, .. |
71 | - } => return 99, | |
85 | + } => return KeyEvents::KeyTesting, | |
72 | 86 | |
73 | 87 | Event::KeyPressed { |
74 | 88 | code: Key::LEFT, .. |
75 | - } => return KeyEvents::Char_Left as i32, | |
89 | + } => return KeyEvents::Char_Left, | |
76 | 90 | Event::KeyPressed { |
77 | 91 | code: Key::RIGHT, .. |
78 | - } => return KeyEvents::Char_Right as i32, | |
92 | + } => return KeyEvents::Char_Right, | |
79 | 93 | Event::KeyReleased { |
80 | 94 | code:Key::RIGHT, .. |
81 | - } => return KeyEvents::Char_Key_Release as i32, | |
95 | + } => return KeyEvents::Char_Key_Release, | |
82 | 96 | Event::KeyReleased { |
83 | 97 | code:Key::LEFT, .. |
84 | - } => return KeyEvents::Char_Key_Release as i32, | |
98 | + } => return KeyEvents::Char_Key_Release, | |
85 | 99 | |
86 | 100 | |
87 | 101 | Event::KeyPressed { |
88 | 102 | code: Key::A, .. //move left for second character |
89 | - } => return KEY_BASE + KeyEvents::Char_Left as i32, | |
103 | + } => return KeyEvents::Char_B_Left, | |
90 | 104 | Event::KeyPressed { |
91 | 105 | code: Key::D, .. //move right for second character |
92 | - } => return KEY_BASE + KeyEvents::Char_Right as i32, | |
106 | + } => return KeyEvents::Char_B_Right, | |
93 | 107 | Event::KeyReleased { |
94 | 108 | code:Key::A, .. |
95 | - } => return KEY_BASE + KeyEvents::Char_Key_Release as i32, | |
109 | + } => return KeyEvents::Char_B_Key_Release, | |
96 | 110 | Event::KeyReleased { |
97 | 111 | code:Key::D, .. |
98 | - } => return KEY_BASE + KeyEvents::Char_Key_Release as i32, | |
112 | + } => return KeyEvents::Char_B_Key_Release, | |
99 | 113 | _ => {} |
100 | 114 | } |
101 | - return -1; | |
115 | + return KeyEvents::None; | |
102 | 116 | } |
103 | 117 | |
104 | 118 |