Browse Source

overhauled input system, added camera control with WASD [PLAT-1]

master
trashbyte 1 year ago
parent
commit
e5e595d31f
4 changed files with 163 additions and 46 deletions
  1. 2
    0
      .gitignore
  2. 29
    40
      src/engine.rs
  3. 131
    6
      src/input.rs
  4. 1
    0
      src/main.rs

+ 2
- 0
.gitignore View File

@@ -1,2 +1,4 @@
1 1
 /target
2 2
 **/*.rs.bk
3
+.idea
4
+platformaker.iml

+ 29
- 40
src/engine.rs View File

@@ -7,7 +7,7 @@ use crate::assetmanager::AssetManager;
7 7
 use crate::tilemap::TilemapLayer;
8 8
 use crate::tileset::Tileset;
9 9
 use crate::input::InputState;
10
-use crate::types::Point2;
10
+use crate::types::{Vector2, Point2};
11 11
 
12 12
 
13 13
 pub struct Engine {
@@ -53,13 +53,36 @@ fn world_to_screen_coords(point: Point2, camera_pos: Point2) -> Point2 {
53 53
 }
54 54
 
55 55
 
56
+impl Engine {
57
+    pub fn init(&mut self) {
58
+        self.init_input();
59
+    }
60
+
61
+    fn init_input(&mut self) {
62
+        self.input.bind_action("Jump", KeyCode::Space, KeyMods::NONE);
63
+
64
+        self.input.bind_axis("MoveX", KeyCode::D, KeyMods::NONE,  1.0);
65
+        self.input.bind_axis("MoveX", KeyCode::A, KeyMods::NONE, -1.0);
66
+        self.input.bind_axis("MoveY", KeyCode::W, KeyMods::NONE,  1.0);
67
+        self.input.bind_axis("MoveY", KeyCode::S, KeyMods::NONE, -1.0);
68
+    }
69
+}
70
+
71
+
72
+const CAMERA_SPEED: f32 = 5.0;
56 73
 impl EventHandler for Engine {
57 74
     fn update(&mut self, ctx: &mut Context) -> GameResult {
58 75
         const DESIRED_FPS: u32 = 60;
59 76
 
60 77
         while timer::check_update_time(ctx, DESIRED_FPS) {
61 78
             let seconds = 1.0 / (DESIRED_FPS as f32);
62
-            self.camera_pos.x += seconds / 5.0;
79
+            // y axis is flipped since camera transform happens in pixel space with y+ = down
80
+            let camera_movement = Vector2::new(self.input.get_axis_value("MoveX").unwrap(),
81
+                                               self.input.get_axis_value("MoveY").unwrap() * -1.0)
82
+                                           * seconds * CAMERA_SPEED;
83
+            self.camera_pos += camera_movement;
84
+
85
+            println!("{}", self.input.get_action_value("Jump").unwrap());
63 86
         }
64 87
 
65 88
         Ok(())
@@ -81,46 +104,12 @@ impl EventHandler for Engine {
81 104
     }
82 105
 
83 106
 
84
-    fn key_down_event(
85
-        &mut self,
86
-        ctx: &mut Context,
87
-        keycode: KeyCode,
88
-        _keymod: KeyMods,
89
-        _repeat: bool,
90
-    ) {
91
-        match keycode {
92
-            KeyCode::S => {
93
-                self.input.yaxis = -1.0;
94
-            }
95
-            KeyCode::W => {
96
-                self.input.yaxis = 1.0;
97
-            }
98
-            KeyCode::A => {
99
-                self.input.xaxis = -1.0;
100
-            }
101
-            KeyCode::D => {
102
-                self.input.xaxis = 1.0;
103
-            }
104
-            KeyCode::P => {
105
-                let img = graphics::screenshot(ctx).expect("Could not take screenshot");
106
-                img.encode(ctx, graphics::ImageFormat::Png, "/screenshot.png")
107
-                    .expect("Could not save screenshot");
108
-            }
109
-            KeyCode::Escape => ggez::quit(ctx),
110
-            _ => (), // Do nothing
111
-        }
107
+    fn key_down_event(&mut self, ctx: &mut Context, keycode: KeyCode, keymods: KeyMods, repeat: bool) {
108
+        self.input.key_down_event(ctx, keycode, keymods, repeat);
112 109
     }
113 110
 
114 111
 
115
-    fn key_up_event(&mut self, _ctx: &mut Context, keycode: KeyCode, _keymod: KeyMods) {
116
-        match keycode {
117
-            KeyCode::W | KeyCode::S => {
118
-                self.input.yaxis = 0.0;
119
-            }
120
-            KeyCode::A | KeyCode::D => {
121
-                self.input.xaxis = 0.0;
122
-            }
123
-            _ => (), // Do nothing
124
-        }
112
+    fn key_up_event(&mut self, ctx: &mut Context, keycode: KeyCode, keymods: KeyMods) {
113
+        self.input.key_up_event(ctx, keycode, keymods)
125 114
     }
126 115
 }

+ 131
- 6
src/input.rs View File

@@ -1,16 +1,141 @@
1
+use std::collections::HashMap;
2
+
3
+use ggez::event::{KeyCode, KeyMods};
4
+use ggez::Context;
5
+
6
+
7
+#[derive(Debug, PartialEq)]
8
+struct ActionBinding {
9
+    key_code: KeyCode,
10
+    key_mods: KeyMods,
11
+    value: bool,
12
+}
13
+#[derive(Debug, PartialEq)]
14
+struct AxisBinding {
15
+    key_code: KeyCode,
16
+    key_mods: KeyMods,
17
+    value_mult: f32,
18
+    value: f32,
19
+}
20
+
21
+
1 22
 #[derive(Debug)]
2 23
 pub struct InputState {
3
-    pub xaxis: f32,
4
-    pub yaxis: f32,
5
-    pub fire: bool,
24
+    actions: HashMap<String, Vec<ActionBinding>>,
25
+    axes: HashMap<String, Vec<AxisBinding>>,
26
+}
27
+
28
+
29
+impl InputState {
30
+    pub fn key_down_event(&mut self, ctx: &mut Context, key_code: KeyCode, key_mods: KeyMods, _repeat: bool) {
31
+        if key_code == KeyCode::Escape {
32
+            ggez::quit(ctx)
33
+        }
34
+        for (_, binds) in self.axes.iter_mut() {
35
+            for bind in binds.iter_mut() {
36
+                if bind.key_code == key_code {
37
+                    if bind.key_mods == KeyMods::NONE || bind.key_mods == key_mods {
38
+                        bind.value = bind.value_mult;
39
+                    }
40
+                }
41
+            }
42
+        }
43
+        for (_, binds) in self.actions.iter_mut() {
44
+            for bind in binds.iter_mut() {
45
+                if bind.key_code == key_code {
46
+                    if bind.key_mods == KeyMods::NONE || bind.key_mods == key_mods {
47
+                        bind.value = true;
48
+                    }
49
+                }
50
+            }
51
+        }
52
+    }
53
+
54
+
55
+    pub fn key_up_event(&mut self, _ctx: &mut Context, key_code: KeyCode, key_mods: KeyMods) {
56
+        for (_, binds) in self.axes.iter_mut() {
57
+            for bind in binds.iter_mut() {
58
+                if bind.key_code == key_code {
59
+                    if bind.key_mods == KeyMods::NONE || bind.key_mods == key_mods {
60
+                        bind.value = 0.0;
61
+                    }
62
+                }
63
+            }
64
+        }
65
+        for (_, binds) in self.actions.iter_mut() {
66
+            for bind in binds.iter_mut() {
67
+                if bind.key_code == key_code {
68
+                    if bind.key_mods == KeyMods::NONE || bind.key_mods == key_mods {
69
+                        bind.value = false;
70
+                    }
71
+                }
72
+            }
73
+        }
74
+    }
75
+
76
+
77
+    pub fn bind_action(&mut self, name: &str, key_code: KeyCode, key_mods: KeyMods) {
78
+        if !self.actions.contains_key(name) {
79
+            self.actions.insert(String::from(name), Vec::new());
80
+        }
81
+
82
+        let action_binds = self.actions.get_mut(name).unwrap();
83
+
84
+        let new_binding = ActionBinding { key_code, key_mods, value: false };
85
+        if !action_binds.contains(&new_binding) {
86
+            action_binds.push(new_binding);
87
+        }
88
+    }
89
+
90
+
91
+    pub fn bind_axis(&mut self, name: &str, key_code: KeyCode, key_mods: KeyMods, value_mult: f32) {
92
+        if !self.axes.contains_key(name) {
93
+            self.axes.insert(String::from(name), Vec::new());
94
+        }
95
+
96
+        let axis_binds = self.axes.get_mut(name).unwrap();
97
+
98
+        let new_binding = AxisBinding { key_code, key_mods, value_mult, value: 0.0 };
99
+        if !axis_binds.contains(&new_binding) {
100
+            axis_binds.push(new_binding);
101
+        }
102
+    }
103
+
104
+
105
+    pub fn get_axis_value(&self, name: &str) -> Option<f32> {
106
+        match self.axes.get(name) {
107
+            Some(binds) => {
108
+                let mut value = 0.0;
109
+                for b in binds {
110
+                    value += b.value;
111
+                }
112
+                Some(value)
113
+            },
114
+            None => None
115
+        }
116
+    }
117
+
118
+
119
+    pub fn get_action_value(&self, name: &str) -> Option<bool> {
120
+        match self.actions.get(name) {
121
+            Some(binds) => {
122
+                let mut value = false;
123
+                for b in binds {
124
+                    value |= b.value;
125
+                }
126
+                Some(value)
127
+            },
128
+            None => None
129
+        }
130
+    }
6 131
 }
7 132
 
133
+
8 134
 impl Default for InputState {
9 135
     fn default() -> Self {
10 136
         InputState {
11
-            xaxis: 0.0,
12
-            yaxis: 0.0,
13
-            fire: false,
137
+            actions: HashMap::new(),
138
+            axes: HashMap::new(),
14 139
         }
15 140
     }
16 141
 }

+ 1
- 0
src/main.rs View File

@@ -37,5 +37,6 @@ pub fn main() -> GameResult {
37 37
     let (ctx, events_loop) = &mut cb.build()?;
38 38
 
39 39
     let engine = &mut Engine::new(ctx)?;
40
+    engine.init();
40 41
     event::run(ctx, events_loop, engine)
41 42
 }

Loading…
Cancel
Save