-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e1f139a
Showing
8 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
local Player = require 'Player' | ||
|
||
local Bullet = {} | ||
|
||
Bullet.bullets = {} | ||
Bullet.toRemove = {} | ||
|
||
function Bullet.new(source) | ||
local bullet = {} | ||
bullet.w = 10 | ||
bullet.speed = 400 | ||
bullet.x = Player.x + Player.w / 2 - bullet.w / 2 | ||
bullet.y = Player.y - bullet.w | ||
|
||
function bullet:draw() | ||
love.graphics.setColor(1,1,1) | ||
love.graphics.rectangle("fill", bullet.x, bullet.y, bullet.w, bullet.w) | ||
end | ||
|
||
Bullet.bullets[#Bullet.bullets + 1] = bullet | ||
|
||
return bullet | ||
end | ||
|
||
function Bullet.update(dt) | ||
for i = 1, #Bullet.bullets do | ||
Bullet.bullets[i].y = Bullet.bullets[i].y - Bullet.bullets[i].speed * dt | ||
if Bullet.bullets[i].y < -Bullet.bullets[i].w then | ||
table.insert(Bullet.toRemove, Bullet.bullets[i]) | ||
end | ||
end | ||
for i = 1, #Bullet.toRemove do | ||
for j = 1, #Bullet.bullets do | ||
if Bullet.toRemove[i] == Bullet.bullets[j] then | ||
table.remove(Bullet.bullets, j) | ||
table.remove(Bullet.toRemove, i) | ||
end | ||
end | ||
end | ||
end | ||
|
||
function Bullet.deleteAll() | ||
Bullet.bullets = {} | ||
end | ||
|
||
return Bullet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
local Button = {} | ||
|
||
Button.buttons = {} | ||
|
||
function Button.new(funcToExec, text, x, y, w, h, color, textColor, shape) | ||
button = { | ||
func = funcToExec or function() end, | ||
text = text or "", | ||
x = x or 0, | ||
y = y or 0, | ||
w = w or font:getWidth(text) + .1*love.graphics.getWidth(), | ||
h = h or font:getHeight(text) + .1*love.graphics.getHeight(), | ||
color = color or {0,0,0}, | ||
textColor = textColor or {0,0,0}, | ||
shape = shape or 'r' | ||
} | ||
|
||
function button:draw() | ||
local font = love.graphics.newFont(24) | ||
love.graphics.setColor(self.color[1], self.color[2], self.color[3]) | ||
if shape == 'r' then | ||
love.graphics.rectangle("fill", self.x, self.y, self.w, self.h) | ||
elseif shape == 'c' then | ||
love.graphics.ellipse("fill", self.x, self.y, self.w/2, self.h/2) | ||
end | ||
love.graphics.setColor(self.textColor[1], self.textColor[2], self.textColor[3]) | ||
love.graphics.print(self.text, self.x + self.w/2 - font:getWidth(self.text)/4, self.y + self.h/2 - font:getHeight(self.text)/4) | ||
end | ||
|
||
Button.buttons[#Button.buttons + 1] = button | ||
|
||
return button | ||
end | ||
|
||
function Button.ping(x, y, button) | ||
local clickCoords = {} | ||
clickCoords.x = x; clickCoords.y = y; clickCoords.w = 0; clickCoords.h = 0 | ||
for i = 1, #Button.buttons do | ||
if CheckCollision(Button.buttons[i], clickCoords) and button == 1 then | ||
Button.buttons[i].func() | ||
break -- only one button can be clicked at a time | ||
end | ||
end | ||
end | ||
|
||
function Button.draw() | ||
for i = 1, #Button.buttons do | ||
Button.buttons[i]:draw() | ||
end | ||
end | ||
|
||
function Button.deleteAll() | ||
Button.buttons = {} | ||
end | ||
|
||
return Button |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
local Enemy = {} | ||
Enemy.list = {} | ||
Enemy.toRemove = {} | ||
|
||
function Enemy.new(gameTime) | ||
enemy = {} | ||
enemy.w = math.random() * 100 + 5 | ||
enemy.x = math.abs(math.random() * (love.graphics.getWidth() - enemy.w)) | ||
enemy.y = -enemy.w | ||
enemy.speed = (1/(1/(1 + math.exp(-enemy.w/25)))) * (400 + gameTime) | ||
|
||
function enemy:draw() | ||
love.graphics.setColor(1,0,0) | ||
love.graphics.rectangle("fill", self.x, self.y, self.w, self.w) | ||
end | ||
|
||
Enemy.list[#Enemy.list + 1] = enemy | ||
|
||
return enemy | ||
end | ||
|
||
function Enemy.update(dt) | ||
for i = 1, #Enemy.list do | ||
Enemy.list[i].y = Enemy.list[i].y + Enemy.list[i].speed * dt | ||
if Enemy.list[i].y > love.graphics.getHeight() or enemy.hit then | ||
table.insert(Enemy.toRemove, Enemy.list[i]) | ||
end | ||
end | ||
for i = 1, #Enemy.toRemove do | ||
for j = 1, #Enemy.list do | ||
if Enemy.toRemove[i] == Enemy.list[j] then | ||
table.remove(Enemy.list, j) | ||
table.remove(Enemy.toRemove, i) | ||
end | ||
end | ||
end | ||
end | ||
|
||
function Enemy.deleteAll() | ||
Enemy.list = {} | ||
end | ||
|
||
return Enemy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
local Player = {} | ||
|
||
Player.w = 50 | ||
Player.x = love.graphics.getWidth() / 2 - Player.w / 2 | ||
Player.y = love.graphics.getHeight() * .8 | ||
Player.speed = 300 | ||
|
||
function Player.draw() | ||
love.graphics.setColor(0,1,0) | ||
love.graphics.rectangle("fill", Player.x, Player.y, Player.w, Player.w) | ||
end | ||
|
||
function Player.move(dt) | ||
if love.keyboard.isDown('w') and Player.y > 0 then Player.y = Player.y - Player.speed * dt end | ||
if love.keyboard.isDown('s') and Player.y < love.graphics.getHeight() - Player.w then Player.y = Player.y + Player.speed * dt end | ||
if love.keyboard.isDown('a') and Player.x > 0 then Player.x = Player.x - Player.speed * dt end | ||
if love.keyboard.isDown('d') and Player.x < love.graphics.getWidth() - Player.w then Player.x = Player.x + Player.speed * dt end | ||
end | ||
|
||
function Player.reset() | ||
Player.x = love.graphics.getWidth() / 2 - Player.w / 2 | ||
Player.y = love.graphics.getHeight() * .8 | ||
end | ||
|
||
return Player |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
local State = {} | ||
|
||
State.currentState = nil | ||
State.mystates = {} | ||
|
||
function State.import(states) | ||
for k, v in pairs(states) do | ||
State.mystates[k] = { | ||
onStart = v.onStart or function() end, | ||
onExit = v.onExit or function() end, | ||
update = v.update or function() end, | ||
draw = v.draw or function() end, | ||
keypressed = v.keypressed or function() end, | ||
mousepressed = v.mousepressed or function() end, | ||
globals = v.globals or {} | ||
} | ||
end | ||
end | ||
|
||
function State.load() | ||
assert(State.currentState, "current State is nil") | ||
State.currentState.onStart() | ||
State.declareGlobals() | ||
end | ||
|
||
function State.declareGlobals() | ||
for k, v in pairs(State.currentState.globals) do | ||
if k == nil then | ||
k = v | ||
end | ||
end | ||
end | ||
|
||
function State.change(toState) | ||
State.currentState.onExit() | ||
State.setCurrentState(toState) | ||
State.currentState.onStart() | ||
State.declareGlobals() | ||
end | ||
|
||
function State.setCurrentState(state) | ||
State.currentState = State.mystates[state] | ||
end | ||
|
||
function State.update(dt) | ||
State.currentState.update(dt) | ||
end | ||
|
||
function State.draw() | ||
State.currentState.draw() | ||
end | ||
|
||
function State.keypressed(key) | ||
State.currentState.keypressed(key) | ||
end | ||
|
||
function State.mousepressed(x, y, button) | ||
State.currentState.mousepressed(x, y, button) | ||
end | ||
|
||
return State |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
local functions = {} | ||
|
||
function functions.CheckCollision(a, b) | ||
local ax2,ay2,bx2,by2 = a.x + a.w, a.y + a.w, b.x + b.w, b.y + b.w | ||
return a.x < bx2 and ax2 > b.x and a.y < by2 and ay2 > b.y | ||
end | ||
|
||
return functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
local State = require 'State' | ||
local states = require 'states' | ||
|
||
function love.load() | ||
State.import(states) | ||
State.setCurrentState('menu') | ||
State.load() | ||
end | ||
|
||
function love.update(dt) | ||
State.update(dt) | ||
end | ||
|
||
function love.draw() | ||
State.draw() | ||
end | ||
|
||
function love.keypressed(key) | ||
if key == 'escape' then love.event.quit() end | ||
State.keypressed(key) | ||
end | ||
|
||
function love.mousepressed(x, y, button) | ||
State.mousepressed(x, y, button) | ||
end | ||
|
||
function CheckCollision(a, b) | ||
local ax2,ay2,bx2,by2 = a.x + a.w, a.y + a.w, b.x + b.w, b.y + b.w | ||
return a.x < bx2 and ax2 > b.x and a.y < by2 and ay2 > b.y | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
local Player = require 'Player' | ||
local Enemy = require 'Enemy' | ||
local Bullet = require 'Bullet' | ||
local Button = require 'Button' | ||
local State = require 'State' | ||
|
||
local states = {} | ||
--possible functions: onStart, onExit, update, draw, keypressed, mousepressed, declareGlobals | ||
states.menu = { | ||
onStart = function() | ||
local text1 = "Start Game!" | ||
local font = love.graphics.newFont(24) | ||
local buttonw1 = font:getWidth(text1) + .1 * love.graphics.getWidth() | ||
local buttonh1 = font:getHeight(text1) + .1 * love.graphics.getHeight() | ||
local text2 = "Options" | ||
local buttonw2 = font:getWidth(text2) + .1 * love.graphics.getWidth() | ||
local buttonh2 = font:getHeight(text2) + .1 * love.graphics.getHeight() | ||
Button.new( | ||
function() | ||
State.change("game") | ||
end, | ||
text1, | ||
love.graphics.getWidth() / 2 - buttonw1 / 2, | ||
love.graphics.getHeight() / 2 - buttonh1 / 2 - .1 * love.graphics.getHeight(), | ||
buttonw1, | ||
buttonh1, | ||
{1,.5,0}, | ||
{0,1,0} | ||
) | ||
|
||
Button.new( | ||
function() love.event.quit() end, | ||
text2, | ||
love.graphics.getWidth()/2 - buttonw2 / 2, | ||
love.graphics.getHeight() / 2 - buttonh2 / 2 + .1 * love.graphics.getHeight(), | ||
buttonw2, | ||
buttonh2, | ||
{1,.5,0}, | ||
{0,1,0} | ||
) | ||
end, | ||
onExit = function() | ||
love.graphics.clear() | ||
Button.deleteAll() | ||
end, | ||
draw = function() | ||
Button.draw() | ||
end, | ||
keypressed = function(key) | ||
--[[if key == 'space' then | ||
State.change(State.mystates.game) | ||
end]] | ||
end, | ||
mousepressed = function(x, y, button) | ||
Button.ping(x, y, button) | ||
end | ||
} | ||
|
||
states.game = { | ||
globals = {readyTime = 3, delay = .25}, | ||
update = function(dt) | ||
local globals = states.game.globals | ||
globals.readyTime = globals.readyTime - dt | ||
globals.delay = globals.delay - dt | ||
if globals.readyTime < 0 then | ||
Player.move(dt) | ||
Enemy.update(dt) | ||
Bullet.update(dt) | ||
for i = 1, #Enemy.list do | ||
if CheckCollision(Player, Enemy.list[i]) then | ||
State.change("game") | ||
break | ||
end | ||
for j = 1, #Bullet.bullets do | ||
if CheckCollision(Bullet.bullets[j], Enemy.list[i]) then | ||
table.insert(Bullet.toRemove, Bullet.bullets[j]) | ||
table.insert(Enemy.toRemove, Enemy.list[i]) | ||
end | ||
end | ||
end | ||
if love.math.random() < .1 then | ||
Enemy.new(-globals.readyTime + 3) | ||
end | ||
end | ||
end, | ||
draw = function() | ||
local globals = states.game.globals | ||
if globals.readyTime > 0 then | ||
love.graphics.setColor(1,1,1) | ||
love.graphics.print("Get ready! Game will start in " .. math.ceil(globals.readyTime), love.graphics.getWidth()/2, love.graphics.getHeight()/2) | ||
elseif globals.readyTime < 0 then | ||
Player.draw() | ||
for i = 1, #Enemy.list do | ||
Enemy.list[i]:draw() | ||
end | ||
for i = 1, #Bullet.bullets do | ||
Bullet.bullets[i]:draw() | ||
end | ||
end | ||
end, | ||
keypressed = function(key) | ||
local globals = states.game.globals | ||
if globals.readyTime < 0 then | ||
if key == 'space' and globals.delay < 0 then | ||
Bullet.new() | ||
globals.delay = .25 | ||
end | ||
end | ||
end, | ||
onExit = function() | ||
local globals = states.game.globals | ||
Enemy.deleteAll() | ||
Bullet.deleteAll() | ||
Player.reset() | ||
globals.readyTime = 3; globals.delay = .25 | ||
end | ||
} | ||
|
||
return states |