Browse Source

Add player collision

main
Stephanie Gredell 2 years ago
parent
commit
a15354f87f
  1. 18
      Game.lua
  2. 1
      Menu.lua
  3. 97
      Player.lua
  4. 2
      globals.lua
  5. 36
      main.lua

18
Game.lua

@ -30,7 +30,23 @@ function Game() @@ -30,7 +30,23 @@ function Game()
table.insert(chats, i, Chat(chat_x, chat_y, self.level))
end
end
end,
draw = function (self, faded)
if faded then
Text(
"PAUSED",
0,
love.graphics.getHeight() * 0.4,
"h1",
false,
false,
love.graphics.getWidth(),
"center",
1
):draw()
end
end,
}
end

1
Menu.lua

@ -14,7 +14,6 @@ function Menu(game, player) @@ -14,7 +14,6 @@ function Menu(game, player)
local buttons = {
Button(funcs.newGame, nil, nil, love.graphics.getWidth() / 3, 50, "New Game", "center", "h3", love.graphics.getWidth() / 3, love.graphics.getHeight() * 0.25),
Button(nil, nil, nil, love.graphics.getWidth() / 3, 50, "Settings", "center", "h3", love.graphics.getWidth() / 3, love.graphics.getHeight() * 0.4),
Button(funcs.quitGame, nil, nil, love.graphics.getWidth() / 3, 50, "Quit", "center", "h3", love.graphics.getWidth() / 3, love.graphics.getHeight() * 0.55),
}

97
Player.lua

