Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Fixed mouse cursor in editor and map crashing without player, key, or…
Browse files Browse the repository at this point in the history
… door.
  • Loading branch information
jessefreeman committed Oct 5, 2021
1 parent c2674ce commit 1142f66
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 510 deletions.
504 changes: 43 additions & 461 deletions Game/Src/entities.lua

Large diffs are not rendered by default.

110 changes: 97 additions & 13 deletions Game/Src/scene-editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function EditorScene:Init()
local _editor = {
cursorPos = NewPoint(0, 0),
cursorOffset = NewPoint(),
cursorBounds = NewRect(0, 1, Display().X/8 - 1, Display().Y/8 - 3),
cursorBounds = NewRect(0, 1, Display().X/8, Display().Y/8 - 2),
currentTile = 0,
cursorCanvas = NewCanvas(8, 8),
inputTime = 0,
Expand All @@ -36,7 +36,9 @@ function EditorScene:Init()
startCount = 2,
startCounts = 2,
selectLock = false,
startLock = false
startLock = false,
mouseTime = 100,
lastMousePos = NewPoint()
}
setmetatable(_editor, EditorScene) -- make Account handle lookup

Expand All @@ -48,9 +50,12 @@ function EditorScene:Reset()

self.selectLock = Button(Buttons.Select, InputState.Down)
self.startLock = Button(Buttons.Start, InputState.Down)

self.currentTile = 0
self.startTimer = -1

self.cursorPos.X = math.floor((Display().X * .5) / 8)
self.cursorPos.Y = math.floor((Display().Y * .5) / 8) - 2

self.cursorCanvas:SetStroke(4, 1)
self.cursorCanvas:DrawRectangle(0, 0, self.cursorCanvas.Width, self.cursorCanvas.Height)

Expand All @@ -72,7 +77,7 @@ function EditorScene:Reset()
{02, 21}, -- Player
{03, 22}, -- Enemy
{04, 04}, -- Platform Left
{05, 28}, -- Platform Center
{05, 05}, -- Platform Center
{06, 06}, -- Platform Right
{07, 07}, -- Platform
{08, 28}, -- Platform Edge (Should remove?)
Expand All @@ -89,13 +94,27 @@ function EditorScene:Reset()
{19, 19}, -- Pillar Top
}

DrawRect(0, Display().Y - 9, Display().X, 9, BackgroundColor())

-- print("Rebuild picker")
DrawMetaSprite("tile-picker", 0, 17, false, false, DrawMode.Tile)

end

function EditorScene:Update(timeDelta)

local newMousePos = MousePosition()

if(self.lastMousePos.X ~= newMousePos.X or self.lastMousePos.Y ~= newMousePos.Y) then

self.lastMousePos = newMousePos

self.mouseTime = 1000

end



-- Reset select
if(Button(Buttons.Select, InputState.Released)) then
self.selectLock = false
Expand All @@ -119,7 +138,7 @@ function EditorScene:Update(timeDelta)

self.cursorPos.Y = self.cursorPos.Y - 1

if(self.cursorPos.Y < 0) then
if(self.cursorPos.Y <= 0) then
self.cursorPos.Y = self.cursorBounds.Height
end

Expand Down Expand Up @@ -162,18 +181,55 @@ function EditorScene:Update(timeDelta)
-- Reset the alt tile
self.altTile = false

elseif(Button(Buttons.A)) then
-- elseif(Button(Buttons.A) or MouseButton(0, InputState.Released)) then

-- local value = self.spriteId > 0 and self.spriteId or -1

-- if (Tile(self.cursorPos.X + self.cursorBounds.X, self.cursorPos.Y + self.cursorBounds.Y).SpriteId ~= value) then

-- Tile(self.cursorPos.X + self.cursorBounds.X, self.cursorPos.Y - 1 + self.cursorBounds.Y, value)

-- self:ResetBlink()

-- end

end

end


if(Button(Buttons.A) or MouseButton(0)) then

local c = self.cursorPos.X + self.cursorBounds.X
local r = self.cursorPos.Y + self.cursorBounds.Y

if(r > 1 and r < 18) then

local value = self.spriteId > 0 and self.spriteId or -1

if (Tile(self.cursorPos.X + self.cursorBounds.X, self.cursorPos.Y + self.cursorBounds.Y).SpriteId ~= value) then
if (Tile(c, r-1).SpriteId ~= value) then

