From 28b22cc3cc57f711743497ee8d139741e7ff669b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Fri, 14 Feb 2025 00:44:00 +0300 Subject: [PATCH] add back player --- data/images/measure001a.png | Bin 0 -> 2774 bytes data/scripts/classes/player.lua | 39 ++++++++++++++++++++++++++++++++ data/scripts/game.lua | 25 +++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 data/images/measure001a.png create mode 100644 data/scripts/classes/player.lua diff --git a/data/images/measure001a.png b/data/images/measure001a.png new file mode 100644 index 0000000000000000000000000000000000000000..dec5b59cc01a269635586fa27586d6b40e511fd0 GIT binary patch literal 2774 zcmeAS@N?(olHy`uVBq!ia0y~yU;#2&7+9ErRIjnw1`sdZ(btiIVPik{pF~z5UnsyQ z#P$C|!~bjC|1UQFKV9lS5cDwU@z^CYFmUC2x;TbZ+A`@48 Q-2s{7>FVdQ&MBb@0EF*zl>h($ literal 0 HcmV?d00001 diff --git a/data/scripts/classes/player.lua b/data/scripts/classes/player.lua new file mode 100644 index 0000000..8388b9c --- /dev/null +++ b/data/scripts/classes/player.lua @@ -0,0 +1,39 @@ +local Vector3 = require("types.vector3") +local util = require("util") + +local Player = { + position = Vector3(0, 1, 0), + velocity = Vector3(), + + speed = 0.07, + mouse_sensitivity = 0.01, + + yaw = 0, + yaw_speed = 0.05, +} + +function Player:tick(ctx) + input_action{name = "left", control = "A"} + input_action{name = "right", control = "D"} + input_action{name = "forward", control = "W"} + input_action{name = "back", control = "S"} + + local camera_forward = Vector3(draw_camera_from_principal_axes(self).direction) + camera_forward.y = 0 + camera_forward = camera_forward:normalized() + local camera_right = camera_forward:cross(Vector3.UP) + + local forward_input = util.b2n(input_action_pressed{name = "forward"}) - util.b2n(input_action_pressed{name = "back"}) + local strafe_input = util.b2n(input_action_pressed{name = "right"}) - util.b2n(input_action_pressed{name = "left"}) + + local direction = ((camera_forward * forward_input) + (camera_right * strafe_input)):normalized() + self.velocity = direction * self.speed + + if ctx.mouse_capture then + self.yaw = self.yaw + self.mouse_sensitivity * ctx.mouse_movement.x + end + + self.position = self.position + self.velocity +end + +return Player \ No newline at end of file diff --git a/data/scripts/game.lua b/data/scripts/game.lua index 204cb81..6590aa4 100644 --- a/data/scripts/game.lua +++ b/data/scripts/game.lua @@ -1,8 +1,31 @@ +local player = require "classes.player" +local util = require "util" +local Vector3 = require "types.vector3" + -- called every frame, with constant delta time function game_tick() -- ctx.initialization_needed is true first frame and every time dynamic reload is performed if ctx.initialization_needed then -- ctx.udata persists on reload - ctx.udata = {} + ctx.udata = { + capture = false + } end + + ctx.mouse_capture = ctx.udata.capture + input_action{name = "toggle_mouse", control = "ESCAPE"} + + if input_action_just_pressed{name = "toggle_mouse"} then + ctx.udata.capture = not ctx.udata.capture + end + + player:tick(ctx) + -- draw_camera{position = Vector3(0, 1, 0), direction = Vector3.FORWARD} + -- draw ground + local q = util.create_plane_quad(Vector3(0, 0, 0), Vector3.UP, 10) + local params = { + texture = "images/measure001a.png", + texture_region = { x = 0, y = 0, w = 512, h = 512 }, + } + draw_quad(util.merge(q, params)) end