Skip to content

Commit

Permalink
Recommited ghosts
Browse files Browse the repository at this point in the history
  • Loading branch information
slemonide committed Dec 3, 2017
1 parent eae0060 commit c044678
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 3 deletions.
14 changes: 14 additions & 0 deletions candles.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
candles = {}

candles.addCandle = function (x, y)
table.insert(candles, {
x = x,
y = y
})

if (not candles[x]) then
candles[x] = {}
end
candles[x][y] = true

end
68 changes: 68 additions & 0 deletions ghosts.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require('player')
require('utils')
require('nodes')
require('textures')

MAX_ACTIVE_RANGE = 10 -- in blocks

ghosts = {}

ghosts.addGhost = function(x, y, texture, followPlayer)
table.insert(ghosts, {
x = x,
y = y,
speed = 2, -- in blocks per dt
timer = 0,
texture = texture,
followPlayer = followPlayer
})
end

ghosts.addPinkGhost = function(x, y)
ghosts.addGhost(x, y, textures.ghostPink, false)
end

ghosts.addGreyGhost = function(x, y)
ghosts.addGhost(x, y, textures.ghostGrey, true)
end

ghosts.update = function(dt)
for _, ghost in ipairs(ghosts) do

local dx = player.x - ghost.x
local dy = player.y - ghost.y

if (dx == 0 and dy == 0) then
player.dead = true
end

if (math.abs(dx) < MAX_ACTIVE_RANGE and math.abs(dy) < MAX_ACTIVE_RANGE) then
if (ghost.timer > 1) then
ghost.timer = 0

local next_try_x, next_try_y
if (ghost.followPlayer) then
next_try_x = ghost.x + sign(dx)
next_try_y = ghost.y + sign(dy)
else
next_try_x = ghost.x + sign(math.random() - 0.5)
next_try_y = ghost.y + sign(math.random() - 0.5)
end


local choice = math.random() > 0.5
if choice and nodes.isWalkable(ghost.x, next_try_y) then
ghost.x = ghost.x
ghost.y = next_try_y
end

if not choice and nodes.isWalkable(next_try_x, ghost.y) then
ghost.x = next_try_x
ghost.y = ghost.y
end
else
ghost.timer = ghost.timer + dt * ghost.speed
end
end
end
end
53 changes: 52 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ require('textures')
require('nodes')
require('maze')
require('player')
require('ghosts')
require('candles')

------------------------
-- Load love
Expand All @@ -23,7 +25,7 @@ end
------------------------

function love.update(dt)
-- TODO: finish
ghosts.update(dt)
end

------------------------
Expand All @@ -34,6 +36,9 @@ draw = {}
draw.deathMessage = {}
draw.deathMessage.x = 0.5 + math.random()
draw.deathMessage.y = 0.5 + math.random()
draw.restartMessage = {}
draw.restartMessage.x = 0.5 + math.random()
draw.restartMessage.y = 0.5 + math.random()

-- Put origin to the center of the window
local function updateOrigin()
Expand All @@ -52,6 +57,7 @@ function love.draw()
-- need this to render textures properly
love.graphics.setColor(255, 255, 255)

-- TODO: move nodes, candles, ghosts, etc. into one group so it's easier to iterate
-- Draw nodes
for _, node in ipairs(nodes) do
if (math.abs(node.x - player.x) * SIZE < love.graphics.getWidth()
Expand All @@ -60,6 +66,22 @@ function love.draw()
end
end

-- Draw candles
--[[for _, candle in ipairs(candles) do
if (math.abs(candle.x - player.x) * SIZE < love.graphics.getWidth()
and math.abs(candle.y - player.y) * SIZE < love.graphics.getHeight()) then
love.graphics.draw(textures.candle, candle.x * SIZE, candle.y * SIZE)
end
end--]]

-- Draw ghosts
for _, ghost in ipairs(ghosts) do
if (math.abs(ghost.x - player.x) * SIZE < love.graphics.getWidth()
and math.abs(ghost.y - player.y) * SIZE < love.graphics.getHeight()) then
love.graphics.draw(ghost.texture, ghost.x * SIZE, ghost.y * SIZE)
end
end

-- Draw player
love.graphics.draw(textures.player, player.x * SIZE, player.y * SIZE)
end
Expand All @@ -70,6 +92,17 @@ function love.draw()
love.graphics.getWidth() / 2 * draw.deathMessage.x,
love.graphics.getHeight() / 2 * draw.deathMessage.y,
0, 2, 2)
love.graphics.setColor(255, 50, 100, 255)
love.graphics.print(
"Press C to restart at a checkpoint.",
love.graphics.getWidth() / 2 * draw.restartMessage.x,
love.graphics.getHeight() / 2 * draw.restartMessage.y,
0, 2, 2)
love.graphics.print(
"Press R to restart at a random location.",
love.graphics.getWidth() / 2 * draw.restartMessage.x,
love.graphics.getHeight() / 2 * draw.restartMessage.y + 25,
0, 2, 2)
end
end