DrawRect(self.cursorX, self.cursorY, 8, 8, BackgroundColor())
Tile(self.cursorPos.X + self.cursorBounds.X, self.cursorPos.Y + self.cursorBounds.Y, value)
Tile(self.cursorPos.X + self.cursorBounds.X, self.cursorPos.Y - 1 + self.cursorBounds.Y, value)

self:ResetBlink()

end
end

end

if(MouseButton(0, InputState.Released)) then

local c = self.cursorPos.X + self.cursorBounds.X
local r = self.cursorPos.Y + self.cursorBounds.Y

if(r > 17) then
--
-- print("Select tile", c)

self.currentTile = c

-- DrawRect(c * 8, 18, 8, 8, 2, DrawMode.SpriteBelow)

end

Expand All @@ -182,7 +238,7 @@ function EditorScene:Update(timeDelta)


-- Always check for the button release independent of the timer
if(Button(Buttons.B, InputState.Released)) then
if(Button(Buttons.B, InputState.Released) or MouseButton(1, InputState.Released)) then

self.altTile = not self.altTile

Expand All @@ -199,9 +255,25 @@ function EditorScene:Update(timeDelta)
end

-- Update all of the cursor and selection values
if(self.mouseTime > 0) then

self.mouseTime = self.mouseTime - (1000 * (timeDelta/1000))

-- newMousePos.X = math.floor(newMousePos.X/4) * 4
-- newMousePos.Y = math.floor(newMousePos.Y/4) * 4

self.cursorPos.X = math.floor((self.lastMousePos.X) / 8)
self.cursorPos.Y = math.floor((self.lastMousePos.Y) / 8)

self.cursorX = (((self.cursorPos.X + self.cursorBounds.X)) * 8)
self.cursorY = ((self.cursorPos.Y + self.cursorBounds.Y) * 8)
DrawRect(newMousePos.X, newMousePos.Y, 1, 1, 0, DrawMode.Mouse)

end

if(self.cursorBounds.Contains(self.cursorPos)) then
self.cursorX = ((self.cursorPos.X + self.cursorBounds.X)) * 8
self.cursorY = ((self.cursorPos.Y + self.cursorBounds.Y) * 8) - 8
end
--
self.tileId = self.currentTile + 1
self.selectionX = (self.tileId - 1) * 8

Expand Down Expand Up @@ -285,6 +357,18 @@ function EditorScene:Draw()

else

if(self.mouseTime > 0) then

DrawMetaSprite("cursor", self.lastMousePos.X, self.lastMousePos.Y, false, false, DrawMode.Mouse)

-- if(self.cursorPos.Y + self.cursorBounds.Y > 17) then

-- DrawRect((self.cursorPos.X + self.cursorBounds.X) * 8, 17 * 8 - 1, 8, 9, 3, DrawMode.SpriteBelow)

-- end

end

-- Draw the cursor
if(self.blink == true) then

Expand All @@ -302,7 +386,7 @@ function EditorScene:Draw()
end

-- Draw selected tile background
DrawRect(self.selectionX, Display().Y - 8, 8, 8, 3, DrawMode.Sprite)
DrawRect(self.selectionX, Display().Y - 9, 8, 9, 3, DrawMode.Sprite)

-- Draw selected tile
DrawSprite(self.spriteId, self.selectionX, Display().Y - 8, false, false, DrawMode.Sprite)
Expand Down
68 changes: 34 additions & 34 deletions Game/Src/scene-game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function GameScene:RestartLevel()

local flagMap = {}

SOLID, PLATFORM, DOOR_OPEN, DOOR_LOCKED, ENEMY, SPIKE, SWITCH_OFF, SWITCH_ON, LADDER, PLAYER, KEY, GEM = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
EMPTY, SOLID, PLATFORM, DOOR_OPEN, DOOR_LOCKED, ENEMY, SPIKE, SWITCH_OFF, SWITCH_ON, LADDER, PLAYER, KEY, GEM = -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

