• R/O
  • SSH

silny-kombat: Commit

Mercurial repo for silny-kombat project


Commit MetaInfo

Revision3f4295f010ee73118ab57a54f29ff4d596b6a70e (tree)
Time2021-11-11 05:13:40
AuthorPiotr Kozka <piotoor@gmai...>
CommiterPiotr Kozka

Log Message

State conversions moved away from trigger method

Change Summary

Incremental Difference

diff -r 7e4085eef9be -r 3f4295f010ee src/character.rs
--- a/src/character.rs Tue Oct 19 21:28:33 2021 +0200
+++ b/src/character.rs Wed Nov 10 21:13:40 2021 +0100
@@ -30,29 +30,23 @@
3030 &self.name
3131 }
3232
33- // pub fn get_state(&self) -> &character_state_machine::CharacterFSMState {
34- // &self.machine.get_state()
35- // }
36-
37- pub fn get_num_state(&self) -> i32 {
38- self.machine.get_num_state()
39- }
40-
41- // pub fn set_num_state(&mut self, num_state: i32) {
42- // self.machine.set_num_state(num_state)
43- // }
44-
45- pub fn trigger(&mut self, signal: i32) -> i32 {
46- self.machine.trigger(signal)
33+ pub fn get_state(&self) -> &character_state_machine::CharacterFSMState {
34+ &self.machine.get_state()
4735 }
4836
4937 pub fn trigger_(&mut self, signal: character_state_machine::CharacterFSMInput) -> Result<Option<character_state_machine::CharacterFSMOutput>, TransitionImpossibleError> {
5038 let machine_output = self.machine.trigger_(signal);
51-
5239 machine_output
5340 }
5441
55- pub fn update_sprite(&mut self){
56- self.char_sprite.show_sprite(self.get_num_state());
42+ pub fn update_sprite(&mut self, state: Option<character_state_machine::CharacterFSMOutput>){
43+ //self.char_sprite.show_sprite(self.get_num_state());
44+ match state {
45+ Some(character_state_machine::CharacterFSMOutput::Back) => self.char_sprite.show_sprite(1),
46+ Some(character_state_machine::CharacterFSMOutput::Front) => self.char_sprite.show_sprite(2),
47+ Some(character_state_machine::CharacterFSMOutput::Idle) => self.char_sprite.show_sprite(0),
48+ None => println!("No output provided")
49+ }
50+
5751 }
58-}
\ No newline at end of file
52+}
diff -r 7e4085eef9be -r 3f4295f010ee src/character_state_machine.rs
--- a/src/character_state_machine.rs Tue Oct 19 21:28:33 2021 +0200
+++ b/src/character_state_machine.rs Wed Nov 10 21:13:40 2021 +0100
@@ -18,24 +18,16 @@
1818 }
1919
2020 pub struct StateMachineWrapper {
21- machine: StateMachine<CharacterFSM>,
22- num_state: i32
21+ machine: StateMachine<CharacterFSM>
2322 }
2423
2524 impl StateMachineWrapper {
2625 pub fn new() -> StateMachineWrapper {
2726 StateMachineWrapper {
28- machine: StateMachine::new(),
29- num_state: 0
27+ machine: StateMachine::new()
3028 }
3129 }
3230
33-
34-
35- pub fn get_num_state(&self) -> i32 {
36- self.num_state
37- } // todo remove
38-
3931 pub fn get_state(&self) -> &CharacterFSMState {
4032 &self.machine.state()
4133 }
@@ -44,47 +36,6 @@
4436 self.machine.consume(&signal)
4537 }
4638
47- pub fn trigger(&mut self, signal: i32) -> i32 {
48- let output: Result<Option<CharacterFSMOutput>, TransitionImpossibleError>;
49- let mut ans = 1;
50- if signal == 1 {
51- output = self.machine.consume(&CharacterFSMInput::BackPressed);
52- match output {
53- Ok(v) => match v {
54- Some(CharacterFSMOutput::Back) => ans = 1,
55- Some(CharacterFSMOutput::Front) => ans = 2,
56- Some(CharacterFSMOutput::Idle) => ans = 0,
57- None => println!("None"),
58- }
59- Err(e) => println!("State machine error back pressed {}", e),
60- }
61- } else if signal == 2 {
62- output = self.machine.consume(&CharacterFSMInput::FrontPressed);
63- match output {
64- Ok(v) => match v {
65- Some(CharacterFSMOutput::Back) => ans = 1,
66- Some(CharacterFSMOutput::Front) => ans = 2,
67- Some(CharacterFSMOutput::Idle) => ans = 0,
68- None => println!("None"),
69- }
70- Err(e) => println!("State machine error front pressed {}", e),
71- }
72- } else {
73- output = self.machine.consume(&CharacterFSMInput::Released);
74- match output {
75- Ok(v) => match v {
76- Some(CharacterFSMOutput::Back) => ans = 1,
77- Some(CharacterFSMOutput::Front) => ans = 2,
78- Some(CharacterFSMOutput::Idle) => ans = 0,
79- None => println!("None"),
80- }
81- Err(e) => println!("State machine error released {}", e),
82- }
83- }
84- println!("ans = {}", ans);
85- self.num_state = ans;
86- ans
87- }
8839
8940
9041 }
diff -r 7e4085eef9be -r 3f4295f010ee src/controller.rs
--- a/src/controller.rs Tue Oct 19 21:28:33 2021 +0200
+++ b/src/controller.rs Wed Nov 10 21:13:40 2021 +0100
@@ -1,6 +1,7 @@
11
22 use crate::character::Character;
3-
3+use rust_fsm::*;
4+use crate::character_state_machine;
45 use crate::key_processing;
56
67 //use crate::key_processing::KeyProcessor;
@@ -49,24 +50,8 @@
4950
5051 //TODO: find a better name
5152 pub fn run(&self) {
52-
53- // poll event from keyboard E
54- // send E to KeyProcessor
55- // obtain KEY_EVENT from KeyProcessor based on E
56- //CharacterA.update(KEY_EVENT_1) - this returns id D_P1 from character state machine
57- //CharacterB.update(KEY_EVENT_2) - this returns id D_P2 from character state machine
58- //Based on D_P1 and D_P2, checkCollision()
59- //If True - queue interaction_event to character (for example "is_hit")
60- //If False - CharacterX.sprite.update()
61-
6253 let mut window = prepare_window();
63-
6454 let key_processor = key_processing::KeyProcessor::new();
65-
66-
67- //let mut character_sprite = sprite_rendering::CharacterSprite::new(&textures);
68- //let mut character_sprite_b = sprite_rendering::CharacterSprite::new(&textures);
69-
7055 let mut character_A: Character = Character ::new("Sub Zero", &self.textures);
7156 let mut character_B: Character = Character ::new("Sub Zero", &self.textures);
7257
@@ -75,11 +60,6 @@
7560 character_B.char_sprite.sprite.set_position(Vector2f::new(600.0, 0.0));
7661 character_B.char_sprite.sprite.set_scale(Vector2f::new(-1.0, 1.0));
7762
78- //loop
79-
80- //let &mut mut stateReference: &mut i32;
81-
82-
8363 loop {
8464 while let Some(event) = window.poll_event() {
8565 let mut key_result = key_processor.match_key(event);
@@ -97,36 +77,26 @@
9777
9878 if key_result < KeyProcessor::KEY_BASE {
9979 //first char
100- process_key_result_for_character(&key_processor, &mut character_A, key_result);
101-
80+ let output = process_key_result_for_character(&key_processor, &mut character_A, key_result);
81+ println!("-------------------------------");
82+ match output {
83+ Ok(v) => character_A.update_sprite(v),
84+ Err(e) => println!("Unsupported key_result {}", key_result)
85+ }
10286 } else {
10387 //second char
10488 key_result = key_result - KeyProcessor::KEY_BASE;
105- process_key_result_for_character(&key_processor, &mut character_B, key_result);
89+ let output = process_key_result_for_character(&key_processor, &mut character_B, key_result);
90+ println!("-------------------------------");
91+ match output {
92+ Ok(v) => character_B.update_sprite(v),
93+ Err(e) => println!("Unsupported key_result {}", key_result)
94+ }
10695 }
107-
108-
109- // //first char/////////////
110- // if key_result == 3 {
111- // state = 0;
112- // counter = 0;
113- // }
114- // if key_result == 1 || key_result == 2 || key_result == 3 {
115- // keyProcessor.process_key(key_result, &mut state);
116- // }
117- // //second char///////////
118- // else if key_result == KeyProcessor::KEY_BASE + 1 || key_result == KeyProcessor::KEY_BASE + 2 {
119- // keyProcessor.process_key(key_result - KeyProcessor::KEY_BASE, &mut state_b);
120- // } else if key_result == KeyProcessor::KEY_BASE + 3 {
121- // state_b = 0;
122- // counter = 0;
123- // }
96+ println!("-------------------------------");
12497 }
12598
12699
127- character_A.update_sprite();
128- character_B.update_sprite();
129-
130100 window.clear(Color::rgb(50, 200, 50));
131101 window.draw(&character_A.char_sprite.sprite);
132102 window.draw(&character_B.char_sprite.sprite);
@@ -134,8 +104,6 @@
134104
135105 //end main loop
136106 }
137-
138-
139107 }
140108
141109 }
@@ -158,10 +126,14 @@
158126 window
159127 }
160128
161-pub fn process_key_result_for_character(key_processor:&key_processing::KeyProcessor, character: & mut Character, key_result:i32)
129+pub fn process_key_result_for_character(key_processor:&key_processing::KeyProcessor, character: & mut Character, key_result:i32) -> Result<Option<character_state_machine::CharacterFSMOutput>, TransitionImpossibleError>
162130 {
163- if key_result == 1 || key_result == 2 || key_result == 3 {
164- character.trigger(key_processor.process_key(key_result));
131+ println!("key_result processed = {}", key_processor.process_key(key_result));
132+ match key_processor.process_key(key_result) {
133+ 1 => character.trigger_(character_state_machine::CharacterFSMInput::BackPressed),
134+ 2 => character.trigger_(character_state_machine::CharacterFSMInput::FrontPressed),
135+ 3 => character.trigger_(character_state_machine::CharacterFSMInput::Released),
136+ _ => Err(TransitionImpossibleError)
165137 }
166138 }
167139
Show on old repository browser