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 = {
|
2014-09-01 23:22:46 -04:00
|
|
|
'hero': {
|
|
|
|
'thangs': ['Hero Placeholder']
|
|
|
|
'margin': 1
|
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'rocks': {
|
|
|
|
'thangs': ['Rock 1', 'Rock 2', 'Rock 3', 'Rock 4', 'Rock 5', 'Rock Cluster 1', 'Rock Cluster 2', 'Rock Cluster 3']
|
|
|
|
'margin': 1
|
|
|
|
}
|
|
|
|
'trees': {
|
|
|
|
'thangs': ['Tree 1', 'Tree 2', 'Tree 3', 'Tree 4']
|
2014-09-01 23:07:50 -04:00
|
|
|
'margin': 0.5
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'shrubs': {
|
|
|
|
'thangs': ['Shrub 1', 'Shrub 2', 'Shrub 3']
|
2014-09-01 23:07:50 -04:00
|
|
|
'margin': 0.5
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'houses': {
|
|
|
|
'thangs': ['House 1', 'House 2', 'House 3', 'House 4']
|
|
|
|
'margin': 4
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'animals': {
|
|
|
|
'thangs': ['Cow', 'Horse']
|
|
|
|
'margin': 1
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'wood': {
|
2014-08-12 01:03:11 -04:00
|
|
|
'thangs': ['Firewood 1', 'Firewood 2', 'Firewood 3']
|
2014-07-21 09:55:35 -04:00
|
|
|
'margin': 1
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'farm': {
|
|
|
|
'thangs': ['Farm']
|
|
|
|
'margin': 9
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'cave': {
|
|
|
|
'thangs': ['Cave']
|
|
|
|
'margin': 5
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'stone': {
|
|
|
|
'thangs': ['Gargoyle', 'Rock Cluster 1', 'Rock Cluster 2', 'Rock Cluster 3']
|
|
|
|
'margin': 1
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-08-12 01:03:11 -04:00
|
|
|
'torch': {
|
|
|
|
'thangs': ['Torch']
|
|
|
|
'margin': 0
|
|
|
|
}
|
|
|
|
'chains': {
|
|
|
|
'thangs': ['Chains']
|
|
|
|
'margin': 0
|
|
|
|
}
|
|
|
|
'barrel': {
|
|
|
|
'thangs': ['Barrel']
|
|
|
|
'margin': 1
|
|
|
|
}
|
2014-07-24 19:03:03 -04:00
|
|
|
'doors': {
|
|
|
|
'thangs': ['Dungeon Door']
|
|
|
|
'margin': -1
|
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'grass_floor': {
|
|
|
|
'thangs': ['Grass01', 'Grass02', 'Grass03', 'Grass04', 'Grass05']
|
|
|
|
'margin': -1
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'dungeon_wall': {
|
|
|
|
'thangs': ['Dungeon Wall']
|
2014-08-06 10:43:56 -04:00
|
|
|
'margin': 2
|
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'dungeon_floor': {
|
|
|
|
'thangs': ['Dungeon Floor']
|
|
|
|
'margin': -1
|
2014-08-06 10:43:56 -04:00
|
|
|
}
|
2014-08-11 17:41:42 -04:00
|
|
|
'indoor_wall': {
|
|
|
|
'thangs': ['Indoor Wall']
|
|
|
|
'margin': 2
|
|
|
|
}
|
|
|
|
'indoor_floor': {
|
|
|
|
'thangs': ['Indoor Floor']
|
|
|
|
'margin': -1
|
|
|
|
}
|
|
|
|
'furniture': {
|
2014-08-12 11:12:42 -04:00
|
|
|
'thangs': ['Bookshelf', 'Chair', 'Table', 'Candle', 'Treasure Chest']
|
2014-08-11 17:41:42 -04:00
|
|
|
'margin': -1
|
|
|
|
}
|
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': {
|
2014-09-02 17:16:36 -04:00
|
|
|
'terrainName': 'Dungeon'
|
2014-07-20 13:04:21 -04:00
|
|
|
'type':'dungeon'
|
2014-07-21 09:55:35 -04:00
|
|
|
'borders':'dungeon_wall'
|
2014-07-20 13:04:21 -04:00
|
|
|
'borderNoise':0
|
|
|
|
'borderSize':4
|
2014-08-12 01:03:11 -04:00
|
|
|
'borderThickness':1
|
2014-07-21 09:55:35 -04:00
|
|
|
'floors':'dungeon_floor'
|
2014-07-20 13:04:21 -04:00
|
|
|
'decorations': {
|
2014-08-11 21:08:22 -04:00
|
|
|
'Room': {
|
|
|
|
'num': [1,1]
|
|
|
|
'width': [12, 20]
|
|
|
|
'height': [8, 16]
|
|
|
|
'thickness': [2,2]
|
|
|
|
'cluster': 'dungeon_wall'
|
|
|
|
}
|
2014-09-01 23:22:46 -04:00
|
|
|
'hero': {
|
|
|
|
'num': [1, 1]
|
|
|
|
'width': 2
|
|
|
|
'height': 2
|
|
|
|
'clusters': {
|
|
|
|
'hero': [1, 1]
|
|
|
|
}
|
|
|
|
}
|
2014-08-12 01:03:11 -04:00
|
|
|
'Barrels': {
|
|
|
|
'num': [1,1]
|
|
|
|
'width': [8, 12]
|
|
|
|
'height': [8, 12]
|
|
|
|
'numBarrels': [4,6]
|
2014-08-27 15:24:03 -04:00
|
|
|
'cluster': 'barrel'
|
2014-08-12 01:03:11 -04:00
|
|
|
}
|
2014-07-20 13:04:21 -04:00
|
|
|
'cave': {
|
|
|
|
'num':[1,1]
|
2014-07-24 19:03:03 -04:00
|
|
|
'width': 10
|
|
|
|
'height': 10
|
2014-07-20 13:04:21 -04:00
|
|
|
'clusters': {
|
|
|
|
'cave':[1,1]
|
|
|
|
'stone':[2,4]
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 17:41:42 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'indoor': {
|
2014-09-02 17:16:36 -04:00
|
|
|
'terrainName': 'Indoor'
|
2014-08-11 17:41:42 -04:00
|
|
|
'type':'indoor'
|
|
|
|
'borders':'indoor_wall'
|
|
|
|
'borderNoise':0
|
|
|
|
'borderSize':4
|
2014-08-12 01:03:11 -04:00
|
|
|
'borderThickness':1
|
2014-08-11 17:41:42 -04:00
|
|
|
'floors':'indoor_floor'
|
|
|
|
'decorations': {
|
2014-08-11 21:08:22 -04:00
|
|
|
'Room': {
|
|
|
|
'num': [1,1]
|
|
|
|
'width': [12, 20]
|
|
|
|
'height': [8, 16]
|
|
|
|
'thickness': [2,2]
|
|
|
|
'cluster': 'indoor_wall'
|
|
|
|
}
|
2014-09-01 23:22:46 -04:00
|
|
|
'hero': {
|
|
|
|
'num': [1, 1]
|
|
|
|
'width': 2
|
|
|
|
'height': 2
|
|
|
|
'clusters': {
|
|
|
|
'hero': [1, 1]
|
|
|
|
}
|
|
|
|
}
|
2014-08-11 17:41:42 -04:00
|
|
|
'furniture': {
|
|
|
|
'num':[1,2]
|
|
|
|
'width': 15
|
|
|
|
'height': 15
|
|
|
|
'clusters': {
|
2014-08-12 11:12:42 -04:00
|
|
|
'furniture':[2,4]
|
2014-08-11 17:41:42 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-20 13:04:21 -04:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 15:34:21 -04:00
|
|
|
'grassy': {
|
2014-09-02 17:16:36 -04:00
|
|
|
'terrainName': 'Grass'
|
2014-07-03 15:34:21 -04:00
|
|
|
'type':'grassy'
|
2014-07-21 09:55:35 -04:00
|
|
|
'borders':'trees'
|
2014-07-20 13:04:21 -04:00
|
|
|
'borderNoise':1
|
|
|
|
'borderSize':0
|
2014-08-12 01:03:11 -04:00
|
|
|
'borderThickness':3
|
2014-07-21 09:55:35 -04:00
|
|
|
'floors':'grass_floor'
|
2014-07-03 15:34:21 -04:00
|
|
|
'decorations': {
|
2014-09-01 23:22:46 -04:00
|
|
|
'hero': {
|
|
|
|
'num': [1, 1]
|
|
|
|
'width': 2
|
|
|
|
'height': 2
|
|
|
|
'clusters': {
|
|
|
|
'hero': [1, 1]
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 15:34:21 -04:00
|
|
|
'house': {
|
|
|
|
'num':[1,2] #min-max
|
2014-07-21 09:55:35 -04:00
|
|
|
'width': 15
|
|
|
|
'height': 15
|
2014-07-03 15:34:21 -04:00
|
|
|
'clusters': {
|
|
|
|
'houses':[1,1]
|
|
|
|
'trees':[1,2]
|
|
|
|
'shrubs':[0,3]
|
|
|
|
'rocks':[1,2]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'farm': {
|
2014-07-21 09:55:35 -04:00
|
|
|
'num':[1,1] #min-max
|
|
|
|
'width': 25
|
|
|
|
'height': 15
|
2014-07-03 15:34:21 -04:00
|
|
|
'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
|
2014-08-12 01:03:11 -04:00
|
|
|
'sizeFactor':1
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
|
|
|
'large': {
|
|
|
|
'x':160
|
|
|
|
'y':136
|
2014-08-12 01:03:11 -04:00
|
|
|
'sizeFactor':2
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
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-09-02 17:16:36 -04:00
|
|
|
}}
|
|
|
|
|
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
|
|
|
|
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-09-02 17:16:36 -04:00
|
|
|
Backbone.Mediator.publish 'editor:random-terrain-generated', thangs: @thangs, terrain: presets[presetType].terrainName
|
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-07-24 19:03:03 -04:00
|
|
|
@falseCount = 0
|
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-25 02:46:27 -04:00
|
|
|
@rects = []
|
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 {
|
2014-07-21 09:55:35 -04:00
|
|
|
'id': @getRandomThang(clusters[preset.floors].thangs)
|
2014-06-28 00:50:55 -04:00
|
|
|
'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-21 09:55:35 -04:00
|
|
|
'margin': clusters[preset.floors].margin
|
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)
|
2014-08-12 01:03:11 -04:00
|
|
|
for j in _.range(preset.borderThickness)
|
|
|
|
# Bottom wall
|
2014-07-20 15:10:35 -04:00
|
|
|
while not @addThang {
|
2014-07-21 09:55:35 -04:00
|
|
|
'id': @getRandomThang(clusters[preset.borders].thangs)
|
2014-07-13 22:41:33 -04:00
|
|
|
'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-07-21 09:55:35 -04:00
|
|
|
'margin': clusters[preset.borders].margin
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
continue
|
2014-08-12 01:03:11 -04:00
|
|
|
|
|
|
|
# Top wall
|
2014-07-20 15:10:35 -04:00
|
|
|
while not @addThang {
|
2014-07-21 09:55:35 -04:00
|
|
|
'id': @getRandomThang(clusters[preset.borders].thangs)
|
2014-07-13 22:41:33 -04:00
|
|
|
'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-07-21 09:55:35 -04:00
|
|
|
'margin': clusters[preset.borders].margin
|
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-08-12 01:03:11 -04:00
|
|
|
# Double wall on top
|
|
|
|
if preset.type is 'dungeon'
|
|
|
|
@addThang {
|
|
|
|
'id': @getRandomThang(clusters[preset.borders].thangs)
|
|
|
|
'pos': {
|
2014-08-27 15:24:03 -04:00
|
|
|
'x': i + preset.borderSize/2
|
2014-08-12 01:03:11 -04:00
|
|
|
'y': presetSize.y - 3 * preset.borderSize/2
|
|
|
|
}
|
|
|
|
'margin': clusters[preset.borders].margin
|
2014-08-27 15:24:03 -04:00
|
|
|
}
|
2014-08-12 01:03:11 -04:00
|
|
|
if ( i / preset.borderSize ) % 2 and i isnt presetSize.x - thangSizes.borderSize.x
|
|
|
|
@addThang {
|
|
|
|
'id': @getRandomThang(clusters['torch'].thangs)
|
|
|
|
'pos': {
|
2014-08-27 15:24:03 -04:00
|
|
|
'x': i + preset.borderSize
|
2014-09-01 23:07:50 -04:00
|
|
|
'y': presetSize.y - preset.borderSize / 2
|
2014-08-12 01:03:11 -04:00
|
|
|
}
|
|
|
|
'margin': clusters['torch'].margin
|
2014-08-27 15:24:03 -04:00
|
|
|
}
|
2014-08-12 01:03:11 -04:00
|
|
|
else if ( i / preset.borderSize ) % 2 is 0 and i and _.random(100) < 30
|
|
|
|
@addThang {
|
|
|
|
'id': @getRandomThang(clusters['chains'].thangs)
|
|
|
|
'pos': {
|
2014-08-27 15:24:03 -04:00
|
|
|
'x': i + preset.borderSize
|
2014-09-01 23:07:50 -04:00
|
|
|
'y': presetSize.y - preset.borderSize / 2
|
2014-08-12 01:03:11 -04:00
|
|
|
}
|
|
|
|
'margin': clusters['chains'].margin
|
2014-08-27 15:24:03 -04:00
|
|
|
}
|
2014-08-12 01:03:11 -04:00
|
|
|
|
2014-07-15 22:37:10 -04:00
|
|
|
for i in _.range(0, presetSize.y, thangSizes.borderSize.y)
|
2014-08-12 01:03:11 -04:00
|
|
|
for j in _.range(preset.borderThickness)
|
|
|
|
# Left wall
|
2014-07-20 15:10:35 -04:00
|
|
|
while not @addThang {
|
2014-07-21 09:55:35 -04:00
|
|
|
'id': @getRandomThang(clusters[preset.borders].thangs)
|
2014-07-13 22:41:33 -04:00
|
|
|
'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-07-21 09:55:35 -04:00
|
|
|
'margin': clusters[preset.borders].margin
|
2014-06-28 00:50:55 -04:00
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
continue
|
2014-08-12 01:03:11 -04:00
|
|
|
|
|
|
|
# Right wall
|
2014-07-20 15:10:35 -04:00
|
|
|
while not @addThang {
|
2014-07-21 09:55:35 -04:00
|
|
|
'id': @getRandomThang(clusters[preset.borders].thangs)
|
2014-07-13 22:41:33 -04:00
|
|
|
'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-07-21 09:55:35 -04:00
|
|
|
'margin': clusters[preset.borders].margin
|
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-03 15:34:21 -04:00
|
|
|
for name, decoration of preset.decorations
|
2014-08-12 01:03:11 -04:00
|
|
|
for num in _.range(presetSize.sizeFactor * _.random(decoration.num[0], decoration.num[1]))
|
2014-07-24 19:03:03 -04:00
|
|
|
if @['build'+name] isnt undefined
|
|
|
|
@['build'+name](preset, presetSize, decoration)
|
|
|
|
continue
|
2014-07-25 02:46:27 -04:00
|
|
|
while true
|
|
|
|
rect = {
|
|
|
|
'x':_.random(decoration.width/2 + preset.borderSize/2 + thangSizes.borderSize.x, presetSize.x - decoration.width/2 - preset.borderSize/2 - thangSizes.borderSize.x),
|
|
|
|
'y':_.random(decoration.height/2 + preset.borderSize/2 + thangSizes.borderSize.y, presetSize.y - decoration.height/2 - preset.borderSize/2 - thangSizes.borderSize.y)
|
|
|
|
'width':decoration.width
|
|
|
|
'height':decoration.height
|
|
|
|
}
|
|
|
|
break if @addRect rect
|
2014-08-06 10:43:56 -04:00
|
|
|
|
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-21 09:55:35 -04:00
|
|
|
'id':@getRandomThang(clusters[cluster].thangs)
|
2014-07-03 15:34:21 -04:00
|
|
|
'pos':{
|
2014-07-21 09:55:35 -04:00
|
|
|
'x':_.random(rect.x - rect.width/2, rect.x + rect.width/2)
|
|
|
|
'y':_.random(rect.y - rect.height/2, rect.y + rect.height/2)
|
2014-07-03 15:34:21 -04:00
|
|
|
}
|
2014-07-21 09:55:35 -04:00
|
|
|
'margin':clusters[cluster].margin
|
2014-07-03 15:34:21 -04:00
|
|
|
}
|
2014-07-20 15:10:35 -04:00
|
|
|
continue
|
2014-07-03 15:34:21 -04:00
|
|
|
|
2014-07-24 19:03:03 -04:00
|
|
|
buildRoom: (preset, presetSize, room) ->
|
2014-09-01 23:07:50 -04:00
|
|
|
grid = preset.borderSize
|
2014-07-25 02:46:27 -04:00
|
|
|
while true
|
|
|
|
rect = {
|
2014-09-01 23:07:50 -04:00
|
|
|
'width':presetSize.sizeFactor * (room.width[0] + grid * _.random(0, (room.width[1] - room.width[0])/grid))
|
|
|
|
'height':presetSize.sizeFactor * (room.height[0] + grid * _.random(0, (room.height[1] - room.height[0])/grid))
|
2014-07-25 02:46:27 -04:00
|
|
|
}
|
2014-09-01 23:07:50 -04:00
|
|
|
# This logic isn't quite right--it makes the rooms bigger than intended--but it's snapping correctly, which is fine for now.
|
|
|
|
rect.width = Math.round((rect.width - grid) / (2 * grid)) * 2 * grid + grid
|
|
|
|
rect.height = Math.round((rect.height - grid) / (2 * grid)) * 2 * grid + grid
|
2014-07-25 02:46:27 -04:00
|
|
|
roomThickness = _.random(room.thickness[0], room.thickness[1])
|
2014-09-01 23:07:50 -04:00
|
|
|
rect.x = _.random(rect.width/2 + grid * (roomThickness+1.5), presetSize.x - rect.width/2 - grid * (roomThickness+1.5))
|
|
|
|
rect.y = _.random(rect.height/2 + grid * (roomThickness+2.5), presetSize.y - rect.height/2 - grid * (roomThickness+3.5))
|
2014-08-06 10:43:56 -04:00
|
|
|
# Snap room walls to the wall grid.
|
2014-09-01 23:07:50 -04:00
|
|
|
rect.x = Math.round((rect.x - grid / 2) / grid) * grid
|
|
|
|
rect.y = Math.round((rect.y - grid / 2) / grid) * grid
|
2014-07-25 02:46:27 -04:00
|
|
|
break if @addRect {
|
|
|
|
'x': rect.x
|
|
|
|
'y': rect.y
|
2014-09-01 23:07:50 -04:00
|
|
|
'width': rect.width + 2.5 * roomThickness * grid
|
|
|
|
'height': rect.height + 2.5 * roomThickness * grid
|
2014-07-25 02:46:27 -04:00
|
|
|
}
|
2014-07-24 19:03:03 -04:00
|
|
|
|
2014-09-01 23:07:50 -04:00
|
|
|
xRange = _.range(rect.x - rect.width/2 + grid, rect.x + rect.width/2, grid)
|
2014-07-24 19:03:03 -04:00
|
|
|
topDoor = _.random(1) > 0.5
|
|
|
|
topDoorX = xRange[_.random(0, xRange.length-1)]
|
|
|
|
bottomDoor = if not topDoor then true else _.random(1) > 0.5
|
|
|
|
bottomDoorX = xRange[_.random(0, xRange.length-1)]
|
|
|
|
|
|
|
|
for t in _.range(0, roomThickness+1)
|
2014-09-01 23:07:50 -04:00
|
|
|
for i in _.range(rect.x - rect.width/2 - (t-1) * grid, rect.x + rect.width/2 + t * grid, grid)
|
2014-08-12 01:03:11 -04:00
|
|
|
# Bottom wall
|
2014-07-24 19:03:03 -04:00
|
|
|
thang = {
|
|
|
|
'id': @getRandomThang(clusters[room.cluster].thangs)
|
|
|
|
'pos': {
|
|
|
|
'x': i
|
2014-09-01 23:07:50 -04:00
|
|
|
'y': rect.y - rect.height/2 - t * grid
|
2014-07-24 19:03:03 -04:00
|
|
|
}
|
|
|
|
'margin': clusters[room.cluster].margin
|
|
|
|
}
|
|
|
|
if i is bottomDoorX and bottomDoor
|
|
|
|
thang.id = @getRandomThang(clusters['doors'].thangs)
|
2014-09-01 23:07:50 -04:00
|
|
|
thang.pos.y -= grid/3
|
2014-07-24 19:03:03 -04:00
|
|
|
@addThang thang unless i is bottomDoorX and t isnt roomThickness and bottomDoor
|
|
|
|
|
2014-09-01 23:07:50 -04:00
|
|
|
if t is roomThickness and i isnt rect.x - rect.width/2 - (t-1) * grid and preset.type is 'dungeon'
|
|
|
|
if ( i isnt bottomDoorX and i isnt bottomDoorX + grid ) or not bottomDoor
|
2014-08-12 01:03:11 -04:00
|
|
|
@addThang {
|
|
|
|
'id': @getRandomThang(clusters['torch'].thangs)
|
|
|
|
'pos': {
|
2014-09-01 23:07:50 -04:00
|
|
|
'x': thang.pos.x - grid / 2
|
|
|
|
'y': thang.pos.y + grid
|
2014-08-27 15:24:03 -04:00
|
|
|
}
|
2014-08-12 01:03:11 -04:00
|
|
|
'margin': clusters['torch'].margin
|
|
|
|
}
|
|
|
|
|
|
|
|
# Top wall
|
2014-07-24 19:03:03 -04:00
|
|
|
thang = {
|
|
|
|
'id': @getRandomThang(clusters[room.cluster].thangs)
|
|
|
|
'pos': {
|
|
|
|
'x': i
|
2014-09-01 23:07:50 -04:00
|
|
|
'y': rect.y + rect.height/2 + t * grid
|
2014-07-24 19:03:03 -04:00
|
|
|
}
|
|
|
|
'margin': clusters[room.cluster].margin
|
|
|
|
}
|
|
|
|
if i is topDoorX and topDoor
|
|
|
|
thang.id = @getRandomThang(clusters['doors'].thangs)
|
2014-09-01 23:07:50 -04:00
|
|
|
thang.pos.y -= grid
|
2014-07-24 19:03:03 -04:00
|
|
|
@addThang thang unless i is topDoorX and t isnt roomThickness and topDoor
|
|
|
|
|
|
|
|
for t in _.range(0, roomThickness)
|
2014-09-01 23:07:50 -04:00
|
|
|
for i in _.range(rect.y - rect.height/2 - t * grid, rect.y + rect.height/2 + (t+1) * grid, grid)
|
2014-08-27 15:24:03 -04:00
|
|
|
# Left wall
|
2014-07-24 19:03:03 -04:00
|
|
|
@addThang {
|
|
|
|
'id': @getRandomThang(clusters[room.cluster].thangs)
|
|
|
|
'pos': {
|
2014-09-01 23:07:50 -04:00
|
|
|
'x': rect.x - rect.width/2 - t * grid
|
2014-07-24 19:03:03 -04:00
|
|
|
'y': i
|
|
|
|
}
|
|
|
|
'margin': clusters[room.cluster].margin
|
|
|
|
}
|
|
|
|
|
2014-08-27 15:24:03 -04:00
|
|
|
# Right wall
|
2014-07-24 19:03:03 -04:00
|
|
|
@addThang {
|
|
|
|
'id': @getRandomThang(clusters[room.cluster].thangs)
|
|
|
|
'pos': {
|
2014-09-01 23:07:50 -04:00
|
|
|
'x': rect.x + rect.width/2 + t * grid
|
2014-07-24 19:03:03 -04:00
|
|
|
'y': i
|
|
|
|
}
|
|
|
|
'margin': clusters[room.cluster].margin
|
|
|
|
}
|
|
|
|
|
2014-08-12 01:03:11 -04:00
|
|
|
buildBarrels: (preset, presetSize, decoration) ->
|
|
|
|
rect = {
|
|
|
|
'width':presetSize.sizeFactor * ( _.random( decoration.width[0], decoration.width[1] ) )
|
|
|
|
'height':presetSize.sizeFactor * ( _.random( decoration.height[0], decoration.height[1] ) )
|
|
|
|
}
|
|
|
|
x = [ rect.width/2 + preset.borderSize , presetSize.x - rect.width/2 - preset.borderSize ]
|
|
|
|
y = [ rect.height/2 + preset.borderSize , presetSize.y - rect.height/2 - 2 * preset.borderSize ]
|
|
|
|
|
|
|
|
for i in x
|
|
|
|
for j in y
|
|
|
|
if _.random(100) < 40
|
|
|
|
rect = {
|
|
|
|
'x': i
|
|
|
|
'y': j
|
|
|
|
'width': rect.width
|
|
|
|
'height': rect.height
|
|
|
|
}
|
|
|
|
if @addRect rect
|
|
|
|
for num in _.range( _.random( decoration.numBarrels[0], decoration.numBarrels[1] ) )
|
|
|
|
while not @addThang {
|
|
|
|
'id': @getRandomThang(clusters[decoration.cluster].thangs)
|
|
|
|
'pos': {
|
|
|
|
'x': _.random(rect.x - rect.width/2, rect.x + rect.width/2)
|
|
|
|
'y': _.random(rect.y - rect.height/2, rect.y + rect.height/2)
|
|
|
|
}
|
|
|
|
'margin': clusters[decoration.cluster].margin
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
|
2014-07-20 15:10:35 -04:00
|
|
|
addThang: (thang) ->
|
2014-07-25 02:46:27 -04:00
|
|
|
if @falseCount > 100
|
2014-07-24 19:38:42 -04:00
|
|
|
console.log 'infinite loop', thang
|
|
|
|
@falseCount = 0
|
|
|
|
return true
|
2014-07-20 15:10:35 -04:00
|
|
|
for existingThang in @thangs
|
2014-07-21 09:55:35 -04:00
|
|
|
if existingThang.margin is -1 or thang.margin is -1
|
|
|
|
continue
|
2014-08-06 10:43:56 -04:00
|
|
|
if Math.abs(existingThang.pos.x - thang.pos.x) < thang.margin + existingThang.margin and Math.abs(existingThang.pos.y - thang.pos.y) < thang.margin + existingThang.margin
|
2014-07-24 19:38:42 -04:00
|
|
|
@falseCount++
|
2014-08-06 10:43:56 -04:00
|
|
|
return false
|
2014-07-20 15:10:35 -04:00
|
|
|
@thangs.push thang
|
|
|
|
true
|
2014-07-03 15:34:21 -04:00
|
|
|
|
2014-07-25 02:46:27 -04:00
|
|
|
addRect: (rect) ->
|
|
|
|
if @falseCount > 100
|
|
|
|
console.log 'infinite loop', rect
|
|
|
|
@falseCount = 0
|
|
|
|
return true
|
|
|
|
for existingRect in @rects
|
2014-08-11 21:08:22 -04:00
|
|
|
if Math.abs(existingRect.x - rect.x) <= rect.width/2 + existingRect.width/2 and Math.abs(existingRect.y - rect.y) <= rect.height/2 + existingRect.height/2
|
2014-07-25 02:46:27 -04:00
|
|
|
@falseCount++
|
2014-08-06 10:43:56 -04:00
|
|
|
return false
|
2014-07-25 02:46:27 -04:00
|
|
|
@rects.push rect
|
|
|
|
true
|
|
|
|
|
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
|