Permalink
Browse files

Never modify lists while iterating through them

  • Loading branch information...
1 parent 0a1f304 commit 064152341b0df93083a619a7b3c933d515d811a2 Amos Wenger committed Aug 29, 2012
Showing with 71 additions and 42 deletions.
  1. +34 −10 source/Block.ooc
  2. +19 −7 source/Bullet.ooc
  3. +3 −1 source/Hero.ooc
  4. +1 −6 source/Instructions.ooc
  5. +10 −3 source/Level.ooc
  6. +1 −7 source/LevelSelect.ooc
  7. +1 −7 source/Menu.ooc
  8. +2 −1 source/bioboy.ooc
View
44 source/Block.ooc
@@ -12,21 +12,43 @@ Block: class extends Actor {
image: String
x, y: Int
+ // should stay in the list and updated
+ alive := true
+
+ // permanent back-and-forth
permanent := false
- dead := false
+
+ // movement definitely stopped
+ stopped := false
+
+ // can be destroyed by an explosion
destructible := true
+
+ // can be shot through / a block can move through it
permeable := false
+
+ // lets the hero slide along it
slippery := false
+ // inert block, never moves
+ inert := false
+
+ // block you can stand on
+ solid := true
+
+ // for collisions
box: Box
+
+ // current movement direction
dir := vec2(0, 0)
- pos: Vec2
- inert := false
- solid := true
+ // current position
+ pos: Vec2
- // only relevant for bomb (pretty code o/)
+ // only relevant for bomb
countdown := -1
+
+ // only relevant for blinks
blinkCount := 0
speed := 3.0
@@ -103,14 +125,14 @@ Block: class extends Actor {
countdown = 59
}
case =>
- if (!dead) {
+ if (!stopped) {
dir set!(orientation())
}
}
}
- update: func (delta: Float) {
- if (dead) return
+ update: func (delta: Float) -> Bool {
+ if (stopped) return false
if (playCount > 0) {
playCount -= 1
@@ -172,7 +194,7 @@ Block: class extends Actor {
} else {
dir set!(0, 0)
if (block inert) {
- dead = true
+ stopped = true
}
}
}
@@ -188,6 +210,8 @@ Block: class extends Actor {
kick()
}
}
+
+ !alive
}
applyThrust: func {
@@ -212,7 +236,7 @@ Block: class extends Actor {
}
_destroy: func {
- level blocks remove(this)
+ alive = false
destroy()
}
View
26 source/Bullet.ooc
@@ -12,6 +12,8 @@ Explosion: class extends Actor {
counter := 0
maxCounter := 10
+ alive := true
+
init: func (=engine, =pass, pos: Vec2, image: String) {
sprite = ImageSprite new(pos, "assets/png/%s.png" format(image))
sprite center!()
@@ -21,7 +23,7 @@ Explosion: class extends Actor {
update(0)
}
- update: func (delta: Float) {
+ update: func (delta: Float) -> Bool {
counter += 1
sprite alpha = 0.8 + ((maxCounter - counter) / (1.0 * maxCounter) * 0.2)
@@ -33,6 +35,8 @@ Explosion: class extends Actor {
if (counter >= maxCounter) {
destroy()
}
+
+ !alive
}
destroy: func {
@@ -41,7 +45,7 @@ Explosion: class extends Actor {
_destroy: func {
destroy()
- engine remove(this)
+ alive = false
}
}
@@ -56,6 +60,8 @@ DamageLabel: class extends Actor {
counter := 0
maxCounter := 100
+ alive := true
+
init: func (=engine, =pass, pos: Vec2, damage: Int) {
sprite = LabelSprite new(pos, "- %d" format(damage))
sprite fontSize = 30.0
@@ -65,15 +71,17 @@ DamageLabel: class extends Actor {
pass addSprite(sprite)
}
- update: func (delta: Float) {
+ update: func (delta: Float) -> Bool {
counter += 1
sprite alpha = (maxCounter - counter) / (1.0 * maxCounter)
sprite pos add!(0, -1)
if (counter >= maxCounter) {
- destroy()
+ _destroy()
}
+
+ !alive
}
destroy: func {
@@ -82,7 +90,7 @@ DamageLabel: class extends Actor {
_destroy: func {
destroy()
- engine remove(this)
+ alive = false
}
}
@@ -108,6 +116,8 @@ Bullet: class extends Actor {
type: BulletType
+ alive := true
+
init: func (=engine, =level, =pos, =dir) {
engine add(this)
ui = engine ui
@@ -124,7 +134,7 @@ Bullet: class extends Actor {
level play("plop")
}
- update: func (delta: Float) {
+ update: func (delta: Float) -> Bool {
pos add!(dir mul(speed))
box pos set!(pos add(sprite offset))
@@ -173,6 +183,8 @@ Bullet: class extends Actor {
break
}
}
+
+ !alive
}
destroy: func {
@@ -181,7 +193,7 @@ Bullet: class extends Actor {
_destroy: func {
destroy()
- engine remove(this)
+ alive = false
}
}
View
4 source/Hero.ooc
@@ -74,7 +74,7 @@ Hero: class extends Actor {
}
}
- update: func (delta: Float) {
+ update: func (delta: Float) -> Bool {
if (input isPressed(Keys CTRL) && hasPower(Power SLOW)) {
engine slomo = true
} else {
@@ -118,6 +118,8 @@ Hero: class extends Actor {
velX *= 0.7
}
}
+
+ false
}
handleCollisions: func {
View
7 source/Instructions.ooc
@@ -3,7 +3,7 @@ import ldkit/[Engine, Dead, Math, Sprites, UI, Actor, Input, Pass]
import bioboy
-Instructions: class extends Actor {
+Instructions: class {
engine: Engine
ui: UI
@@ -33,16 +33,11 @@ Instructions: class extends Actor {
clear: func {
pass enabled = false
input enabled = false
- engine remove(this)
}
enter: func {
pass enabled = true
input enabled = true
- engine add(this)
- }
-
- update: func (delta: Float) {
}
}
View
13 source/Level.ooc
@@ -116,17 +116,24 @@ Level: class extends Actor {
timeLabel setText(TimeHelper format(millis))
}
- update: func (delta: Float) {
+ update: func (delta: Float) -> Bool {
factor := engine slomo ? 0.5 : 1.0
millis += (delta * factor) as Long
updateHud()
- for(block in blocks) {
- block update(delta)
+ iter := blocks iterator()
+ while (iter hasNext?()) {
+ block := iter next()
+ if (block update(delta)) {
+ // returned true = wants to be destroyed
+ iter remove()
+ }
}
hero update(delta)
+
+ false
}
clear: func {
View
8 source/LevelSelect.ooc
@@ -107,7 +107,7 @@ Plan: class {
}
-LevelSelect: class extends Actor {
+LevelSelect: class {
// powers
dgun := false
@@ -276,7 +276,6 @@ LevelSelect: class extends Actor {
enter: func {
pass enabled = true
input enabled = true
- engine add(this)
buildGrid()
updateSelector(0, 0)
@@ -285,7 +284,6 @@ LevelSelect: class extends Actor {
clear: func {
pass enabled = false
input enabled = false
- engine remove(this)
}
toScreen: func (i, j: Int) -> Vec2 {
@@ -419,10 +417,6 @@ LevelSelect: class extends Actor {
}
}
- update: func (delta: Float) {
-
- }
-
destroy: func {
}
View
8 source/Menu.ooc
@@ -5,7 +5,7 @@ import structs/ArrayList
import Block, Hero, bioboy
-Menu: class extends Actor {
+Menu: class {
engine: Engine
ui: UI
@@ -112,17 +112,11 @@ Menu: class extends Actor {
enter: func {
pass enabled = true
input enabled = true
- engine add(this)
}
clear: func {
pass enabled = false
input enabled = false
- engine remove(this)
- }
-
- update: func (delta: Float) {
-
}
}
View
3 source/bioboy.ooc
@@ -95,7 +95,7 @@ Game: class extends Actor {
"assets/ogg/music/%s.ogg" format(musics get(currentMusic))
}
- update: func (delta: Float) {
+ update: func (delta: Float) -> Bool {
if (musicSource && musicSource getState() == SourceState STOPPED) {
currentMusic += 1
if (currentMusic >= musics size) {
@@ -109,6 +109,7 @@ Game: class extends Actor {
handleEvent(ev)
}
+ false
}
handleEvent: func (ev: GameEvent) {

0 comments on commit 0641523

Please sign in to comment.