Merge branch 'master' into production

This commit is contained in:
Scott Erickson 2014-01-13 17:01:32 -08:00
commit 8096c0e2d2
3 changed files with 32 additions and 3 deletions

View file

@ -32,4 +32,8 @@ class Rand
rand2: (min, max) =>
min + @rand max - min
# return an array of numbers from 0 to n-1, shuffled
randArray: (n) =>
_.shuffle [0...n]
module.exports = Rand

View file

@ -1,23 +1,42 @@
ThangState = require './thang_state'
{thangNames} = require './names'
{ArgumentError} = require './errors'
Rand = require './rand'
module.exports = class Thang
@className: "Thang"
@random = new Rand
# Random ordering for each sprite name
@ordering: (spriteName) ->
Thang.orders ?= {}
names = thangNames[spriteName]
if names
len = names.length
array = Thang.orders[spriteName]
unless array?
array = @random.randArray len
Thang.orders[spriteName] = array
else
array = []
array
@nextID: (spriteName) ->
Thang.lastIDNums ?= {}
names = thangNames[spriteName]
order = @ordering spriteName
if names
lastIDNum = Thang.lastIDNums[spriteName]
idNum = (if lastIDNum? then lastIDNum + 1 else 0)
Thang.lastIDNums[spriteName] = idNum
id = names[idNum % names.length]
id = names[order[idNum % names.length]]
if idNum >= names.length
id += Math.floor(idNum / names.length) + 1
else
Thang.lastIDNums[spriteName] = if Thang.lastIDNums[spriteName]? then Thang.lastIDNums[spriteName] + 1 else 0
id = spriteName + (Thang.lastIDNums[spriteName] or '')
id
@resetThangIDs: -> Thang.lastIDNums = {}
constructor: (@world, @spriteName, @id) ->
@ -137,4 +156,4 @@ module.exports = class Thang
for prop, val of o.finalState
# TODO: take some (but not all) of deserialize logic from ThangState to handle other types
t[prop] = val
t
t

View file

@ -245,11 +245,17 @@ module.exports = class World
addScripts: (scripts...) ->
@scripts = (@scripts ? []).concat scripts
addTrackedProperties: (props...) ->
@trackedProperties = (@trackedProperties ? []).concat props
serialize: ->
# Code hotspot; optimize it
if @frames.length < @totalFrames then worldShouldBeOverBeforeSerialization
[transferableObjects, nontransferableObjects] = [0, 0]
o = {name: @name, totalFrames: @totalFrames, maxTotalFrames: @maxTotalFrames, frameRate: @frameRate, dt: @dt, victory: @victory, userCodeMap: {}, showCoordinates: @showCoordinates, showGrid: @showGrid, showPaths: @showPaths, indieSprites: @indieSprites}
o = {name: @name, totalFrames: @totalFrames, maxTotalFrames: @maxTotalFrames, frameRate: @frameRate, dt: @dt, victory: @victory, userCodeMap: {}}
o[prop] = @[prop] for prop in @trackedProperties or []
for thangID, methods of @userCodeMap
serializedMethods = o.userCodeMap[thangID] = {}
for methodName, method of methods