-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
89 lines (77 loc) · 2.85 KB
/
player.lua
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
collision = require('collision')
INTERACTION_DURATION = 1
function init_player()
-- Player spritesheet/grid
Player_SS = love.graphics.newImage('assets/player.png')
local g = anim8.newGrid(8, 12, Player_SS:getWidth(), Player_SS:getHeight())
-- Player object
Player = {
x = 100,
y = 100,
hitbox = function (x, y) return {
x = x+1,
y = y+1,
width = 5,
height = 10
} end,
liminal_x = 20,
liminal_y = 20,
speed = 60,
facing = 'front',
spritesheet = love.graphics.newImage('assets/player.png'), -- accessed by animation:draw
animations = {
front = anim8.newAnimation(g(1,1), 1),
back = anim8.newAnimation(g(1,3), 1),
right = anim8.newAnimation(g(1,2), 1),
left = anim8.newAnimation(g(1,2), 1):flipH(),
front_walk = anim8.newAnimation(g('2-3',1), 0.1),
back_walk = anim8.newAnimation(g('2-3',3), 0.1),
right_walk = anim8.newAnimation(g('2-3',2), 0.1),
left_walk = anim8.newAnimation(g('2-3',2), 0.1):flipH(),
front_interacting = anim8.newAnimation(g('1-2', 4), 0.1),
back_interacting = anim8.newAnimation(g('2-3', 5), 0.1),
right_interacting = anim8.newAnimation(g(3,4, 1,5), 0.1),
left_interacting = anim8.newAnimation(g(3,4, 1,5), 0.1):flipH(),
},
states = {
movement = function () return {
id = 'movement'
} end,
interacting = function () return {
id = 'interacting',
duration_left = INTERACTION_DURATION
} end
}
}
Player.state = Player.states.movement()
Player.current_animation = Player.animations[Player.facing]
Directions = {
front = {0,1},
back = {0,-1},
right = {1,0},
left = {-1,0}
}
function Player:walk(direction, dt)
self.current_animation = Player.animations[direction..'_walk']
self.liminal_x = self.liminal_x + (Directions[direction][1] * self.speed * dt)
self.liminal_y = self.liminal_y + (Directions[direction][2] * self.speed * dt)
local x, liminal_x = check_liminality(self.x, self.liminal_x)
local y, liminal_y = check_liminality(self.y, self.liminal_y)
local hitbox = self.hitbox(x, y)
if check_tilemap_collision(hitbox) then
self.liminal_x = self.x
self.liminal_y = self.y
else
self.x = x
self.y = y
self.liminal_x = liminal_x
self.liminal_y = liminal_y
end
self.facing = direction
end
function Player:standstill()
self.current_animation = Player.animations[Player.facing]
self.liminal_x = self.x
self.liminal_y = self.y
end
end