BatsAndPray/main.lua

185 lines
3.9 KiB
Lua
Raw Normal View History

2020-09-18 23:10:01 -05:00
animx = require "lib/animx"
2020-09-19 01:53:57 -05:00
class = require "lib/middleclass"
2020-09-18 23:10:01 -05:00
----------------------------------------
-- LOAD
----------------------------------------
function love.load ()
left = 0; right = 1; up = 2; down = 3
upleft = 4; downleft = 5; upright = 6; downright = 7
2020-09-19 01:53:57 -05:00
player = Bat:new( )
--
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 )
2020-09-19 01:53:57 -05:00
-- player.x = player.x + 100 * dt
2020-09-18 23:10:01 -05:00
2020-09-19 01:53:57 -05:00
player:update( dt )
2020-09-18 23:10:01 -05:00
animx.update(dt)
end
----------------------------------------
-- DRAW
----------------------------------------
function love.draw ()
love.graphics.print('Hello World!', 400, 300)
2020-09-19 01:53:57 -05:00
player:draw()
2020-09-18 23:10:01 -05:00
end
----------------------------------------
-- INPUT
----------------------------------------
function love.keypressed ( key )
if ( key == "right" ) then
2020-09-19 01:53:57 -05:00
player.moving = true
player.direction = right
2020-09-18 23:10:01 -05:00
elseif ( key == "left" ) then
2020-09-19 01:53:57 -05:00
player.moving = true
player.direction = left
2020-09-18 23:10:01 -05:00
elseif ( key == "space" ) then
2020-09-19 01:53:57 -05:00
player.flying = true
player.actor:switch('flap')
player.actor:getAnimation():restart()
2020-09-18 23:10:01 -05:00
end
end
function love.keyreleased (key)
2020-09-19 01:53:57 -05:00
if ( key == "right" and player.direction == right ) then
2020-09-18 23:10:01 -05:00
if ( love.keyboard.isDown("left") ) then
2020-09-19 01:53:57 -05:00
player.direction = left
2020-09-18 23:10:01 -05:00
else
2020-09-19 01:53:57 -05:00
player.moving = false
2020-09-18 23:10:01 -05:00
end
2020-09-19 01:53:57 -05:00
elseif ( key == "left" and player.direction == left ) then
2020-09-18 23:10:01 -05:00
if ( love.keyboard.isDown("right") ) then
2020-09-19 01:53:57 -05:00
player.direction = right
2020-09-18 23:10:01 -05:00
else
2020-09-19 01:53:57 -05:00
player.moving = false
2020-09-18 23:10:01 -05:00
end
end
end
----------------------------------------
-- FLIERS
----------------------------------------
2020-09-19 01:53:57 -05:00
-- birds and bats both fly. fliers.
Flier = class('Flier')
function Flier:initialize ( x, y, actor )
self.x = x
self.y = y
self.y_vel = 0
self.x_vel = 0
self.moving = false
self.flying = false
self.actor = actor
end
2020-09-18 23:10:01 -05:00
-- generic flier update: physics + changing position
2020-09-19 01:53:57 -05:00
function Flier:update ( dt )
self:physics( dt )
2020-09-18 23:10:01 -05:00
end
2020-09-19 01:53:57 -05:00
function Flier:draw ( )
if ( self.direction == right ) then
self.actor:flipX(true)
elseif (self.direction == left) then
self.actor:flipX(false)
2020-09-19 01:15:36 -05:00
end
2020-09-19 01:53:57 -05:00
self.actor:draw( self.x, self.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
2020-09-19 01:53:57 -05:00
function Flier:physics ( dt )
self:physics_x( dt )
self:physics_y( dt )
2020-09-18 23:10:01 -05:00
end
2020-09-19 01:53:57 -05:00
function Flier:physics_x ( dt )
2020-09-18 23:10:01 -05:00
max_vel = 300
min_vel = -300
2020-09-19 01:15:36 -05:00
turn = 300
2020-09-18 23:10:01 -05:00
2020-09-19 01:53:57 -05:00
-- holding arrow-key
if ( self.moving ) then
if ( self.x_vel < max_vel and self.direction == right ) then
self.x_vel = self.x_vel + (max_vel / turn)
elseif ( self.x_vel > min_vel and self.direction == left ) then
self.x_vel = self.x_vel - (max_vel / turn)
2020-09-18 23:10:01 -05:00
end
else
2020-09-19 01:53:57 -05:00
if ( self.x_vel > 0 ) then
self.x_vel = self.x_vel - (max_vel / (turn * 3))
elseif ( self.x_vel < 0 ) then
self.x_vel = self.x_vel + (max_vel / (turn * 3))
2020-09-18 23:10:01 -05:00
end
end
2020-09-19 01:53:57 -05:00
self.x = self.x + self.x_vel * dt
2020-09-18 23:10:01 -05:00
end
2020-09-19 01:53:57 -05:00
function Flier:physics_y ( dt )
gravity = .85
2020-09-18 23:10:01 -05:00
floor = 500
-- wing-flap
2020-09-19 01:53:57 -05:00
if ( self.flying ) then
self.y_vel = -175
self.flying = false
2020-09-18 23:10:01 -05:00
end
-- gravity
2020-09-19 01:53:57 -05:00
if ( self.y < floor ) then
self.y_vel = self.y_vel + gravity
2020-09-18 23:10:01 -05:00
end
-- if on ground; stop gravity
2020-09-19 01:53:57 -05:00
if ( self.y > floor ) then
self.y = floor
self.y_vel = 0
2020-09-18 23:10:01 -05:00
end
2020-09-19 01:53:57 -05:00
self.y = self.y + self.y_vel * dt
end
Bat = class('Bat', Flier)
function Bat:initialize()
batSheet = love.graphics.newImage("art/sprites/bat.png")
batFlapAnim = animx.newAnimation{
img = batSheet,
tileWidth = 32,
frames = { 2, 3, 4, 5 }
}:onAnimOver( function()
player.actor:switch('idle')
end )
batIdleAnim = animx.newAnimation {
img = batSheet,
tileWidth = 32,
frames = { 1 }
}
batActor = animx.newActor {
['idle'] = batIdleAnim,
['flap'] = batFlapAnim
}:switch('idle')
Flier.initialize( self, 50, 100, batActor )
2020-09-18 23:10:01 -05:00
end