mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2025-03-14 07:00:01 -04:00
Randomized naming for Thangs
This commit is contained in:
parent
76b795a8e6
commit
00cdb1e5ad
2 changed files with 26 additions and 2 deletions
|
@ -32,4 +32,14 @@ class Rand
|
|||
rand2: (min, max) =>
|
||||
min + @rand max - min
|
||||
|
||||
# return an array of randomly shuffled numbers from 0 to n, exclusive
|
||||
randArray: (n) =>
|
||||
array = ( i for i in [0..(n-1)] )
|
||||
|
||||
for i in [0..(n-1)]
|
||||
r = @rand n
|
||||
[ array[i], array[r] ] = [ array[r], array[i] ]
|
||||
|
||||
array
|
||||
|
||||
module.exports = Rand
|
|
@ -1,17 +1,31 @@
|
|||
ThangState = require './thang_state'
|
||||
{thangNames} = require './names'
|
||||
{ArgumentError} = require './errors'
|
||||
Rand = require './rand'
|
||||
|
||||
module.exports = class Thang
|
||||
@className: "Thang"
|
||||
@random = new Rand 0
|
||||
@ordering: (spriteName) ->
|
||||
Thang.orders ?= {}
|
||||
names = thangNames[spriteName]
|
||||
if names
|
||||
len = names.length
|
||||
array = Thang.orders[spriteName]
|
||||
if !array?
|
||||
array = @random.randArray len
|
||||
Thang.orders[spriteName] = array
|
||||
else
|
||||
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
|
||||
|
@ -137,4 +151,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
|
Loading…
Reference in a new issue