-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
152 lines (128 loc) · 2.76 KB
/
Copy pathmain.lua
File metadata and controls
152 lines (128 loc) · 2.76 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
love.graphics.setDefaultFilter("nearest", "nearest")
local res = require "res"
local spronk = require "spronk"
-- https://clembod.itch.io/warrior-free-animation-set
local spritesheet = love.graphics.newImage("player.png")
local spr
local player = {
x = 0,
y = 170-42,
vx = 0,
vy = 0,
jump_force = -240,
gravity = -600,
ground = 168,
speed = 80,
}
function love.load()
love.graphics.setBackgroundColor(100/255, 169/255, 229/255, 1)
spr = spronk.new(69, 44, spritesheet)
-- optional bounding box support
spr:set_bbox({
x = 22, -- x offset
y = 12, -- y offset
w = 10, -- bbox width
h = 31 -- bbox height
})
spr:add_state("idle", {
row = 1,
frames = '1-6',
duration = 0.8
})
spr:add_state("run", {
frames = '2:1-6, 3:1-2',
duration = 0.6
})
spr:add_state("jump", {
frames = '7:5-6, 8:1-2',
duration = 0.6
})
spr:add_state("fall", {
frames = '8:3-6, 9:1',
duration = 0.6
})
spr:add_state("land", {
frames = '11:4-6, 12:1-3',
duration = 0.2,
loop = false,
on_complete = function()
player.landing = false
spr:set_state("idle")
end
})
spr:add_state("attack1", {
frames = '3:3-6, 4:1-5',
duration = 0.4,
loop = false,
on_complete = function()
player.attacking = false
spr:set_state("idle")
end
})
end
--[[ WARNING WARNING WARNING WARNING ]]
-- DO NOT USE THIS AS AN ACTUAL EXAMPLE FOR A PLAYER CONTROLLER. IT IS TERRIBLE AND BUGGY.
-- YOU HAVE BEEN WARNED.
function love.update(dt)
spr:update(dt)
-- run
if love.keyboard.isDown("right") then
if player.x < 300 then
spr:flip_h(false)
player.x = player.x + (player.speed * dt)
player.running = true
end
elseif love.keyboard.isDown("left") then
if player.x > -20 then
spr:flip_h(true)
player.x = player.x - (player.speed * dt)
player.running = true
end
else
player.running = false
end
-- jump
if love.keyboard.isDown("z") then
if player.vy == 0 then
spr:set_state("jump")
player.vy = player.jump_force
end
end
-- gravity
if player.vy ~= 0 then
player.y = player.y + player.vy * dt
player.vy = player.vy - player.gravity * dt
end
if player.y > player.ground-44 then
player.vy = 0
player.y = player.ground-44
if spr.current == "fall" then
player.landing = true
spr:set_state("land")
end
end
if love.keyboard.isDown("x") and not player.attacking then
player.attacking = true
spr:set_state("attack1")
elseif not player.attacking then
-- animations
if player.vy < 0 then
spr:set_state("jump")
elseif player.vy > 0 then
spr:set_state("fall")
else
if not player.landing then
if player.running then
spr:set_state("run")
else
spr:set_state("idle")
end
end
end
end
end
function love.draw()
res.set(320, 180)
spr:draw(player.x, player.y)
res.unset()
end