cool !
This commit is contained in:
		| @@ -1,13 +1,17 @@ | |||||||
| -- called every frame, with constant delta time | local FONT = "fonts/Lunchtype21_Regular.ttf" | ||||||
| local capture = false |  | ||||||
| local Vector3 = require "vector3" |  | ||||||
|  |  | ||||||
| local rot = 0 | local Vector3 = require "vector3" | ||||||
| local rot_spd = 0.05 | local capture = false | ||||||
|  |  | ||||||
|  | local head_rotation = 0 | ||||||
|  | local rotate_speed = 0.05 | ||||||
|  |  | ||||||
|  | local mouse_sensitivity = 0.01 | ||||||
|  |  | ||||||
| local spd = 0.07 | local spd = 0.07 | ||||||
|  |  | ||||||
| local pos = Vector3() | local position = Vector3() | ||||||
|  | local velocity = Vector3() | ||||||
|  |  | ||||||
| local function Vector2(x, y) | local function Vector2(x, y) | ||||||
|     if y == nil then |     if y == nil then | ||||||
| @@ -39,33 +43,40 @@ function game_tick() | |||||||
|     if ctx.initialization_needed then |     if ctx.initialization_needed then | ||||||
|         -- ctx.udata persists on reload |         -- ctx.udata persists on reload | ||||||
|         ctx.udata = {} |         ctx.udata = {} | ||||||
|         print(Vector3(5, 0, 0)) |  | ||||||
|     end |     end | ||||||
|     ctx.mouse_capture = capture |     ctx.mouse_capture = capture | ||||||
|     input_action{name="quit", control="SPACE"} |     input_action{name="toggle_mouse", control="ESCAPE"} | ||||||
|  |  | ||||||
|     input_action{name = "left", control = "A"} |     input_action{name = "left", control = "A"} | ||||||
|     input_action{name = "right", control = "D"} |     input_action{name = "right", control = "D"} | ||||||
|     input_action{name = "forward", control = "W"} |     input_action{name = "forward", control = "W"} | ||||||
|     input_action{name = "back", control = "S"} |     input_action{name = "back", control = "S"} | ||||||
|  |  | ||||||
|     if input_action_just_pressed{name = "quit"} then |     if input_action_just_pressed{name = "toggle_mouse"} then | ||||||
|         if not capture then |         if not capture then | ||||||
|             capture = true |             capture = true | ||||||
|         else |         else | ||||||
|             capture = false |             capture = false | ||||||
|         end |         end | ||||||
|     end |     end | ||||||
|     local dir = Vector3(draw_camera_from_principal_axes{position = pos, yaw = rot}.direction) |      | ||||||
|  |     local dir = Vector3(draw_camera_from_principal_axes{position = position, yaw = head_rotation}.direction) | ||||||
|     dir.y = 0 |     dir.y = 0 | ||||||
|     dir = dir:normalized() |     dir = dir:normalized() | ||||||
|  |  | ||||||
|  |     if not capture then | ||||||
|         local rot_d = b2n(input_action_pressed{name = "right"}) - b2n(input_action_pressed{name = "left"}) |         local rot_d = b2n(input_action_pressed{name = "right"}) - b2n(input_action_pressed{name = "left"}) | ||||||
|     rot = rot + rot_d * rot_spd |         head_rotation = head_rotation + rot_d * rotate_speed | ||||||
|  |     else | ||||||
|  |         head_rotation = head_rotation + mouse_sensitivity * ctx.mouse_movement.x | ||||||
|  |     end | ||||||
|  |  | ||||||
|     local mov_d = b2n(input_action_pressed{name = "forward"}) - b2n(input_action_pressed{name = "back"}) |     local mov_d = b2n(input_action_pressed{name = "forward"}) - b2n(input_action_pressed{name = "back"}) | ||||||
|     -- pos = trig.v3_add(pos, trig.v3_mult(dir, spd * mov_d)) |     velocity = dir * spd * mov_d | ||||||
|     pos = pos + dir * spd * mov_d |  | ||||||
|  |     position = position + velocity | ||||||
|  |  | ||||||
|     draw_billboard{position = Vector3(5, 0, 0), size = Vector2(1, 1), texture = "images/duck.png"} |     draw_billboard{position = Vector3(5, 0, 0), size = Vector2(1, 1), texture = "images/duck.png"} | ||||||
|  |  | ||||||
|  |     draw_text{position = Vector2(0), string = "vel: " .. tostring(velocity), font = FONT} | ||||||
| end | end | ||||||
|   | |||||||
| @@ -129,6 +129,10 @@ function Vector3:length_squared() | |||||||
|   return x2 + y2 + z2 |   return x2 + y2 + z2 | ||||||
| end | end | ||||||
|  |  | ||||||
|  | function Vector3:length() | ||||||
|  |   return math.sqrt(self:length_squared()) | ||||||
|  | end | ||||||
|  |  | ||||||
| function Vector3:normalized() | function Vector3:normalized() | ||||||
|   local length = math.sqrt(self:length_squared()) |   local length = math.sqrt(self:length_squared()) | ||||||
|   return Vector3(self.x / length, self.y / length, self.z / length) |   return Vector3(self.x / length, self.y / length, self.z / length) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user