From 9120ff684d62691c04564cc8a038c80a85b91bce Mon Sep 17 00:00:00 2001 From: ubq323 Date: Wed, 30 Mar 2022 22:17:29 +0100 Subject: more thing --- src/player.lua | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/player.lua (limited to 'src/player.lua') diff --git a/src/player.lua b/src/player.lua new file mode 100644 index 0000000..16a54dd --- /dev/null +++ b/src/player.lua @@ -0,0 +1,38 @@ +local player = { + x = 250, + y = 250, +} + +local SPEED = 170 +local SPRINT_SPEED = 280 + +function player:move(dt,world) + local speed = + (love.keyboard.isScancodeDown("lshift","rshift") and SPRINT_SPEED or SPEED) + local vx = 0 + local vy = 0 + + if love.keyboard.isScancodeDown('w','up') then vy = vy - 1 end + if love.keyboard.isScancodeDown('s','down') then vy = vy + 1 end + if love.keyboard.isScancodeDown('a','left') then vx = vx - 1 end + if love.keyboard.isScancodeDown('d','right') then vx = vx + 1 end + + local sf = math.sqrt(vx*vx+vy*vy) + if sf == 0 then return end + vx = vx / sf + vy = vy / sf + + local tx = self.x + vx * dt * speed + local ty = self.y + vy * dt * speed + + local ax, ay = world:move("player",tx,ty) + self.x = ax + self.y = ay +end + +function player:draw() + love.graphics.setColor(0.91,0.62,0) + love.graphics.rectangle("fill", self.x,self.y, 10,10) +end + +return player -- cgit v1.2.3