@ -4,7 +4,7 @@ local love = require "love" @@ -4,7 +4,7 @@ local love = require "love"
function Player(debugging)
local lg = love.graphics
local MAX_BAN = 60
local EXPLODE_DUR = 3
return {
sprite = lg.newImage('assets/prime.png'),
@ -16,13 +16,27 @@ function Player(debugging) @@ -16,13 +16,27 @@ function Player(debugging)
rotation_speed = 1,
thrusting = false,
bans = {},
max_bans = 60,
explode_time = 0,
exploding = false,
thrust = {
x = 0,
y = 0,
speed = 1
},
draw = function(self)
lg.draw(player.sprite, player.x, player.y, player.angle, 0.5, 0.5, player.sprite:getWidth() / 2, player.sprite:getHeight() / 2)
if not self.exploding then
lg.draw(player.sprite, player.x, player.y, player.angle, 0.5, 0.5, player.sprite:getWidth() / 2, player.sprite:getHeight() / 2)
else
love.graphics.setColor(1, 0, 0)
love.graphics.circle("fill", self.x, self.y, player.sprite:getWidth() / 2 * 1.5)
love.graphics.setColor(1, 158/255, 0)
love.graphics.circle("fill", self.x, self.y, player.sprite:getWidth() / 2 * 1)
love.graphics.setColor(1, 234/255, 0)
love.graphics.circle("fill", self.x, self.y, player.sprite:getWidth() / 2 * 0.5)
end
-- draw ban hammers
for _, ban in pairs(self.bans) do
@ -30,7 +44,8 @@ function Player(debugging) @@ -30,7 +44,8 @@ function Player(debugging)
end
end,
shootBan = function (self)
if (#self.bans <= MAX_BAN) then
self.max_bans = self.max_bans - 1
if (#self.bans <= self.max_bans) then
table.insert(self.bans, Ban(
self.x,
self.y,
@ -43,44 +58,48 @@ function Player(debugging) @@ -43,44 +58,48 @@ function Player(debugging)
end,
movePlayer = function (self, dt)
local FPS = love.timer.getFPS()
local friction = 0.7
self.exploding = self.explode_time > 0
if love.keyboard.isDown('left') then
self.angle = self.angle - self.rotation_speed * dt
self.facing = self.angle
end
if love.keyboard.isDown('right') then
self.angle = self.angle + self.rotation_speed * dt
self.facing = self.angle
end
if self.thrusting then
self.thrust.x = self.thrust.x + self.thrust.speed * math.sin(self.angle) / FPS
self.thrust.y = self.thrust.y - self.thrust.speed * math.cos(self.angle) / FPS
else
-- applies friction to stop the player
if self.thrust.x ~= 0 or self.thrust.y ~= 0 then
self.thrust.x = self.thrust.x - friction * self.thrust.x / FPS
self.thrust.y = self.thrust.y - friction * self.thrust.y / FPS
if not self.exploding then
local FPS = love.timer.getFPS()
local friction = 0.7
if love.keyboard.isDown('left') then
self.angle = self.angle - self.rotation_speed * dt
self.facing = self.angle
end
end
if love.keyboard.isDown('right') then
self.angle = self.angle + self.rotation_speed * dt
self.facing = self.angle
end
self.x = self.x + self.thrust.x
self.y = self.y + self.thrust.y
if self.thrusting then
self.thrust.x = self.thrust.x + self.thrust.speed * math.sin(self.angle) / FPS
self.thrust.y = self.thrust.y - self.thrust.speed * math.cos(self.angle) / FPS
else
-- applies friction to stop the player
if self.thrust.x ~= 0 or self.thrust.y ~= 0 then
self.thrust.x = self.thrust.x - friction * self.thrust.x / FPS
self.thrust.y = self.thrust.y - friction * self.thrust.y / FPS
end
end
-- make sure the player can't go off screen on x axis
if self.x + (self.sprite:getWidth() / 2) < 0 then
self.x = love.graphics.getWidth() + (self.sprite:getWidth() / 2)
elseif self.x - (self.sprite:getWidth() / 2) > love.graphics.getWidth() then
self.x = -(self.sprite:getWidth() / 2)
end
self.x = self.x + self.thrust.x
self.y = self.y + self.thrust.y
-- make sure the player can't go off screen on y axis
if self.y + (self.sprite:getHeight() / 2) < 0 then
self.y = love.graphics.getHeight() + (player.sprite:getHeight() / 2)
elseif self.y - (self.sprite:getHeight() / 2) > love.graphics.getHeight() then
self.y = -(self.sprite:getHeight() / 2)
-- make sure the player can't go off screen on x axis
if self.x + (self.sprite:getWidth() / 2) < 0 then
self.x = love.graphics.getWidth() + (self.sprite:getWidth() / 2)
elseif self.x - (self.sprite:getWidth() / 2) > love.graphics.getWidth() then
self.x = -(self.sprite:getWidth() / 2)
end
-- make sure the player can't go off screen on y axis
if self.y + (self.sprite:getHeight() / 2) < 0 then
self.y = love.graphics.getHeight() + (player.sprite:getHeight() / 2)
elseif self.y - (self.sprite:getHeight() / 2) > love.graphics.getHeight() then
self.y = -(self.sprite:getHeight() / 2)
end
end
-- this will move the ban
@ -99,7 +118,11 @@ function Player(debugging) @@ -99,7 +118,11 @@ function Player(debugging)
self.destroyBan(self, index)
end
end -- endfor
end --end movePlayer
end, --end movePlayer
explode = function (self) -- player can now expload
self.explode_time = math.ceil(EXPLODE_DUR * love.timer.getFPS())
end
} -- end player
end

2
globals.lua

@ -1,3 +1,5 @@ @@ -1,3 +1,5 @@
destroy_ast = false
function calculateDistance(x1, y1, x2, y2)
return math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2))
end

36
main.lua

@ -22,10 +22,17 @@ function love.keypressed(key) @@ -22,10 +22,17 @@ function love.keypressed(key)
if key == "escape" then
game:changeGameState("paused")
end
if key == "m" then
game:changeGameState("menu")
end
elseif game.state.paused then
if key == "escape" then
game:changeGameState("running")
end
elseif game.state.menu then
if key == "m" then
game:changeGameState("running")
end
end -- endif for gamestate
if key == "up" then
@ -56,6 +63,15 @@ function love.update(dt) @@ -56,6 +63,15 @@ function love.update(dt)
player:movePlayer(dt)
for chat_index, chat in pairs(chats) do
if not player.exploding then
if calculateDistance(player.x, player.y, chat.x, chat.y) < chat.radius then
player:explode() -- delete ban
destroy_ast = true
end
else
player.explode_time = player.explode_time - 1
end
-- we new check to see for ban collision detection
for _, ban in pairs(player.bans) do
if calculateDistance(ban.x, ban.y, chat.x, chat.y) < chat.radius then
@ -64,6 +80,11 @@ function love.update(dt) @@ -64,6 +80,11 @@ function love.update(dt)
end
end
if destroy_ast then
destroy_ast = false
chat:destroy(chats, chat_index, game)
end
chat:move(player.x, player.y, dt)
end
@ -82,6 +103,21 @@ function love.draw() @@ -82,6 +103,21 @@ function love.draw()
end
elseif game.state.paused then
game:draw(game.state.paused)
elseif game.state.menu then
menu:draw()
end
local ammo = player.max_bans
if player.max_bans > 0 then
ammo = player.max_bans
else
ammo = 0
end
love.graphics.print("AMMO: " .. ammo, 10, 10)
if ammo == 0 then
love.graphics.print("OUT OF AMMO", love.graphics.getWidth() / 2, love.graphics.getHeight() / 4)
end
end

Loading…
Cancel
Save