Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement flood filling.

  • Loading branch information...
commit f58fa1113b0a6bf1ed8e0d53d8d0a5904747cd50 1 parent 4a899e3
@stephank authored
View
1  src/objects/all.coffee
@@ -1,6 +1,7 @@
exports.registerWithWorld = (w) ->
w.registerType require './world_pillbox'
w.registerType require './world_base'
+ w.registerType require './flood_fill'
w.registerType require './tank'
w.registerType require './explosion'
w.registerType require './mine_explosion'
View
56 src/objects/flood_fill.coffee
@@ -0,0 +1,56 @@
+BoloObject = require '../object'
+
+
+## Flood fill
+
+# An invisible object, which implements the slow but sure flooding when a crater or new tile of
+# river is created.
+
+class FloodFill extends BoloObject
+
+ styled: null
+
+ serialization: (isCreate, p) ->
+ if isCreate
+ p 'H', 'x'
+ p 'H', 'y'
+
+ p 'B', 'lifespan'
+
+ #### World updates
+
+ spawn: (cell) ->
+ [@x, @y] = cell.getWorldCoordinates()
+ @lifespan = 16
+
+ anySpawn: ->
+ @cell = @world.map.cellAtWorld(@x, @y)
+ @neighbours = [@cell.neigh(1, 0), @cell.neigh(0, 1), @cell.neigh(-1, 0), @cell.neigh(0, -1)]
+
+ update: ->
+ if @lifespan-- == 0
+ @flood()
+ @world.destroy(this)
+
+ canGetWet: ->
+ result = no
+ for n in @neighbours
+ if not (n.base or n.pill) and n.isType(' ', '^', 'b')
+ result = yes
+ break
+ return result
+
+ flood: ->
+ if @canGetWet()
+ @cell.setType ' ', no
+ @spread()
+
+ spread: ->
+ for n in @neighbours
+ if not (n.base or n.pill) and n.isType('%')
+ @world.spawn FloodFill, n
+ return
+
+
+## Exports
+module.exports = FloodFill
View
13 src/world_map.coffee
@@ -11,6 +11,7 @@ net = require './net'
sounds = require './sounds'
WorldPillbox = require './objects/world_pillbox'
WorldBase = require './objects/world_base'
+FloodFill = require './objects/flood_fill'
## Terrain data
@@ -129,15 +130,15 @@ class WorldMapCell extends Map::CellClass
when '|' then '}'
when 'b' then ' '
@setType nextType
+ @map.world?.spawn(FloodFill, this) if @isType ' '
sfx
takeExplosionHit: ->
- if @pill?
- @pill.takeExplosionHit()
- else if @isType 'b'
- @setType ' '
- else unless @isType ' ', '^', 'b'
- @setType '%'
+ return @pill.takeExplosionHit() if @pill?
+ if @isType('b') then @setType ' '
+ else if not @isType(' ', '^', 'b') then @setType '%'
+ else return
+ @map.world?.spawn(FloodFill, this)
## Map class
Please sign in to comment.
Something went wrong with that request. Please try again.