2020-09-18 23:10:01 -05:00
|
|
|
animx = require "lib/animx"
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
-- LOAD
|
|
|
|
----------------------------------------
|
|
|
|
function love.load ()
|
|
|
|
left = 0; right = 1; up = 2; down = 3
|
|
|
|
upleft = 4; downleft = 5; upright = 6; downright = 7
|
|
|
|
|
2020-09-19 01:15:36 -05:00
|
|
|
batSheet = love.graphics.newImage("art/sprites/bat.png")
|
|
|
|
|
|
|
|
batFlapAnim = animx.newAnimation{
|
|
|
|
img = batSheet,
|
|
|
|
tileWidth = 32,
|
|
|
|
frames = { 2, 3, 4, 5 }
|
|
|
|
}:onAnimOver( function()
|
|
|
|
bat.actor:switch('idle')
|
|
|
|
end )
|
|
|
|
|
|
|
|
batIdleAnim = animx.newAnimation {
|
|
|
|
img = batSheet,
|
|
|
|
tileWidth = 32,
|
|
|
|
frames = { 1 }
|
|
|
|
}
|
|
|
|
|
|
|
|
batActor = animx.newActor {
|
|
|
|
['idle'] = batIdleAnim,
|
|
|
|
['flap'] = batFlapAnim
|
|
|
|
}:switch('idle')
|
|
|
|
|
|
|
|
-- batActor:switch('idle')
|
2020-09-18 23:10:01 -05:00
|
|
|
|
|
|
|
bat = { x = 50, y = 200,
|
|
|
|
y_vel = 0, x_vel = 0,
|
|
|
|
moving = false,
|
|
|
|
flying = false,
|
|
|
|
direction = left,
|
2020-09-19 01:15:36 -05:00
|
|
|
actor = batActor
|
2020-09-18 23:10:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
-- for compliance with Statute 43.5 (2019); all birds must report births to local Officials
|
|
|
|
birdRegistry = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
-- UPDATE
|
|
|
|
----------------------------------------
|
|
|
|
function love.update ( dt )
|
|
|
|
-- bat.x = bat.x + 100 * dt
|
|
|
|
|
|
|
|
flier_update( bat, dt )
|
|
|
|
animx.update(dt)
|
|
|
|
end
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
-- DRAW
|
|
|
|
----------------------------------------
|
|
|
|
function love.draw ()
|
|
|
|
love.graphics.print('Hello World!', 400, 300)
|
|
|
|
flier_draw( bat )
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
-- INPUT
|
|
|
|
----------------------------------------
|
|
|
|
function love.keypressed ( key )
|
|
|
|
if ( key == "right" ) then
|
|
|
|
bat.moving = true
|
|
|
|
bat.direction = right
|
|
|
|
elseif ( key == "left" ) then
|
|
|
|
bat.moving = true
|
|
|
|
bat.direction = left
|
|
|
|
elseif ( key == "space" ) then
|
|
|
|
bat.flying = true
|
2020-09-19 01:15:36 -05:00
|
|
|
bat.actor:switch('flap')
|
|
|
|
bat.actor:getAnimation():restart()
|
2020-09-18 23:10:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function love.keyreleased (key)
|
|
|
|
if ( key == "right" and bat.direction == right ) then
|
|
|
|
if ( love.keyboard.isDown("left") ) then
|
|
|
|
bat.direction = left
|
|
|
|
else
|
|
|
|
bat.moving = false
|
|
|
|
end
|
|
|
|
elseif ( key == "left" and bat.direction == left ) then
|
|
|
|
if ( love.keyboard.isDown("right") ) then
|
|
|
|
bat.direction = right
|
|
|
|
else
|
|
|
|
bat.moving = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
-- FLIERS
|
|
|
|
----------------------------------------
|
|
|
|
-- generic flier update: physics + changing position
|
|
|
|
function flier_update ( flier, dt )
|
|
|
|
flier_physics( flier, dt )
|
|
|
|
end
|
|
|
|
|
|
|
|
function flier_draw ( flier )
|
2020-09-19 01:15:36 -05:00
|
|
|
if ( flier.direction == right ) then
|
|
|
|
flier.actor:flipX(true)
|
|
|
|
elseif (flier.direction == left) then
|
|
|
|
flier.actor:flipX(false)
|
|
|
|
end
|
|
|
|
flier.actor:draw( flier.x, flier.y )
|
2020-09-18 23:10:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
-- "PHYSICS"
|
|
|
|
----------------------------------------
|
|
|
|
-- "physics" being used verryyyyy lightly here
|
|
|
|
|
|
|
|
-- does basics physics work (determines velocity) for a flier
|
|
|
|
function flier_physics ( flier, dt )
|
|
|
|
flier_physics_x( flier, dt )
|
|
|
|
flier_physics_y( flier, dt )
|
|
|
|
end
|
|
|
|
|
|
|
|
function flier_physics_x ( flier, dt )
|
|
|
|
max_vel = 300
|
|
|
|
min_vel = -300
|
2020-09-19 01:15:36 -05:00
|
|
|
floor = 500
|
|
|
|
turn = 300
|
2020-09-18 23:10:01 -05:00
|
|
|
|
|
|
|
if ( flier.moving ) then
|
|
|
|
if ( flier.x_vel < max_vel and flier.direction == right ) then
|
2020-09-19 01:15:36 -05:00
|
|
|
flier.x_vel = flier.x_vel + (max_vel / turn)
|
2020-09-18 23:10:01 -05:00
|
|
|
elseif ( flier.x_vel > min_vel and flier.direction == left ) then
|
2020-09-19 01:15:36 -05:00
|
|
|
flier.x_vel = flier.x_vel - (max_vel / turn)
|
2020-09-18 23:10:01 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if ( flier.x_vel > 0 ) then
|
2020-09-19 01:15:36 -05:00
|
|
|
flier.x_vel = flier.x_vel - (max_vel / (turn * 3))
|
2020-09-18 23:10:01 -05:00
|
|
|
elseif ( flier.x_vel < 0 ) then
|
2020-09-19 01:15:36 -05:00
|
|
|
flier.x_vel = flier.x_vel + (max_vel / (turn * 3))
|
2020-09-18 23:10:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
flier.x = flier.x + flier.x_vel * dt
|
|
|
|
end
|
|
|
|
|
|
|
|
function flier_physics_y ( flier, dt )
|
|
|
|
gravity = .75
|
|
|
|
floor = 500
|
|
|
|
|
|
|
|
-- wing-flap
|
|
|
|
if ( flier.flying ) then
|
|
|
|
flier.y_vel = -200
|
|
|
|
flier.flying = false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- gravity
|
|
|
|
if ( flier.y < floor ) then
|
|
|
|
flier.y_vel = flier.y_vel + gravity
|
|
|
|
end
|
|
|
|
|
|
|
|
-- if on ground; stop gravity
|
|
|
|
if ( flier.y > floor ) then
|
|
|
|
flier.y = floor
|
|
|
|
flier.y_vel = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
flier.y = flier.y + flier.y_vel * dt
|
|
|
|
end
|