@props = []# parallel array to @thang's trackedPropertiesKeys/Types
returnunlessthang
@thang = thang
forprop,propIndexinthang.trackedPropertiesKeys
type = thang.trackedPropertiesTypes[propIndex]
value = thang[prop]
iftypeis'Vector'
@props.pushvalue?.copy()# could try storing [x, y, z] or {x, y, z} here instead if this is expensive
elseiftypeis'object'
@props.push = clone(value,true)
else
@props.pushvalue
# Either pass storage and type, or don't pass either of them
getStoredProp: (propIndex, type, storage) ->
# Optimize it
unlesstype
type = @trackedPropertyTypes[propIndex]
storage = @trackedPropertyValues[propIndex]
iftypeis"Vector"
value = newVectorstorage[3*@frameIndex],storage[3*@frameIndex+1],storage[3*@frameIndex+2]
elseiftypeis'string'
specialKey = storage[@frameIndex]
value = @specialKeysToValues[specialKey]
elseiftypeis'Thang'
specialKey = storage[@frameIndex]
value = @thang.world.getThangByID@specialKeysToValues[specialKey]
elseiftypeis'array'
specialKey = storage[@frameIndex]
value = @specialKeysToValues[specialKey]
value = value.split('\x1E')# Record Separator
else
value = storage[@frameIndex]
value
getStateForProp: (prop) ->
# Get the property, whether we have it stored in @props or in @trackedPropertyValues. Optimize it.
# Figured based on http://jsperf.com/object-vs-array-vs-native-linked-list/13 that it should be faster with small arrays to do the indexOf reads (each up to 24x faster) than to do a single object read, and then we don't have to maintain an extra @props object; just keep array
propIndex = @trackedPropertyKeys.indexOfprop
returnnullifpropIndexis-1
value = @props[propIndex]
returnvalueifvalueisntundefinedor@hasRestored
return@props[propIndex]=@getStoredProppropIndex
restore: ->
# Restore trackedProperties' values to @thang, retrieving them from @trackedPropertyValues if needed. Optimize it.
# Optimize like no tomorrow--most performance-sensitive part of the whole app, called once per WorldFrame per Thang per trackedProperty, blocking the UI