Working second level and not so working main menu

This commit is contained in:
Adam Blažek 2020-06-30 18:20:11 +02:00
parent e6c2008c74
commit dd6b015f76
6 changed files with 68 additions and 19 deletions

BIN
data/gui/button.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 207 B

12
data/levels/2.txt Normal file
View File

@ -0,0 +1,12 @@
#############
# #
# #
# #
# #
# #
#**** #####
#**** #####
##*** #####
# #####
#P #####
#############

View File

@ -1,6 +1,7 @@
import
nimgame2 / [
assets,
mosaic,
scene,
texturegraphic,
truetypefont,
@ -12,11 +13,15 @@ const
GameHeight* = 720
GameDim*: Dim = (1280, 720)
GameTitle* = "Soloplayer"
NumberOfLevels* = 2
var
titleScene*, mainScene*: Scene
normalFont*, titleFont*: TrueTypeFont
gfxData*: Assets[TextureGraphic]
buttonMosaic*: Mosaic
buttonSkin*: TextureGraphic
levelNumber*: Positive = 255
proc loadData*() =
normalFont = newTrueTypeFont()
@ -24,6 +29,9 @@ proc loadData*() =
titleFont = newTrueTypeFont()
discard titleFont.load("data/fnt/FantasqueSansMono-Bold.ttf", 96)
gfxData = newAssets[TextureGraphic]("data/gfx", newTextureGraphic)
buttonMosaic = newMosaic("data/gui/button.png", (8, 8))
buttonSkin = newTextureGraphic()
discard buttonSkin.assignTexture buttonMosaic.render(patternStretchBorder(8, 2))
proc freeData*() =
normalFont.free

View File

@ -40,6 +40,7 @@ const
type
Level* = ref object of TileMap
number*: Positive
proc init*(level: Level, number: Positive) =
init Tilemap level
@ -66,5 +67,5 @@ proc switchSecretTiles*(level: Level) =
level.initCollider
proc newLevel*(number: Positive): Level =
new result
result = Level(number: number)
result.init(number)

View File

@ -14,13 +14,13 @@ import
player
type
MainScene = ref object of Scene
MainScene* = ref object of Scene
level: Level
player: Player
proc init*(scene: MainScene) =
init scene.Scene
scene.level = newLevel(1)
proc loadLevel(scene: MainScene, number: Positive) =
scene.clear
scene.level = newLevel(number)
scene.level.pos = (GameDim - scene.level.TileMap.dim) / 2
scene.add scene.level
scene.level.layer = 0
@ -29,6 +29,10 @@ proc init*(scene: MainScene) =
scene.player.layer = 1
scene.player.resetPosition()
scene.add scene.player
scene.render
proc init*(scene: MainScene) =
init scene.Scene
proc free*(scene: MainScene) =
discard
@ -38,7 +42,8 @@ proc newMainScene*(): MainScene =
init result
method show*(scene: MainScene) =
echo "Switched to MainScene"
if scene.level == nil or scene.level.number != levelNumber:
scene.loadLevel levelNumber
method event*(scene: MainScene, event: Event) =
scene.eventScene event
@ -62,3 +67,5 @@ method update*(scene: MainScene, elapsed: float) =
scene.level.switchSecretTiles
if scene.level.collider.collide(scene.player.collider):
scene.level.switchSecretTiles
if ScancodeEscape.pressed:
game.scene = titleScene

View File

@ -1,9 +1,12 @@
import
nimgame2 / [
entity,
gui/button,
gui/widget,
input,
nimgame,
scene,
types,
entity,
settings,
textgraphic,
],
data
@ -13,23 +16,34 @@ type
proc init*(scene: TitleScene) =
init scene.Scene
let titleText = newTextGraphic(titleFont)
titleText.setText GameTitle
let title = newEntity()
title.graphic = titleText
title.centrify()
title.centrify
title.pos = (GameWidth / 2, GameHeight / 3)
scene.add title
let subtitleText = newTextGraphic(normalFont)
subtitleText.setText "Press any key to start"
let playButtonLabel = newTextGraphic(normalFont)
playButtonLabel.setText "PLAY"
let playButton = newGuiButton(buttonSkin, playButtonLabel)
playButton.centrify
playButton.pos = (GameWidth / 2, GameHeight / 2)
playButton.actions.add proc(widget: GuiWidget) =
game.scene = mainScene
scene.add playButton
let subtitle = newEntity()
subtitle.graphic = subtitleText
subtitle.centrify()
subtitle.pos = game.size / 2
scene.add subtitle
let exitButtonLabel = newTextGraphic(normalFont)
exitButtonLabel.setText "EXIT"
let exitButton = newGuiButton(buttonSkin, exitButtonLabel)
exitButton.centrify
exitButton.pos = (GameWidth / 2, GameHeight / 2 + 64)
exitButton.mbAllow = left
exitButton.enable
exitButton.actions.add proc(widget: GuiWidget) =
gameRunning = false
scene.add exitButton
proc free*(scene: TitleScene) =
discard
@ -38,6 +52,13 @@ proc newTitleScene*(): TitleScene =
new result, free
init result
method event*(scene: TitleScene, event: Event) =
if event.kind == KeyDown:
method update*(scene: TitleScene, elapsed: float) =
scene.updateScene(elapsed)
if Scancode1.pressed:
levelNumber = 1
game.scene = mainScene
if Scancode2.pressed:
levelNumber = 2
game.scene = mainScene
if ScancodeEscape.pressed:
gameRunning = false