BatsAndPray/main.lua

268 lines
5.4 KiB
Lua
Raw Normal View History

2020-09-19 18:43:39 -05:00
animx = require "lib/animx"
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 18:43:39 -05:00
bg = love.graphics.newImage("art/bg/sky.png")
player = Bat:new()
birdo = Bird:new( 10, 100 )
birdtwo = Bird:new( 600, 10 )
birdthree = Bird:new( 500, 200)
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
2020-09-19 18:43:39 -05:00
2020-09-18 23:10:01 -05:00
----------------------------------------
-- UPDATE
----------------------------------------
function love.update ( dt )
2020-09-19 01:53:57 -05:00
player:update( dt )
2020-09-19 18:43:39 -05:00
birdo:update( dt )
birdtwo:update( dt )
birdthree:update( dt )
2020-09-18 23:10:01 -05:00
animx.update(dt)
end
2020-09-19 18:43:39 -05:00
2020-09-18 23:10:01 -05:00
----------------------------------------
-- DRAW
----------------------------------------
function love.draw ()
2020-09-19 18:43:39 -05:00
love.graphics.draw(bg, 0, 0)
love.graphics.draw(bg, 512, 0)
2020-09-19 01:53:57 -05:00
player:draw()
2020-09-19 18:43:39 -05:00
birdo:draw()
birdtwo:draw()
birdthree:draw()
2020-09-18 23:10:01 -05:00
end
2020-09-19 18:43:39 -05:00
function love.resize ( width, height )
end
2020-09-18 23:10:01 -05:00
----------------------------------------
-- 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 18:43:39 -05:00
player.flying = 2
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
----------------------------------------
2020-09-19 18:43:39 -05:00
-- FLIER entity superclass
2020-09-18 23:10:01 -05:00
----------------------------------------
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
2020-09-19 18:43:39 -05:00
self.flying = 0
2020-09-19 01:53:57 -05:00
self.actor = actor
end
2020-09-19 18:43:39 -05:00
-- generic flier update: physics
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 18:43:39 -05:00
-- drawing the flier (ofc)
2020-09-19 01:53:57 -05:00
function Flier:draw ( )
2020-09-19 18:43:39 -05:00
if ( self.flying > 0 ) then
self.actor:switch('flap')
self.actor:getAnimation():restart()
self.flying = self.flying - 1
end
2020-09-19 01:53:57 -05:00
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
2020-09-19 18:43:39 -05:00
--------------------
-- "physics"
--------------------
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 18:43:39 -05:00
-- physics on the x-axis
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 18:43:39 -05:00
turn = 150
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 18:43:39 -05:00
-- physics on the y-axis
2020-09-19 01:53:57 -05:00
function Flier:physics_y ( dt )
2020-09-19 18:43:39 -05:00
gravity = 2
2020-09-18 23:10:01 -05:00
floor = 500
-- wing-flap
2020-09-19 18:43:39 -05:00
if ( self.flying > 0 ) then
self.y_vel = -200
self.flying = self.flying - 1
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
2020-09-19 18:43:39 -05:00
----------------------------------------
-- BAT player characters
----------------------------------------
2020-09-19 01:53:57 -05:00
Bat = class('Bat', Flier)
2020-09-19 18:43:39 -05:00
function Bat:initialize ()
-- animations
2020-09-19 01:53:57 -05:00
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
2020-09-19 18:43:39 -05:00
----------------------------------------
-- BIRD enemy characters
----------------------------------------
Bird = class('Bird', Flier)
function Bird:initialize ( x, y )
-- animations
birdSheet = love.graphics.newImage("art/sprites/bat.png")
birdFlapAnim = animx.newAnimation{
img = birdSheet,
tileWidth = 32,
frames = { 2, 3, 4, 5 }
}:onAnimOver( function()
player.actor:switch('idle')
end )
birdIdleAnim = animx.newAnimation {
img = birdSheet,
tileWidth = 32,
frames = { 1 }
}
birdActor = animx.newActor {
['idle'] = birdIdleAnim,
['flap'] = birdFlapAnim
}:switch('idle')
Flier.initialize( self, x, y, birdActor )
self.direction=right
end
function Bird:update ( dt )
self:destiny()
self:physics( dt )
end
-- basic "ai" (determines where the bird should go)
function Bird:destiny ()
self:destiny_x()
self:destiny_y()
end
-- "ai" on x-axis
function Bird:destiny_x ()
if ( self.x > 500 ) then
self.direction = left
elseif (self.x < 200) then
self.direction = right
end
self.moving = true
end
-- "ai" on y-axis
function Bird:destiny_y ()
if ( self.y > player.y and math.random(0,50) == 25 ) then
self.flying = 2
end
end