From b401b0489378d7b82d96615280e12b6dff0d0e41 Mon Sep 17 00:00:00 2001 From: Jayant Jain Date: Sat, 28 Jun 2014 10:20:55 +0530 Subject: [PATCH] Adds randomiseFloor and randomiseBorder functions --- app/templates/modal/terrain_randomise.jade | 2 +- .../modal/terrain_randomise_modal.coffee | 94 +++++++++++++++++-- 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/app/templates/modal/terrain_randomise.jade b/app/templates/modal/terrain_randomise.jade index 680af187c..799b4312c 100644 --- a/app/templates/modal/terrain_randomise.jade +++ b/app/templates/modal/terrain_randomise.jade @@ -6,7 +6,7 @@ block modal-header-content block modal-body-content div#normal-view a(href="#") - div.choose-option(data-preset-type="grassy", data-preset-size="small") + div.choose-option(data-preset-type="dungeon", data-preset-size="small") div.preset-size.name-label span(data-i18n="ladder.small") Small div.preset-name diff --git a/app/views/modal/terrain_randomise_modal.coffee b/app/views/modal/terrain_randomise_modal.coffee index 0b5f96426..35562ef81 100644 --- a/app/views/modal/terrain_randomise_modal.coffee +++ b/app/views/modal/terrain_randomise_modal.coffee @@ -2,6 +2,34 @@ ModalView = require 'views/kinds/ModalView' template = require 'templates/modal/terrain_randomise' CocoModel = require 'models/CocoModel' +presets = { + 'dungeon': { + 'type':'dungeon' + 'borders':['Dungeon Wall'] + 'floors':['Dungeon Floor'] + 'decorations':[] + } +} + +sizes = { + 'small': { + 'x':80 + 'y':68 + } + 'large': { + 'x':160 + 'y':136 + } + 'floorSize': { + 'x':20 + 'y':20 + } + 'borderSize': { + 'x':4 + 'y':4 + } +} + module.exports = class TerrainRandomiseModal extends ModalView id: 'terrain-randomise-modal' template: template @@ -18,16 +46,68 @@ module.exports = class TerrainRandomiseModal extends ModalView onRandomise: (e) -> @thangs = [] - @thangs.push { - 'id':'Grass01' - 'pos':{ - 'x':20 - 'y':20 - } - } + target = $(e.target) + presetType = target.attr 'data-preset-type' + presetSize = target.attr 'data-preset-size' + @randomiseThangs presetType, presetSize + Backbone.Mediator.publish('randomise:terrain-generated', 'thangs': @thangs ) + + randomiseThangs: (presetName, presetSize) -> + preset = presets[presetName] + presetSize = sizes[presetSize] + @thangs = [] + @randomiseFloor preset, presetSize + @randomiseBorder preset, presetSize + + randomiseFloor: (preset, presetSize) -> + for i in _.range(0, presetSize.x, sizes.floorSize.x) + for j in _.range(0, presetSize.y, sizes.floorSize.y) + @thangs.push { + 'id': @getRandomThang(preset.floors) + 'pos': { + 'x': i + 'y': j + } + } + + randomiseBorder: (preset, presetSize) -> + for i in _.range(0-sizes.floorSize.x/2+sizes.borderSize.x, presetSize.x-sizes.floorSize.x/2, sizes.borderSize.x) + @thangs.push { + 'id': @getRandomThang(preset.borders) + 'pos': { + 'x': i + 'y': 0-sizes.floorSize.x/2 + } + } + @thangs.push { + 'id': @getRandomThang(preset.borders) + 'pos': { + 'x': i + 'y': presetSize.y - sizes.borderSize.y + } + } + + for i in _.range(0-sizes.floorSize.y/2, presetSize.y-sizes.borderSize.y, sizes.borderSize.y) + @thangs.push { + 'id': @getRandomThang(preset.borders) + 'pos': { + 'x': 0-sizes.floorSize.x/2+sizes.borderSize.x + 'y': i + } + } + @thangs.push { + 'id': @getRandomThang(preset.borders) + 'pos': { + 'x': presetSize.x - sizes.borderSize.x - sizes.floorSize.x/2 + 'y': i + } + } + + getRandomThang: (thangList) -> + return thangList[_.random(0, thangList.length-1)] getRenderData: -> c = super()