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

Commit

Permalink
Fixed bug with air not counting down, ending/restarting game, and score.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessefreeman committed Oct 5, 2021
1 parent 9772466 commit c2674ce
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Game/Src/entities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function CreateEntity(x, y, spriteName)
entity.h = entity.spriteData.Height--math.floor(#entity.spriteData.spriteIDs / entity.spriteData.width)
end

print("Entity", spriteName, dump(entity))
-- print("Entity", spriteName, dump(entity))

return entity

Expand Down
6 changes: 3 additions & 3 deletions Game/Src/micro-platformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ function MicroPlatformer:Update(timeDelta)
--look for a solid tile
if (bottomFlag == SPIKE or self.player.hitRect.Y > (Display().Y - 16)) then

print("Kill Player")
-- print("Kill Player")

self.player.alive = false

Expand Down Expand Up @@ -211,7 +211,7 @@ function MicroPlatformer:Update(timeDelta)
self.player.dy = 0

--allow jumping again
self.player.isgrounded = true
-- self.player.isgrounded = true


-- Make sure the player doesn't accumulate speed if in a falling loop
Expand Down Expand Up @@ -255,7 +255,7 @@ function MicroPlatformer:Update(timeDelta)
self.spriteDir = self.player.dir

-- Climbing
if(self.currentFlag == LADDER) then
if(self.currentFlag == LADDER and self.player.isgrounded == false) then

self.player.spriteID = PLAYER_CLIMB

Expand Down
9 changes: 6 additions & 3 deletions Game/Src/scene-editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function EditorScene:Reset()
DrawText(" STR SEL A B", 3, -1, DrawMode.TilemapCache, "medium", 2, -4)
DrawText(" [ ] [ ] [ ] [ ]", 3, -1, DrawMode.TilemapCache, "medium", 1, -4)

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


-- Rebuild tilemap
self.tiles = {
Expand All @@ -89,6 +89,9 @@ function EditorScene:Reset()
{19, 19}, -- Pillar Top
}

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

end

function EditorScene:Update(timeDelta)
Expand Down Expand Up @@ -161,7 +164,7 @@ function EditorScene:Update(timeDelta)

elseif(Button(Buttons.A)) then

local value = self.spriteId > 1 and self.spriteId or -1
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

Expand Down Expand Up @@ -316,7 +319,7 @@ end

function EditorScene:RestoreState(value)

print("Restore state", state)
-- print("Restore state", state)

end

154 changes: 96 additions & 58 deletions Game/Src/scene-game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ function GameScene:Init()

QUIT_TIMER, RESPAWN_TIMER, GAME_OVER_TIMER, WIN_GAME = 2, 1, 4, 4

STEP_POINT, KEY_POINT, GEM_POINT, EXIT_POINT = 10, 50, 100, 500

local _game = {
totalInstances = 0,
bounds = Display(),
scoreDisplay = 0,
startTimer = -1,
startDelay = 1000,
-- startCount = 2,
-- startCounts = 2,
score = 0,
-- time = 0,
-- nextLevelDelay = 5,
-- nextLevelTime = 0,
-- nextLevel = false,
-- unlockDoor = false,
maxAir = 100,
air = 100,
airLoss = 4,
maxLives = 3,
lives = 3,
atDoor = false
Expand All @@ -54,35 +52,25 @@ function GameScene:Init()

end

function GameScene:RestartLevel()
-- Reset the player
self.playerEntity.hitRect.X = self.playerPos.x
self.playerEntity.hitRect.Y = self.playerPos.y
self.playerEntity.dx = 0
self.playerEntity.dy = 0
self.playerEntity.alive = true
self.playerEntity.dir = false
self.playerEntity.jumpvel = 2.5
self.playerEntity.isgrounded = false
function GameScene:Reset()

end
self.lives = self.maxLives

function GameScene:Reset()
self:RestartLevel()
end

function GameScene:RestartLevel()

-- Reset everything to default values
self.score = 0
-- self.nextLevel = false
-- self.unlockDoor = false
-- self.nextLevelDelay = 500
self.atDoor = false
self.lives = self.maxLives
self.startTimer = -1

-- Set the player sprites
self.playerEntity.sprites = MetaSprite("player").Sprites
self.air = self.maxAir + 10
self.score = 0
self.scoreDisplay = -1

-- Reset the key flag
self.hasKey = false


-- Create UI
-- DrawRect(0, 0, Display().X, 7, 0)
Expand Down Expand Up @@ -198,7 +186,7 @@ function GameScene:Reset()
-- enemy
elseif(flag == ENEMY ) then

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

entity = Enemy:Init(x, y)
-- totalStars = totalStars + 1
Expand Down Expand Up @@ -245,7 +233,6 @@ function GameScene:Reset()
-- gem
elseif(flag == GEM ) then

print("Found Gem")
end

end
Expand Down Expand Up @@ -278,18 +265,38 @@ function GameScene:Reset()

DrawText("SCORE", 14*8, Display().Y - 9, DrawMode.TilemapCache, "medium", 2, -4)

DrawText("SPACE STATION 8", 3, -1, DrawMode.TilemapCache, "medium", 3, -4)

local message = string.sub(lastImagePath.EntityName, 0, - #" .spacestation8.png"):upper()
local maxChars = 10
if(#message > maxChars) then
message = string.sub(message, 0, maxChars) .. "..."
end

DrawText("PLAYING " .. message, 8, -1, DrawMode.TilemapCache, "medium", 3, -4)
DrawText("SPACE STATION 8", Display().X - 68, -1, DrawMode.TilemapCache, "medium", 3, -4)

-- Update the total instance count
self.totalInstances = #self.instances


self:RestartLevel()
-- Reset the player
self.playerEntity.hitRect.X = self.playerPos.x
self.playerEntity.hitRect.Y = self.playerPos.y
self.playerEntity.dx = 0
self.playerEntity.dy = 0
self.playerEntity.alive = true
self.playerEntity.dir = false
self.playerEntity.jumpvel = 2.5
self.playerEntity.isgrounded = false
-- Set the player sprites
self.playerEntity.sprites = MetaSprite("player").Sprites

end

function GameScene:Update(timeDelta)

local td = timeDelta/1000

if(Button(Buttons.Select, InputState.Down) and self.startTimer == -1) then

self.startTimer = 0
Expand Down Expand Up @@ -323,6 +330,7 @@ function GameScene:Update(timeDelta)

else

self:RestoreTilemap()
self:RestartLevel()
self.startTimer = -1

Expand All @@ -339,38 +347,42 @@ function GameScene:Update(timeDelta)
local wasAlive = self.playerEntity.alive

-- Update the player logic first so we always have the correct player x and y pos
self.microPlatformer:Update(timeDelta / 1000)
self.microPlatformer:Update(td)


-- Check for collisions
if(self.microPlatformer.currentFlag == KEY) then

self.hasKey = true

Tile(self.microPlatformer.currentFlagPos.X, self.microPlatformer.currentFlagPos.Y, -1)
Tile(self.microPlatformer.currentFlagPos.X, self.microPlatformer.currentFlagPos.Y, -1, 0, -1)

self.invalidateKey = true

-- DrawRect(self.doorTile.X * 8, self.doorTile.Y * 8, 8, 8, 3)
Tile(self.doorTile.X, self.doorTile.Y, MetaSprite("door-open").Sprites[1].Id, 0, DOOR_OPEN)

self:IncreaseScore(KEY_POINT)

-- print("KEY COLLISION")

elseif(self.microPlatformer.currentFlag == GEM) then

self.score = score + 100
self:IncreaseScore(GEM_POINT)

Tile(self.microPlatformer.currentFlagPos.X, self.microPlatformer.currentFlagPos.Y, -1)
Tile(self.microPlatformer.currentFlagPos.X, self.microPlatformer.currentFlagPos.Y, -1, 0, -1)

elseif(self.microPlatformer.currentFlag == DOOR_OPEN and self.atDoor == false) then

print("LOCKED DOOR_OPEN")

self.atDoor = true

-- TODO exit level

self.startTimer = 0
self.startCount = WIN_GAME

self:IncreaseScore(EXIT_POINT)

end

self.playerEntity.hitRect.X = Repeat(self.playerEntity.hitRect.X, self.bounds.x-4)
Expand Down Expand Up @@ -407,14 +419,20 @@ function GameScene:Update(timeDelta)

end

if(self.atDoor == false and self.playerEntity.alive == true) then

self.air = self.air - (self.airLoss * td)

if(self.air <= 0) then
self.playerEntity.alive = false
end

end

if(self.playerEntity.alive == false) then

if(wasAlive == true) then

-- Reset the timer
-- self.time = 0

self.startTimer = 0
-- self.startCount = self.startCounts

Expand All @@ -427,31 +445,44 @@ function GameScene:Update(timeDelta)
self.startCount = GAME_OVER_TIMER
end


end

elseif(self.atDoor == false) then

self:IncreaseScore(STEP_POINT * td)

end

local percent = (self.air/ self.maxAir)

if(percent > 1) then
percent = 1
elseif(percent < 0) then
percent = 0
end

DrawRect((8 * 8) + 2, Display().Y - 6, 36 * percent, 3, 2, DrawMode.Sprite)

-- Update score
if(self.scoreDisplay ~= self.score) then

local diff = math.floor((self.score - self.scoreDisplay) / 4)

-- print(score, self.scoreDisplay, diff)
if(diff < 5) then

self.scoreDisplay = self.scoreDisplay + diff
self.scoreDisplay = self.score

if(self.scoreDisplay < 0) then
self.scoreDisplay = 0
end
else

-- Clear below
DrawText(LeftPad(tostring(self.scoreDisplay), 5, "0"), Display().X - (6 * 4), Display().Y - 9, DrawMode.TilemapCache, "medium", 3, -4)
self.scoreDisplay = self.scoreDisplay + diff

-- DrawText(LeftPad(tostring(self.scoreDisplay), 6, "0"), 15 * 8, Display().Y - 9, DrawMode.TilemapCache, "medium" - 4)
end

end

DrawText(LeftPad(tostring(Clamp(self.scoreDisplay, 0, 9999)), 4, "0"), Display().X - (6 * 4), Display().Y - 9, DrawMode.SpriteAbove, "medium", 3, -4)


end

function GameScene:Draw()
Expand Down Expand Up @@ -520,19 +551,20 @@ function GameScene:Draw()

end

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

local total = 20 * 18
-- local total = 20 * 18

for i = 1, total do
-- for i = 1, total do

local pos = CalculatePosition(i-1, 20)
-- local pos = CalculatePosition(i-1, 20)

DrawText(Flag(pos.X, pos.Y), pos.X * 8, pos.Y * 8, DrawMode.Sprite, "medium", 3, -5)
-- DrawText(Flag(pos.X, pos.Y), pos.X * 8, pos.Y * 8, DrawMode.Sprite, "medium", 3, -5)

end
-- end

end
-- end

end

Expand All @@ -544,12 +576,12 @@ end

function GameScene:RestoreState(value)

print("Restore state", state)
-- print("Restore state", state)

end

function GameScene:RestoreTilemap()

local total = #self.originalSprites

for i = 1, total do
Expand All @@ -568,3 +600,9 @@ function GameScene:ReturnToEditor()
SwitchScene(EDITOR)

end

function GameScene:IncreaseScore(value)

self.score = self.score + value

end
2 changes: 1 addition & 1 deletion Game/Src/scene-loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,6 @@ end

function LoaderScene:RestoreState(value)

print("Restore state", state)
-- print("Restore state", state)

end
Loading

0 comments on commit c2674ce

Please sign in to comment.