|
| 1 | +go.property("movement_speed", 400) |
| 2 | +go.property("zoom_speed", 2) |
| 3 | + |
| 4 | +function init(self) |
| 5 | + msg.post(".", "acquire_input_focus") |
| 6 | + self.move = vmath.vector3() |
| 7 | + self.zoom_delta = 0 |
| 8 | + self.move_up = hash("key_w") |
| 9 | + self.move_down = hash("key_s") |
| 10 | + self.move_left = hash("key_a") |
| 11 | + self.move_right = hash("key_d") |
| 12 | + self.zoom_out = hash("key_q") |
| 13 | + self.zoom_in = hash("key_e") |
| 14 | +end |
| 15 | + |
| 16 | + |
| 17 | +function update(self, dt) |
| 18 | + local p = go.get_position() |
| 19 | + p.x = p.x + self.move.x * self.movement_speed * dt |
| 20 | + p.y = p.y + self.move.y * self.movement_speed * dt |
| 21 | + go.set_position(p) |
| 22 | + |
| 23 | + if self.zoom_delta ~= 0 then |
| 24 | + local zoom = go.get("#camera", "orthographic_zoom") |
| 25 | + zoom = zoom + self.zoom_delta * self.zoom_speed * dt |
| 26 | + zoom = math.max(0.1, zoom) |
| 27 | + go.set("#camera", "orthographic_zoom", zoom) |
| 28 | + end |
| 29 | +end |
| 30 | + |
| 31 | + |
| 32 | +function on_input(self, action_id, action) |
| 33 | + local v = action.pressed and 1 or (action.released and -1) or 0 |
| 34 | + if v == 0 then return end |
| 35 | + if action_id == self.move_up then |
| 36 | + self.move.y = self.move.y + v |
| 37 | + elseif action_id == self.move_down then |
| 38 | + self.move.y = self.move.y - v |
| 39 | + elseif action_id == self.move_left then |
| 40 | + self.move.x = self.move.x - v |
| 41 | + elseif action_id == self.move_right then |
| 42 | + self.move.x = self.move.x + v |
| 43 | + elseif action_id == self.zoom_in then |
| 44 | + self.zoom_delta = self.zoom_delta + v |
| 45 | + elseif action_id == self.zoom_out then |
| 46 | + self.zoom_delta = self.zoom_delta - v |
| 47 | + end |
| 48 | +end |
0 commit comments