Skip to content

Commit

Permalink
Added spike entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Cruor committed Feb 10, 2021
1 parent 8af9b7c commit 2313af7
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 0 deletions.
196 changes: 196 additions & 0 deletions src/entities/spikes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
local drawableSpriteStruct = require("structs.drawable_sprite")

local spikeDepth = -1
local spikeTexture = "danger/spikes/%s_%s00"

local spikeVariants = {
"default",
"outline",
"cliffside",
"reflection",
"tentacles"
}

local function getDirectionJustification(direction)
if direction == "up" then
return 0.0, 1.0

elseif direction == "down" then
return 0.0, 0.0

elseif direction == "left" then
return 1.0, 0.0

else
return 0.0, 0.0
end
end

local function getTentacleDirectionJustification(direction)
if direction == "up" or direction == "right" then
return 0.0, 0.5

elseif direction == "down" or direction == "left" then
return 1.0, 0.5
end
end

local function getJustification(direction, variant)
if variant == "tentacles" then
return getTentacleDirectionJustification(direction)

else
return getDirectionJustification(direction)
end
end

local function getDirectionRotation(direction)
return 0
end

local function getTentacleDirectionRotation(direction)
if direction == "up" then
return 0

elseif direction == "down" then
return math.pi

elseif direction == "left" then
return -math.pi / 2

else
return math.pi / 2
end
end

local function getRotation(direction, variant)
if variant == "tentacles" then
return getTentacleDirectionRotation(direction)

else
return getDirectionRotation(direction)
end
end

local function getSpikeSpritesFromTexture(entity, direction, variant, texture, step)
step = step or 8

local horizontal = direction == "left" or direction == "right"
local justificationX, justificationY = getJustification(direction, variant)
local rotation = getRotation(direction, variant)
local length = horizontal and (entity.height or step) or (entity.width or step)
local positionOffsetKey = horizontal and "y" or "x"

local position = {
x = entity.x,
y = entity.y
}

local sprites = {}

for i = 0, length - 1, step do
-- Tentacles overlap instead of "overflowing"
if i == length - step / 2 then
position[positionOffsetKey] -= step / 2
end

local sprite = drawableSpriteStruct.spriteFromTexture(texture, position)

sprite.depth = spikeDepth
sprite.rotation = rotation
sprite:setJustification(justificationX, justificationY)

table.insert(sprites, sprite)

position[positionOffsetKey] += step
end

return sprites
end

-- Spikes with side images
local function getNormalSpikeSprites(entity, direction, variant, step)
local texture = string.format(spikeTexture, variant, direction)

return getSpikeSpritesFromTexture(entity, direction, variant, texture, step or 8)
end

-- Spikes with rated sprites
local function getTentacleSprites(entity, direction, variant, step)
local texture= "danger/tentacles00"

return getSpikeSpritesFromTexture(entity, direction, variant, texture, step or 16)
end

local function getSpikeSprites(entity, direction)
local variant = entity.type or "trigger"

if variant == "tentacles" then
return getTentacleSprites(entity, direction, variant, 16)

else
return getNormalSpikeSprites(entity, direction, variant, 8)
end
end

local function getSpikePlacements(direction, variants)
local placements = {}
local horizontal = direction == "left" or direction == "right"
local lengthKey = horizontal and "height" or "width"

for i, variant in ipairs(variants) do
placements[i] = {
name = string.format("%s_%s", direction, variant),
data = {
type = variant,
}
}

placements[i].data[lengthKey] = 8
end

return placements
end

local spikeUp = {}

spikeUp.name = "spikesUp"
spikeUp.placements = getSpikePlacements("up", spikeVariants)

function spikeUp.sprite(room, entity)
return getSpikeSprites(entity, "up")
end

local spikeDown = {}

spikeDown.name = "spikesDown"
spikeDown.placements = getSpikePlacements("down", spikeVariants)

function spikeDown.sprite(room, entity)
return getSpikeSprites(entity, "down")
end

local spikeLeft = {}

spikeLeft.name = "spikesLeft"
spikeLeft.placements = getSpikePlacements("left", spikeVariants)

function spikeLeft.sprite(room, entity)
return getSpikeSprites(entity, "left")
end

local spikeRight = {}

spikeRight.name = "spikesRight"
spikeRight.placements = getSpikePlacements("right", spikeVariants)

function spikeRight.sprite(room, entity)
return getSpikeSprites(entity, "right")
end

return {
spikeUp,
spikeDown,
spikeLeft,
spikeRight
}
28 changes: 28 additions & 0 deletions src/lang/en_gb.lang
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,34 @@ entities.spring.placements.up=Spring (Up)
entities.wallSpringLeft.placements.right=Spring (Right)
entities.wallSpringRight.placements.left=Spring (Left)

# Spikes Up
entities.spikesUp.placements.up_default=Spikes (Up, Default)
entities.spikesUp.placements.up_outline=Spikes (Up, Outline)
entities.spikesUp.placements.up_cliffside=Spikes (Up, Cliffside)
entities.spikesUp.placements.up_reflection=Spikes (Up, Reflection)
entities.spikesUp.placements.up_tentacles=Spikes (Up, Tentacles)

# Spikes Right
entities.spikesRight.placements.right_default=Spikes (Right, Default)
entities.spikesRight.placements.right_outline=Spikes (Right, Outline)
entities.spikesRight.placements.right_cliffside=Spikes (Right, Cliffside)
entities.spikesRight.placements.right_reflection=Spikes (Right, Reflection)
entities.spikesRight.placements.right_tentacles=Spikes (Right, Tentacles)

# Spikes Down
entities.spikesDown.placements.down_default=Spikes (Down, Default)
entities.spikesDown.placements.down_outline=Spikes (Down, Outline)
entities.spikesDown.placements.down_cliffside=Spikes (Down, Cliffside)
entities.spikesDown.placements.down_reflection=Spikes (Down, Reflection)
entities.spikesDown.placements.down_tentacles=Spikes (Down, Tentacles)

# Spikes Left
entities.spikesLeft.placements.left_default=Spikes (Left, Default)
entities.spikesLeft.placements.left_outline=Spikes (Left, Outline)
entities.spikesLeft.placements.left_cliffside=Spikes (Left, Cliffside)
entities.spikesLeft.placements.left_reflection=Spikes (Left, Reflection)
entities.spikesLeft.placements.left_tentacles=Spikes (Left, Tentacles)


# -- Triggers --
# triggers.triggerName.placements.*
Expand Down

0 comments on commit 2313af7

Please sign in to comment.