diff options
author | ubq323 <ubq323@ubq323.website> | 2022-04-05 03:17:59 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2022-04-05 03:18:21 +0100 |
commit | 14e94c7da39fe99dcf3e3f34ed8f6d29ecac4983 (patch) | |
tree | b44e42444ab4e7b754ff2ab3a095ec9eea934ad7 | |
parent | 9120ff684d62691c04564cc8a038c80a85b91bce (diff) |
rewrite player in fennel
-rw-r--r-- | src/main.lua | 5 | ||||
-rw-r--r-- | src/player.fnl | 29 | ||||
-rw-r--r-- | src/player.lua | 38 |
3 files changed, 33 insertions, 39 deletions
diff --git a/src/main.lua b/src/main.lua index 7a5482c..d62c956 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,8 +1,11 @@ +local fennel = require "fennel" +table.insert(package.loaders or package.searchers, fennel.searcher) + + local player = require "player" local mansion = require "mansion" -print(string.pack) local current_room function love.load() diff --git a/src/player.fnl b/src/player.fnl new file mode 100644 index 0000000..a429282 --- /dev/null +++ b/src/player.fnl @@ -0,0 +1,29 @@ +(local player { :x 250 :y 250 }) + +(local SPEED 170) +(local SPRINT-SPEED 280) + +(fn player.move [self dt world] + (let [speed (if (love.keyboard.isScancodeDown :lshift :rshift) + SPRINT-SPEED SPEED) + up (if (love.keyboard.isScancodeDown :w :up) 1 0) + down (if (love.keyboard.isScancodeDown :s :down) 1 0) + left (if (love.keyboard.isScancodeDown :a :left) 1 0) + right (if (love.keyboard.isScancodeDown :d :right) 1 0) + vy (- down up) + vx (- right left) + sf (math.sqrt (+ (* vx vx) (* vy vy)))] + (when (not= sf 0) + (let [vx (/ vx sf) + vy (/ vy sf) + tx (+ self.x (* vx dt speed)) + ty (+ self.y (* vy dt speed)) + (ax ay) (world:move :player tx ty)] + (tset self :x ax) + (tset self :y ay))))) + +(fn player.draw [self] + (love.graphics.setColor 0.91 0.62 0) + (love.graphics.rectangle :fill self.x self.y 10 10)) + +player diff --git a/src/player.lua b/src/player.lua deleted file mode 100644 index 16a54dd..0000000 --- a/src/player.lua +++ /dev/null @@ -1,38 +0,0 @@ -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 |