Browse Source

added tile cursor [PLAT-2]

master
trashbyte 1 year ago
parent
commit
1b41f21519
4 changed files with 40 additions and 4 deletions
  1. BIN
      resources/cursor.png
  2. 5
    0
      src/assetmanager.rs
  3. 26
    4
      src/engine.rs
  4. 9
    0
      src/input.rs

BIN
resources/cursor.png View File


+ 5
- 0
src/assetmanager.rs View File

@@ -18,6 +18,11 @@ impl AssetManager {
18 18
             tileset_image.set_filter(graphics::FilterMode::Nearest);
19 19
             images.insert(String::from("tileset"), tileset_image);
20 20
         }
21
+        {
22
+            let mut cursor = graphics::Image::new(ctx, "/cursor.png")?;
23
+            cursor.set_filter(graphics::FilterMode::Nearest);
24
+            images.insert(String::from("cursor"), cursor);
25
+        }
21 26
         
22 27
         let mut fonts = HashMap::new();
23 28
         fonts.insert(String::from("DejaVuSerif"), graphics::Font::new(ctx, "/DejaVuSerif.ttf")?);

+ 26
- 4
src/engine.rs View File

@@ -54,10 +54,16 @@ impl Engine {
54 54
     }
55 55
 }
56 56
 
57
+pub fn world_to_screen_coords(point: Point2, camera_pos: Point2) -> Point2 {
58
+    let x = (point.x - camera_pos.x) * 32.0;
59
+    let y = WINDOW_HEIGHT - ((point.y - camera_pos.y) * 32.0);
60
+    Point2::new(x, y)
61
+}
62
+
57 63
 
58
-fn world_to_screen_coords(point: Point2, camera_pos: Point2) -> Point2 {
59
-    let x = point.x * 32.0 - camera_pos.x * 32.0;
60
-    let y = WINDOW_HEIGHT - point.y * 32.0 - camera_pos.y * 32.0;
64
+pub fn screen_to_world_coords(point: Point2, camera_pos: Point2) -> Point2 {
65
+    let x = point.x / 32.0 + camera_pos.x;
66
+    let y = (WINDOW_HEIGHT / 32.0) - (point.y / 32.0) + camera_pos.y;
61 67
     Point2::new(x, y)
62 68
 }
63 69
 
@@ -87,7 +93,7 @@ impl EventHandler for Engine {
87 93
             let seconds = 1.0 / (DESIRED_FPS as f32);
88 94
             // y axis is flipped since camera transform happens in pixel space with y+ = down
89 95
             let camera_movement = Vector2::new(self.input.get_axis_value("MoveX").unwrap(),
90
-                                               self.input.get_axis_value("MoveY").unwrap() * -1.0)
96
+                                               self.input.get_axis_value("MoveY").unwrap())
91 97
                                            * seconds * CAMERA_SPEED;
92 98
             self.camera_pos += camera_movement;
93 99
         }
@@ -101,6 +107,17 @@ impl EventHandler for Engine {
101 107
 
102 108
         self.tilemap.draw(world_to_screen_coords(Point2::new(10.0, 10.0), self.camera_pos), &self.tileset, ctx)?;
103 109
 
110
+        {
111
+            let cursor_pos = screen_to_world_coords(self.input.mouse_pos, self.camera_pos);
112
+            let cursor_pos = Point2::new(cursor_pos.x.floor(), cursor_pos.y.floor());
113
+
114
+            let drawparams = graphics::DrawParam::new()
115
+                .dest(world_to_screen_coords(cursor_pos, self.camera_pos))
116
+                .scale(Vector2::new(2.0, 2.0))
117
+                .offset(Point2::new(0.0, 1.0));
118
+            graphics::draw(ctx, self.assets.image("cursor").as_ref().unwrap(), drawparams)?;
119
+        }
120
+
104 121
         let test_text = graphics::Text::new((String::from("Test text"), self.assets.font("DejaVuSerif").unwrap(), 32.0));
105 122
         graphics::draw(ctx, &test_text, (Point2::new(800.0, 10.0), 0.0, graphics::WHITE))?;
106 123
 
@@ -111,6 +128,11 @@ impl EventHandler for Engine {
111 128
     }
112 129
 
113 130
 
131
+    fn mouse_motion_event(&mut self, ctx: &mut Context, x: f32, y: f32, xrel: f32, yrel: f32) {
132
+        self.input.mouse_motion_event(ctx, x, y, xrel, yrel);
133
+    }
134
+
135
+
114 136
     fn key_down_event(&mut self, ctx: &mut Context, keycode: KeyCode, keymods: KeyMods, repeat: bool) {
115 137
         self.input.key_down_event(ctx, keycode, keymods, repeat);
116 138
     }

+ 9
- 0
src/input.rs View File

@@ -3,6 +3,8 @@ use std::collections::HashMap;
3 3
 use ggez::event::{KeyCode, KeyMods};
4 4
 use ggez::Context;
5 5
 
6
+use crate::types::Point2;
7
+
6 8
 
7 9
 #[derive(Debug, PartialEq)]
8 10
 struct ActionBinding {
@@ -23,6 +25,7 @@ struct AxisBinding {
23 25
 pub struct InputState {
24 26
     actions: HashMap<String, Vec<ActionBinding>>,
25 27
     axes: HashMap<String, Vec<AxisBinding>>,
28
+    pub mouse_pos: Point2,
26 29
 }
27 30
 
28 31
 
@@ -74,6 +77,11 @@ impl InputState {
74 77
     }
75 78
 
76 79
 
80
+    pub fn mouse_motion_event(&mut self, _ctx: &mut Context, x: f32, y: f32, _xrel: f32, _yrel: f32 ) {
81
+        self.mouse_pos = Point2::new(x, y);
82
+    }
83
+
84
+
77 85
     pub fn bind_action(&mut self, name: &str, key_code: KeyCode, key_mods: KeyMods) {
78 86
         if !self.actions.contains_key(name) {
79 87
             self.actions.insert(String::from(name), Vec::new());
@@ -136,6 +144,7 @@ impl Default for InputState {
136 144
         InputState {
137 145
             actions: HashMap::new(),
138 146
             axes: HashMap::new(),
147
+            mouse_pos: Point2::new(0.0, 0.0)
139 148
         }
140 149
     }
141 150
 }

Loading…
Cancel
Save