diff --git a/data/fonts/Lunchtype21_Regular.ttf b/data/fonts/Lunchtype21_Regular.ttf new file mode 100644 index 0000000..668f051 Binary files /dev/null and b/data/fonts/Lunchtype21_Regular.ttf differ diff --git a/data/scripts/game.lua b/data/scripts/game.lua index 204cb81..8d98c5e 100644 --- a/data/scripts/game.lua +++ b/data/scripts/game.lua @@ -1,8 +1,14 @@ +local Vector2 = require "types.vector2" +local player = require "player" + -- called every frame, with constant delta time function game_tick() -- ctx.initialization_needed is true first frame and every time dynamic reload is performed if ctx.initialization_needed then - -- ctx.udata persists on reload + player:init() ctx.udata = {} end + + draw_camera_2d{} + player:tick(ctx) end diff --git a/data/scripts/player.lua b/data/scripts/player.lua new file mode 100644 index 0000000..d692814 --- /dev/null +++ b/data/scripts/player.lua @@ -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 \ No newline at end of file diff --git a/data/scripts/util.lua b/data/scripts/util.lua new file mode 100644 index 0000000..7315df4 --- /dev/null +++ b/data/scripts/util.lua @@ -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 \ No newline at end of file diff --git a/data/sprites/bird1.png b/data/sprites/bird1.png new file mode 100644 index 0000000..adc458a Binary files /dev/null and b/data/sprites/bird1.png differ diff --git a/data/sprites/bird2.png b/data/sprites/bird2.png new file mode 100644 index 0000000..89c09aa Binary files /dev/null and b/data/sprites/bird2.png differ