Skip to content

Commit

Permalink
Make ghosts pass terrain and die on purgatory
Browse files Browse the repository at this point in the history
Ghosts no longer collide with terrain, but die on collision with the
ghost purgatory tile in the graveyard.
  • Loading branch information
rbaltrusch committed Mar 10, 2024
1 parent 571f483 commit 1ac912b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"lighting_dist",
"crown_bar",
"rest_sound",
"won"
"won",
"ghost_collision_map",
"ghost_death_sound"
]
}
Binary file added assets/ghost_death.wav
Binary file not shown.
16 changes: 14 additions & 2 deletions main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ end

function love.load()
--BACKGROUND_COLOUR = Colour.construct(71, 45, 60)
GHOST = 321
GRAVEYARD_PURGATORY = 591
BACKGROUND_COLOUR = Colour.construct(0, 0, 0)
TILE_SIZE = 16
DEBUG_ENABLED = false
Expand Down Expand Up @@ -75,6 +77,9 @@ function love.load()
rest_sound:setVolume(0.5)
rest_sound:setPitch(0.5)

ghost_death_sound = love.audio.newSource("assets/ghost_death.wav", "static")
ghost_death_sound:setVolume(0.75)

local walk_sound = love.audio.newSource("assets/walk.wav", "static")
walk_sound:setVolume(0.2)

Expand Down Expand Up @@ -123,7 +128,8 @@ function love.load()
shader = love.graphics.newShader(require("src/shader"))
tileset = SpriteSheet.load_sprite_sheet("assets/kenney_1-bit-pack/Tilesheet/colored_packed.png", TILE_SIZE, TILE_SIZE)
tilemap = require "assets/map"
collision_map = TileMap.construct_collision_map(tilemap, "terrain")
collision_map = TileMap.construct_collision_map(tilemap, "terrain", function(tile_index) return tile_index == 0 end) -- passable if empty
ghost_collision_map = TileMap.construct_collision_map(tilemap, "terrain", function(tile_index) return true end) -- always passable
tiles = TileMap.construct_tiles(tilemap, tileset)
entities = Entity.construct_from_tilemap(
tiles["enemies"].tiles,
Expand Down Expand Up @@ -206,7 +212,13 @@ local function update(dt)
end

for _, entity in ipairs(entities) do
entity:update(dt, player, collision_map)
local x, y = unpack(entity:get_current_tile())
local tile = tiles["terrain"]:get(x, y)
if not entity.dead and tile and tile.index == GRAVEYARD_PURGATORY and entity.tile.index == GHOST then
entity:die()
ghost_death_sound:play()
end
entity:update(dt, player, entity.tile.index == GHOST and ghost_collision_map or collision_map)
end

if not player:check_alive() then
Expand Down
13 changes: 13 additions & 0 deletions src/entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function Entity.construct(args)
y = args.y,
tile = args.tile,
target = nil,
dead = false,
}

function entity.get_current_tile(self)
Expand All @@ -24,6 +25,10 @@ function Entity.construct(args)
return {x, y}
end

function entity.die(self)
self.dead = true
end

function entity.walk_to_target(self, current_tile, dt)
if not self.walk_sound:isPlaying() then
self.walk_sound:play()
Expand All @@ -39,6 +44,10 @@ function Entity.construct(args)
end

function entity.update(self, dt, player, collision_map)
if self.dead then
return
end

local current_tile = self:get_current_tile()
if self.target then
self:walk_to_target(current_tile, dt)
Expand Down Expand Up @@ -77,6 +86,10 @@ function Entity.construct(args)
end

function entity.render(self, camera)
if self.dead then
return
end

local transform = love.math.newTransform(self.x - camera.total_x, self.y - camera.total_y)
love.graphics.draw(self.tileset.image, self.tile.quad, transform)
-- if self.walk_animation.ongoing then
Expand Down
4 changes: 2 additions & 2 deletions src/tilemap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function TileMap.construct_tiles(tilemap, tileset)
end

-- for use with luafinding a 2d table of bools (passable or not)
function TileMap.construct_collision_map(tilemap, layer_name)
function TileMap.construct_collision_map(tilemap, layer_name, passable_getter)
local tiles = {}
local width = tilemap.width
for _, layer in ipairs(tilemap.layers) do
Expand All @@ -67,7 +67,7 @@ function TileMap.construct_collision_map(tilemap, layer_name)
for i, tile_index in ipairs(layer.data) do
local x = i % width
local y = math.floor(i / width)
tiles[x][y] = tile_index == 0 --passable if empty
tiles[x][y] = passable_getter(tile_index)
end
::continue::
end
Expand Down

0 comments on commit 1ac912b

Please sign in to comment.