Expand All @@ -89,5 +122,23 @@ function love.keypressed(key)
if (player.dead) then
draw.deathMessage.x = 0.5 + math.random()
draw.deathMessage.y = 0.5 + math.random()
draw.restartMessage.x = 0.5 + math.random()
draw.restartMessage.y = 0.5 + math.random()

if (key == "C" or key == "c") then
if (player.checkpoint) then
player.x = player.checkpoint.x * 2
player.y = player.checkpoint.y * 2
end

player.dead = false
elseif (key == "R" or key == "r") then
local pos = candles[math.random(#candles)]

player.x = pos.x * 2
player.y = pos.y * 2

player.dead = false
end
end
end
13 changes: 13 additions & 0 deletions maze.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require('utils')
require('nodes')
require('ghosts')
require('candles')

------------------------
-- A graph representation of the world
Expand Down Expand Up @@ -106,6 +108,10 @@ maze.writeToMap = function()
for _, node in ipairs(maze) do
nodes.addFloor(node.x * 2, node.y * 2)

if (math.random() > 0.96) then
candles.addCandle(node.x * 2, node.y * 2)
end

nodes.addWall(node.x * 2 + 1, node.y * 2 + 1)
nodes.addWall(node.x * 2 - 1, node.y * 2 - 1)
nodes.addWall(node.x * 2 - 1, node.y * 2 + 1)
Expand All @@ -114,6 +120,13 @@ maze.writeToMap = function()

for _, connection in ipairs(maze.connections) do
if (connection.open) then
if (math.random() > 0.97) then
if (math.random() > 0.5) then
ghosts.addPinkGhost(connection.x, connection.y)
else
ghosts.addGreyGhost(connection.x, connection.y)
end
end
nodes.addFloor(connection.x, connection.y)
else
if (math.random() > 0.7) then
Expand Down
10 changes: 9 additions & 1 deletion player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ player = {
x = 0,
y = 0,
dead = false,
blind = false
blind = false,
checkpoint = nil
}

local function getMovement(key)
Expand Down Expand Up @@ -43,6 +44,13 @@ player.handleKey = function(key)
if (nodes.isDeadly(movement.x, movement.y)) then
player.dead = true
end

if (candles[movement.x] or {})[movement.y] then
player.checkpoint = {
x = movement.x,
y = movement.y
}
end
end

if key == "space" then
Expand Down
5 changes: 4 additions & 1 deletion textures.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ textures = {}
textures.load = function()
textures.floor = love.graphics.newImage("assets/block-unit.png")
textures.wall = love.graphics.newImage("assets/wall-unit.png")
textures.player = love.graphics.newImage("assets/blindguyv2.png")
textures.player = love.graphics.newImage("assets/blindwithandWheelchair.png")
textures.spikes = love.graphics.newImage("assets/spike-unit.png")
textures.ghostGrey = love.graphics.newImage("assets/ghostgrey-unit.png")
textures.ghostPink = love.graphics.newImage("assets/ghostpink-unit.png")
textures.candle = love.graphics.newImage("assets/candle-unit.png")
end
10 changes: 10 additions & 0 deletions utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ function shallowcopy(orig)
copy = orig
end
return copy
end

function sign(number)
if (number > 0) then
return 1
elseif (number < 0) then
return -1
else
return 0
end
end

0 comments on commit c044678

Please sign in to comment.