Permalink
Browse files

Switch to event-based system for game

  • Loading branch information...
Amos Wenger
Amos Wenger committed Aug 28, 2012
1 parent ded3adf commit 11d432c50d0693f7804c51fd688b57645cb9445f
Showing with 85 additions and 64 deletions.
  1. +5 −3 source/Instructions.ooc
  2. +6 −5 source/Level.ooc
  3. +5 −6 source/LevelSelect.ooc
  4. +6 −7 source/Menu.ooc
  5. +4 −4 source/Story.ooc
  6. +59 −39 source/bioboy.ooc
View
@@ -1,16 +1,18 @@
import ldkit/[Engine, Dead, Math, Sprites, UI, Actor, Input, Pass]
+import bioboy
+
Instructions: class extends Actor {
engine: Engine
ui: UI
input: Input
pass: Pass
- onExit: Func
+ game: Game
- init: func (=engine, =onExit) {
+ init: func (=engine, =game) {
ui = engine ui
input = ui input sub()
@@ -22,7 +24,7 @@ Instructions: class extends Actor {
input onKeyPress(Keys ESC, ||
clear()
- onExit()
+ game on("return-to-menu")
)
clear()
View
@@ -4,7 +4,7 @@ import io/[FileReader, File]
import structs/ArrayList
import deadlogger/Log
-import Block, Hero, TimeHelper, LevelSelect, Power
+import Block, Hero, TimeHelper, LevelSelect, Power, bioboy
Level: class extends Actor {
@@ -16,7 +16,6 @@ Level: class extends Actor {
hero: Hero
levelFile: String
- onDone: Func (Bool)
pass, bgPass, objectPass, hudPass: Pass
input: Input
@@ -28,7 +27,9 @@ Level: class extends Actor {
logger := static Log getLogger(This name)
- init: func (=engine, =levelSelect, =onDone) {
+ game: Game
+
+ init: func (=engine, =levelSelect, =game) {
ui = engine ui
input = ui input sub()
@@ -42,7 +43,7 @@ Level: class extends Actor {
input onKeyPress(Keys ESC, ||
clear()
- onDone(false)
+ game on("level-fail")
)
}
@@ -159,7 +160,7 @@ Level: class extends Actor {
nextLevel: func {
clear()
play("tiling")
- onDone(true)
+ game on("level-success")
}
play: func (sound: String) {
View
@@ -3,7 +3,7 @@ import ldkit/[Engine, Dead, Math, Sprites, UI, Actor, Input, Pass]
import io/[FileReader, File]
import structs/ArrayList
-import Block, Hero, Power, TimeHelper
+import Block, Hero, Power, TimeHelper, bioboy
Medal: enum {
NONE
@@ -154,10 +154,9 @@ LevelSelect: class extends Actor {
points := 0
- onPlay: Func (String)
- onExit: Func
+ game: Game
- init: func (=engine, =onPlay, =onExit) {
+ init: func (=engine, =game) {
ui = engine ui
input = ui input sub()
@@ -206,11 +205,11 @@ LevelSelect: class extends Actor {
leave: func {
clear()
- onExit()
+ game on("return-to-menu")
}
takeAction: func {
- onPlay(item file)
+ game on("levelselect-play")
}
updateSelector: func (deltaCol, deltaRow: Int) {
View
@@ -3,7 +3,7 @@ import ldkit/[Engine, Dead, Math, Sprites, UI, Actor, Input, Pass]
import io/[FileReader, File]
import structs/ArrayList
-import Block, Hero
+import Block, Hero, bioboy
Menu: class extends Actor {
@@ -22,10 +22,9 @@ Menu: class extends Actor {
currentPos := 0
- onPlay: Func
- onInstructions: Func
+ game: Game
- init: func (=engine, =onPlay, =onInstructions) {
+ init: func (=engine, =game) {
ui = engine ui
input = ui input sub()
@@ -88,11 +87,11 @@ Menu: class extends Actor {
clear()
match currentPos {
case 0 =>
- onPlay()
+ game on("menu-play")
case 1 =>
- onInstructions()
+ game on("menu-instructions")
case 2 =>
- engine quit()
+ game on("quit")
}
}
View
@@ -3,7 +3,7 @@ import ldkit/[Engine, Dead, Math, Sprites, UI, Actor, Input, Pass]
import io/[FileReader, File]
import structs/ArrayList
-import Block, Hero
+import Block, Hero, bioboy
StoryCard: class {
image: String
@@ -25,9 +25,9 @@ Story: class extends Actor {
cards := ArrayList<StoryCard> new()
cardNum := 0
- onDone: Func
+ game: Game
- init: func (=engine, =name, =onDone) {
+ init: func (=engine, =name, =game) {
ui = engine ui
input = ui input sub()
@@ -87,7 +87,7 @@ Story: class extends Actor {
leave: func {
clear()
- onDone()
+ game on("return-to-menu")
}
loadCard: func {
View
@@ -4,14 +4,22 @@ use zombieconfig, ldkit, deadlogger
import zombieconfig
import ldkit/[Engine, Dead, Math, Sprites, UI, Actor, Sound]
import deadlogger/Logger
-import structs/ArrayList
+import structs/[ArrayList, Stack]
import Level, Story, Menu, LevelSelect, Instructions
main: func (args: ArrayList<String>) {
Game new()
}
+GameEvent: class {
+
+ name: String
+
+ init: func (=name) {}
+
+}
+
Game: class extends Actor {
musics := ["drama", "spywillie", "castle", "valse"] as ArrayList<String>
@@ -25,7 +33,7 @@ Game: class extends Actor {
engine: Engine
instructions: Instructions
- shouldSelectLevels := false
+ events := Stack<GameEvent> new()
init: func {
@@ -48,38 +56,16 @@ Game: class extends Actor {
engine ui mousePass enabled = false
engine ui escQuits = false
- menu = Menu new(engine, ||
- scheduleLevelSelect()
- , ||
- instructions enter()
- )
+ menu = Menu new(engine, this)
- instructions = Instructions new(engine, ||
- menu enter()
- )
+ instructions = Instructions new(engine, this)
- levelSelect = LevelSelect new(engine, |levelFile|
- if(level jumpTo(levelFile)) {
- levelSelect clear()
- }
- , ||
- levelSelect clear()
- menu enter()
- )
+ levelSelect = LevelSelect new(engine, this)
- level = Level new(engine, levelSelect, |success|
- level clear()
- if (success) {
- levelSelect success(level millis)
- }
- levelSelect updateSelector(success ? 1 : 0, 0)
- levelSelect enter()
- )
+ level = Level new(engine, levelSelect, this)
level clear()
- story = Story new(engine, "intro", ||
- menu enter()
- )
+ story = Story new(engine, "intro", this)
story loadCard()
play("assets/ogg/music/piano.ogg")
@@ -88,6 +74,14 @@ Game: class extends Actor {
engine run()
}
+ notify: func (event: GameEvent) {
+ events push(event)
+ }
+
+ on: func (name: String) {
+ notify(GameEvent new(name))
+ }
+
play: func (path: String) {
if (musicSource) {
musicSource free()
@@ -101,24 +95,50 @@ Game: class extends Actor {
"assets/ogg/music/%s.ogg" format(musics get(currentMusic))
}
- scheduleLevelSelect: func {
- shouldSelectLevels = true
- }
-
update: func (delta: Float) {
- if(shouldSelectLevels) {
- shouldSelectLevels = false
- levelSelect enter()
- play(musicPath())
- }
-
if (musicSource && musicSource getState() == SourceState STOPPED) {
currentMusic += 1
if (currentMusic >= musics size) {
currentMusic = 0
}
play(musicPath())
}
+
+ while (!events empty?()) {
+ ev := events pop()
+ handleEvent(ev)
+ }
+
+ }
+
+ handleEvent: func (ev: GameEvent) {
+ match (ev name) {
+ case "return-to-menu" =>
+ menu enter()
+
+ case "menu-play" =>
+ levelSelect enter()
+ play(musicPath())
+ case "menu-instructions" =>
+ instructions enter()
+
+ case "levelselect-play" =>
+ if (level jumpTo(levelSelect item file)) {
+ levelSelect clear()
+ }
+
+ case "level-success" =>
+ levelSelect success(level millis)
+ levelSelect updateSelector(1, 0)
+ levelSelect enter()
+
+ case "level-fail" =>
+ levelSelect updateSelector(0, 0)
+ levelSelect enter()
+
+ case "quit" =>
+ engine quit()
+ }
}
}

0 comments on commit 11d432c

Please sign in to comment.