|
@@ -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
|
}
|