2014-06-24 06:30:29 -04:00
|
|
|
ModalView = require 'views/kinds/ModalView'
|
2014-07-13 20:31:09 -04:00
|
|
|
template = require 'templates/editor/level/modal/terrain_randomize'
|
2014-06-24 06:30:29 -04:00
|
|
|
CocoModel = require 'models/CocoModel'
|
|
|
|
|
2014-07-03 15:34:21 -04:00
|
|
|
clusters = {
|
|
|
|
'rocks': ['Rock 1', 'Rock 2', 'Rock 3', 'Rock 4', 'Rock 5', 'Rock Cluster 1', 'Rock Cluster 2', 'Rock Cluster 3']
|
2014-07-15 21:08:00 -04:00
|
|
|
'trees': ['Tree 1', 'Tree 2', 'Tree 3', 'Tree 4']
|
2014-07-03 15:34:21 -04:00
|
|
|
'shrubs': ['Shrub 1', 'Shrub 2', 'Shrub 3']
|
|
|
|
'houses': ['House 1', 'House 2', 'House 3', 'House 4']
|
|
|
|
'animals': ['Cow', 'Horse']
|
|
|
|
'wood': ['Firewood 1', 'Firewood 2', 'Firewood 3', 'Barrel']
|
|
|
|
'farm': ['Farm']
|
2014-07-20 13:04:21 -04:00
|
|
|
'cave': ['Cave']
|
|
|
|
'stone': ['Gargoyle', 'Rock Cluster 1', 'Rock Cluster 2', 'Rock Cluster 3']
|
2014-07-03 15:34:21 -04:00
|
|
|
}
|
|
|
|
|
2014-06-28 00:50:55 -04:00
|
|
|
presets = {
|
2014-07-20 13:04:21 -04:00
|
|
|
'dungeon': {
|
|
|
|
'type':'dungeon'
|
|
|
|
'borders':['Dungeon Wall']
|
|
|
|
'borderNoise':0
|
|
|
|
'borderSize':4
|
|
|
|
'floors':['Dungeon Floor']
|
|
|
|
'decorations': {
|
|
|
|
'cave': {
|
|
|
|
'num':[1,1]
|
|
|
|
'width': 20
|
|
|
|
'height': 20
|
|
|
|
'clusters': {
|
|
|
|
'cave':[1,1]
|
|
|
|
'stone':[2,4]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 15:34:21 -04:00
|
|
|
'grassy': {
|
|
|
|
'type':'grassy'
|
|
|
|
'borders':['Tree 1', 'Tree 2', 'Tree 3']
|
2014-07-20 13:04:21 -04:00
|
|
|
'borderNoise':1
|
|
|
|
'borderSize':0
|
2014-07-15 21:08:00 -04:00
|
|
|
'floors':['Grass01', 'Grass02', 'Grass03', 'Grass04', 'Grass05']
|
2014-07-03 15:34:21 -04:00
|
|
|
'decorations': {
|
|
|
|
'house': {
|
|
|
|
'num':[1,2] #min-max
|
|
|
|
'width': 20
|
|
|
|
'height': 20
|
|
|
|
'clusters': {
|
|
|
|
'houses':[1,1]
|
|
|
|
'trees':[1,2]
|
|
|
|
'shrubs':[0,3]
|
|
|
|
'rocks':[1,2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'farm': {
|
|
|
|
'num':[1,2] #min-max
|
|
|
|
'width': 20
|
|
|
|
'height': 20
|
|
|
|
'clusters': {
|
|
|
|
'farm':[1,1]
|
|
|
|
'shrubs':[2,3]
|
|
|
|
'wood':[2,4]
|
|
|
|
'animals':[2,3]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-15 22:37:10 -04:00
|
|
|
presetSizes = {
|
2014-06-28 00:50:55 -04:00
|
|
|
'small': {
|
|
|
|
'x':80
|
|
|
|
'y':68
|
|
|
|
}
|
|
|
|
'large': {
|
|
|
|
'x':160
|
|
|
|
'y':136
|
|
|
|
}
|
2014-07-15 22:37:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
thangSizes = {
|
2014-06-28 00:50:55 -04:00
|
|
|
'floorSize': {
|
|
|
|
'x':20
|
2014-07-15 19:16:43 -04:00
|
|
|
'y':17
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
|
|
|
'borderSize': {
|
|
|
|
'x':4
|
|
|
|
'y':4
|
2014-07-15 19:16:43 -04:00
|
|
|
'thickness':3
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-13 20:31:09 -04:00
|
|
|
module.exports = class TerrainRandomizeModal extends ModalView
|
|
|
|
id: 'terrain-randomize-modal'
|
2014-06-24 06:30:29 -04:00
|
|
|
template: template
|
|
|
|
thangs = []
|
|
|
|
|
|
|
|
events:
|
2014-07-13 20:31:09 -04:00
|
|
|
'click .choose-option': 'onRandomize'
|
2014-06-24 06:30:29 -04:00
|
|
|
|
|
|
|
onRevertModel: (e) ->
|
|
|
|
id = $(e.target).val()
|
|
|
|
CocoModel.backedUp[id].revert()
|
|
|
|
$(e.target).closest('tr').remove()
|
|
|
|
@reloadOnClose = true
|
|
|
|
|
2014-07-13 20:31:09 -04:00
|
|
|
onRandomize: (e) ->
|
2014-06-28 00:50:55 -04:00
|
|
|
target = $(e.target)
|
|
|
|
presetType = target.attr 'data-preset-type'
|
|
|
|
presetSize = target.attr 'data-preset-size'
|
2014-07-13 20:31:09 -04:00
|
|
|
@randomizeThangs presetType, presetSize
|
2014-07-15 21:08:00 -04:00
|
|
|
Backbone.Mediator.publish('randomize:terrain-generated',
|
2014-06-26 09:02:31 -04:00
|
|
|
'thangs': @thangs
|
|
|
|
)
|
2014-07-10 16:13:51 -04:00
|
|
|
@hide()
|
2014-06-28 00:50:55 -04:00
|
|
|
|
2014-07-13 20:31:09 -04:00
|
|
|
randomizeThangs: (presetName, presetSize) ->
|
2014-06-28 00:50:55 -04:00
|
|
|
preset = presets[presetName]
|
2014-07-15 22:37:10 -04:00
|
|
|
presetSize = presetSizes[presetSize]
|
2014-06-28 00:50:55 -04:00
|
|
|
@thangs = []
|
2014-07-13 20:31:09 -04:00
|
|
|
@randomizeFloor preset, presetSize
|
2014-07-20 13:04:21 -04:00
|
|
|
@randomizeBorder preset, presetSize, preset.borderNoise
|
2014-07-13 20:31:09 -04:00
|
|
|
@randomizeDecorations preset, presetSize
|
2014-06-28 00:50:55 -04:00
|
|
|
|
2014-07-13 20:31:09 -04:00
|
|
|
randomizeFloor: (preset, presetSize) ->
|
2014-07-15 22:37:10 -04:00
|
|
|
for i in _.range(0, presetSize.x, thangSizes.floorSize.x)
|
|
|
|
for j in _.range(0, presetSize.y, thangSizes.floorSize.y)
|
2014-06-28 00:50:55 -04:00
|
|
|
@thangs.push {
|
|
|
|
'id': @getRandomThang(preset.floors)
|
|
|
|
'pos': {
|
2014-07-15 22:37:10 -04:00
|
|
|
'x': i + thangSizes.floorSize.x/2
|
|
|
|
'y': j + thangSizes.floorSize.y/2
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-20 13:04:21 -04:00
|
|
|
randomizeBorder: (preset, presetSize, noiseFactor=1) ->
|
2014-07-15 22:37:10 -04:00
|
|
|
for i in _.range(0, presetSize.x, thangSizes.borderSize.x)
|
|
|
|
for j in _.range(thangSizes.borderSize.thickness)
|
2014-07-20 15:10:35 -04:00
|
|
|
while not @addThang {
|
2014-07-13 22:41:33 -04:00
|
|
|
'id': @getRandomThang(preset.borders)
|
|
|
|
'pos': {
|
2014-07-20 13:04:21 -04:00
|
|
|
'x': i + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.x/2, thangSizes.borderSize.x/2)
|
|
|
|
'y': 0 + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.y/2, thangSizes.borderSize.y)
|
2014-07-13 22:41:33 -04:00
|
|
|
}
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
continue
|
|
|
|
while not @addThang {
|
2014-07-13 22:41:33 -04:00
|
|
|
'id': @getRandomThang(preset.borders)
|
|
|
|
'pos': {
|
2014-07-20 13:04:21 -04:00
|
|
|
'x': i + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.x/2, thangSizes.borderSize.x/2)
|
|
|
|
'y': presetSize.y - preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.y, thangSizes.borderSize.y/2)
|
2014-07-13 22:41:33 -04:00
|
|
|
}
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
continue
|
2014-06-28 00:50:55 -04:00
|
|
|
|
2014-07-15 22:37:10 -04:00
|
|
|
for i in _.range(0, presetSize.y, thangSizes.borderSize.y)
|
2014-07-13 22:41:33 -04:00
|
|
|
for j in _.range(3)
|
2014-07-20 15:10:35 -04:00
|
|
|
while not @addThang {
|
2014-07-13 22:41:33 -04:00
|
|
|
'id': @getRandomThang(preset.borders)
|
|
|
|
'pos': {
|
2014-07-20 13:04:21 -04:00
|
|
|
'x': 0 + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.x/2, thangSizes.borderSize.x)
|
|
|
|
'y': i + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.y/2, thangSizes.borderSize.y/2)
|
2014-07-13 22:41:33 -04:00
|
|
|
}
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
continue
|
|
|
|
while not @addThang {
|
2014-07-13 22:41:33 -04:00
|
|
|
'id': @getRandomThang(preset.borders)
|
|
|
|
'pos': {
|
2014-07-20 13:04:21 -04:00
|
|
|
'x': presetSize.x - preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.x, thangSizes.borderSize.x/2)
|
|
|
|
'y': i + preset.borderSize/2 + noiseFactor * _.random(-thangSizes.borderSize.y/2, thangSizes.borderSize.y/2)
|
2014-07-13 22:41:33 -04:00
|
|
|
}
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
continue
|
2014-06-28 00:50:55 -04:00
|
|
|
|
2014-07-13 20:31:09 -04:00
|
|
|
randomizeDecorations: (preset, presetSize)->
|
2014-07-20 15:18:28 -04:00
|
|
|
if presetSize is presetSizes['small'] then sizeFactor = 1 else sizeFactor = 2
|
2014-07-03 15:34:21 -04:00
|
|
|
for name, decoration of preset.decorations
|
2014-07-20 15:18:28 -04:00
|
|
|
for num in _.range(sizeFactor * _.random(decoration.num[0], decoration.num[1]))
|
2014-07-20 15:10:35 -04:00
|
|
|
center = {
|
2014-07-15 21:08:00 -04:00
|
|
|
'x':_.random(decoration.width, presetSize.x - decoration.width),
|
2014-07-03 15:34:21 -04:00
|
|
|
'y':_.random(decoration.height, presetSize.y - decoration.height)
|
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
min = {
|
2014-07-03 15:34:21 -04:00
|
|
|
'x':center.x - decoration.width/2
|
2014-07-15 21:08:00 -04:00
|
|
|
'y':center.y - decoration.height/2
|
2014-07-03 15:34:21 -04:00
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
max = {
|
2014-07-03 15:34:21 -04:00
|
|
|
'x':center.x + decoration.width/2
|
2014-07-15 21:08:00 -04:00
|
|
|
'y':center.y + decoration.height/2
|
2014-07-03 15:34:21 -04:00
|
|
|
}
|
|
|
|
for cluster, range of decoration.clusters
|
|
|
|
for i in _.range(_.random(range[0], range[1]))
|
2014-07-20 15:10:35 -04:00
|
|
|
while not @addThang {
|
2014-07-03 15:34:21 -04:00
|
|
|
'id':@getRandomThang(clusters[cluster])
|
|
|
|
'pos':{
|
|
|
|
'x':_.random(min.x, max.x)
|
|
|
|
'y':_.random(min.y, max.y)
|
|
|
|
}
|
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
continue
|
2014-07-03 15:34:21 -04:00
|
|
|
|
2014-07-20 15:10:35 -04:00
|
|
|
addThang: (thang) ->
|
|
|
|
for existingThang in @thangs
|
|
|
|
return false if thang.pos.x is existingThang.pos.x and thang.pos.y is existingThang.pos.y
|
|
|
|
@thangs.push thang
|
|
|
|
true
|
2014-07-03 15:34:21 -04:00
|
|
|
|
2014-06-28 00:50:55 -04:00
|
|
|
getRandomThang: (thangList) ->
|
|
|
|
return thangList[_.random(0, thangList.length-1)]
|
2014-07-03 15:34:21 -04:00
|
|
|
|
2014-06-24 06:30:29 -04:00
|
|
|
getRenderData: ->
|
|
|
|
c = super()
|
2014-07-15 22:38:50 -04:00
|
|
|
c.presets = presets
|
|
|
|
c.presetSizes = presetSizes
|
2014-06-24 06:30:29 -04:00
|
|
|
c
|
|
|
|
|
|
|
|
onHidden: ->
|
|
|
|
location.reload() if @reloadOnClose
|