1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
|