little birdy moves
This commit is contained in:
parent
f212474e9b
commit
e8c4458ed8
BIN
data/fonts/Lunchtype21_Regular.ttf
Normal file
BIN
data/fonts/Lunchtype21_Regular.ttf
Normal file
Binary file not shown.
@ -1,8 +1,14 @@
|
|||||||
|
local Vector2 = require "types.vector2"
|
||||||
|
local player = require "player"
|
||||||
|
|
||||||
-- called every frame, with constant delta time
|
-- called every frame, with constant delta time
|
||||||
function game_tick()
|
function game_tick()
|
||||||
-- ctx.initialization_needed is true first frame and every time dynamic reload is performed
|
-- ctx.initialization_needed is true first frame and every time dynamic reload is performed
|
||||||
if ctx.initialization_needed then
|
if ctx.initialization_needed then
|
||||||
-- ctx.udata persists on reload
|
player:init()
|
||||||
ctx.udata = {}
|
ctx.udata = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
draw_camera_2d{}
|
||||||
|
player:tick(ctx)
|
||||||
end
|
end
|
||||||
|
64
data/scripts/player.lua
Normal file
64
data/scripts/player.lua
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
local Vector2 = require "types.vector2"
|
||||||
|
local util = require "util"
|
||||||
|
|
||||||
|
local SPR = {
|
||||||
|
"sprites/bird1.png",
|
||||||
|
"sprites/bird2.png",
|
||||||
|
}
|
||||||
|
|
||||||
|
local TERMINAL_V_VEL = 25
|
||||||
|
|
||||||
|
local p = {
|
||||||
|
position = Vector2(0, 50),
|
||||||
|
velocity = Vector2(),
|
||||||
|
size = Vector2(64, 64),
|
||||||
|
spr_idx = 1,
|
||||||
|
flip = false,
|
||||||
|
}
|
||||||
|
|
||||||
|
function p:rect()
|
||||||
|
return {x = self.position.x, y = self.position.y, w = self.size.x, h = self.size.y}
|
||||||
|
end
|
||||||
|
|
||||||
|
function p:init()
|
||||||
|
self.position.y = 50
|
||||||
|
self.position.x = ctx.resolution.x / 2
|
||||||
|
end
|
||||||
|
|
||||||
|
function p:is_on_ground()
|
||||||
|
return self.position.y >= ctx.resolution.y - 64
|
||||||
|
end
|
||||||
|
|
||||||
|
function p:tick(ctx)
|
||||||
|
input_action{name = "left", control = "LEFT"}
|
||||||
|
input_action{name = "right", control = "RIGHT"}
|
||||||
|
|
||||||
|
input_action{name = "jump", control = "SPACE"}
|
||||||
|
|
||||||
|
self.velocity.x = util.lerp(self.velocity.x, 0, 0.14)
|
||||||
|
|
||||||
|
if not self:is_on_ground() then
|
||||||
|
self.velocity.y = math.min(self.velocity.y + 1, TERMINAL_V_VEL)
|
||||||
|
else
|
||||||
|
self.position.y = ctx.resolution.y - 64
|
||||||
|
end
|
||||||
|
|
||||||
|
local movement = util.b2n(input_action_just_pressed{name = "right"}) - util.b2n(input_action_just_pressed{name = "left"})
|
||||||
|
if movement ~= 0 then
|
||||||
|
self.flip = movement < 0
|
||||||
|
end
|
||||||
|
|
||||||
|
if movement ~= 0 then
|
||||||
|
self.velocity.y = -10
|
||||||
|
self.velocity.x = movement * 20
|
||||||
|
end
|
||||||
|
|
||||||
|
if self.velocity.x < -0.1 or self.velocity.x > 0.1 then
|
||||||
|
self.spr_idx = (math.floor(ctx.frame_number / 11) % 2) + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
self.position = self.position + self.velocity
|
||||||
|
draw_sprite{rect = self:rect(), texture = SPR[self.spr_idx], flip_x = self.flip}
|
||||||
|
end
|
||||||
|
|
||||||
|
return p
|
12
data/scripts/util.lua
Normal file
12
data/scripts/util.lua
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
local util = {}
|
||||||
|
|
||||||
|
function util.b2n(b)
|
||||||
|
return b and 1 or 0
|
||||||
|
end
|
||||||
|
|
||||||
|
function util.lerp(v1, v2, t)
|
||||||
|
-- return (1 - t) * v1 + t * v2
|
||||||
|
return v1 + t * (v2 - v1)
|
||||||
|
end
|
||||||
|
|
||||||
|
return util
|
BIN
data/sprites/bird1.png
Normal file
BIN
data/sprites/bird1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 270 B |
BIN
data/sprites/bird2.png
Normal file
BIN
data/sprites/bird2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 317 B |
Loading…
Reference in New Issue
Block a user