-- Find all the sources for each flag {Sprites, FlagId}
local spriteSrc = {
Expand Down Expand Up @@ -185,16 +185,14 @@ function GameScene:RestartLevel()

-- enemy
elseif(flag == ENEMY ) then

local flip = Tile(pos.X, pos.Y).SpriteId ~= MetaSprite("enemy").Sprites[1].Id

-- print("Enemy", x, y)

entity = Enemy:Init(x, y)
-- totalStars = totalStars + 1

-- Tile(pos.X, pos.Y, -1)

entity = Enemy:Init(x, y, flip)

-- Remove any enemy sprites from the map
spriteId = -1
flag = -1

-- spike
elseif(flag == SPIKE ) then
Expand Down Expand Up @@ -253,11 +251,14 @@ function GameScene:RestartLevel()

if(foundPlayer == false or foundDoor == false or foundKey == false) then

-- TODO need to pass a message to the scene it was missing things
SwitchScene(EDITOR)
self:ReturnToEditor()

return

end

DrawRect(0, Display().Y - 9, Display().X, 9, 0)

DrawMetaSprite("top-bar", 0, 0, false, false, DrawMode.TilemapCache)
DrawMetaSprite("bottom-hud", 0, Display().Y - 8, false, false, DrawMode.TilemapCache)
DrawMetaSprite("ui-o2-border", 8 * 8, Display().Y - 8, false, false, DrawMode.TilemapCache)
Expand Down Expand Up @@ -403,7 +404,7 @@ function GameScene:Update(timeDelta)
if(entity.Update ~= nil) then

-- TODO calculate the next animation frame?
self.instances[i]:Update(timeDelta)
self.instances[i]:Update(td)

end

Expand Down Expand Up @@ -515,42 +516,41 @@ function GameScene:Draw()

DrawText(message, offset, -1, DrawMode.SpriteAbove, "medium", 3, -4)

else

for i = 1, self.totalInstances do
end

local entity = self.instances[i]
for i = 1, self.totalInstances do

if(entity.alive == true) then
entity:Draw(0, 0)
end
local entity = self.instances[i]

if(entity.alive == true) then
entity:Draw(0, 0)
end

if(self.invalidateLives == true) then

for i = 1, self.maxLives do
DrawMetaSprite("ui-life", i * 8, Display().Y - 9, true, false, DrawMode.TilemapCache, self.lives < i and 1 or 3)
end

self.invalidateLives = false
end

end
if(self.invalidateLives == true) then

if(self.invalidateKey == true) then
DrawMetaSprite("ui-key", 40, Display().Y - 8, false, false, DrawMode.TilemapCache, self.hasKey == true and 2 or 1)
self.invalidateKey = false
for i = 1, self.maxLives do
DrawMetaSprite("ui-life", i * 8, Display().Y - 9, true, false, DrawMode.TilemapCache, self.lives < i and 1 or 3)
end

if(self.atDoor == false) then
self.invalidateLives = false

-- Need to draw the player last since the order of sprite draw calls matters
self.microPlatformer:Draw()

end
end

if(self.invalidateKey == true) then
DrawMetaSprite("ui-key", 40, Display().Y - 8, false, false, DrawMode.TilemapCache, self.hasKey == true and 2 or 1)
self.invalidateKey = false
end

if(self.atDoor == false) then

-- Need to draw the player last since the order of sprite draw calls matters
self.microPlatformer:Draw()

end


-- TODO for debugging flags
-- if(Button(Buttons.Start)) then

Expand Down
5 changes: 4 additions & 1 deletion Game/Src/scene-loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ function LoaderScene:Reset()

NewMetaSprite("enemy", {3, 22, 23})

NewMetaSprite("enemy-move", {3, 23})

-- Border
NewMetaSprite("top-bar", {38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38, 38}, 20)
NewMetaSprite("bottom-bar", {39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39, 39}, 20)
Expand All @@ -154,9 +156,10 @@ function LoaderScene:Reset()
-- Collectables
NewMetaSprite("ui-key", {15})
NewMetaSprite("ui-life", {24})
NewMetaSprite("cursor", {34})

-- Flag Tiles
NewMetaSprite("solid", {4, 5, 6, 8, 17, 18, 19, 28, 34})
NewMetaSprite("solid", {4, 5, 6, 8, 17, 18, 19, 28})
NewMetaSprite("platform", {7})
NewMetaSprite("door-open", {1})
NewMetaSprite("door-locked", {20})
Expand Down
2 changes: 1 addition & 1 deletion Game/Src/scene-splash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function SplashScene:Reset()

-- Create UI
DrawRect(0, 0, Display().X, 7, 0)
DrawRect(0, Display().Y - 8, Display().X, 8, 0)
DrawRect(0, Display().Y - 9, Display().X, 9, 0)


DrawText("SPACE STATION 8 BY JESSE FREEMAN", 20, -1, DrawMode.TilemapCache, "medium", 3, -4)
Expand Down
Binary file modified Game/map.spacestation8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Game/sprites.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified References/template-v2.aseprite
Binary file not shown.

0 comments on commit 1142f66

Please sign in to comment.