Skip to content

Commit

Permalink
Building and channel costs
Browse files Browse the repository at this point in the history
  • Loading branch information
azram19 committed Jun 18, 2012
1 parent 330bc55 commit 385a95c
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 101 deletions.
38 changes: 33 additions & 5 deletions dev/client/Negotiator.coffee
Expand Up @@ -55,12 +55,40 @@ class Negotiator


@on 'build:platform', (x, y, type, owner) =>
@buildPlatform x, y, type, owner
@communicator.trigger 'send:build:platform', x, y, type, owner
p = @ui.getPoint x, y

cost = S.Types.Events.Build.Channel.cost
userHas = @myPlayer.resources

canAfford = _.all cost, ( v, k ) ->
userHas[k] > v

if canAfford
_.each cost, ( v, k ) ->
userHas[k] -= v

@buildPlatform x, y, type, owner
@communicator.trigger 'send:build:platform', x, y, type, owner
else
@ui.showTextBubble "Not enough resources", p.x+40, p.y+20, color: color: [159, 17, 27, 1]

@on 'build:channel', (x, y, k, owner) =>
@buildChannel x, y, k, owner
@communicator.trigger 'send:build:channel', x, y, k, owner
p = @ui.getPoint x, y

cost = S.Types.Events.Build.Channel.cost
userHas = @myPlayer.resources

canAfford = _.all cost, ( v, k ) ->
userHas[k] > v

if canAfford
_.each cost, ( v, k ) ->
userHas[k] -= v

@buildChannel x, y, k, owner
@communicator.trigger 'send:build:channel', x, y, k, owner
else
@ui.showTextBubble "Not enough resources", p.x+40, p.y+20, color: color: [159, 17, 27, 1]

@on 'routing', (obj, routing) =>
_.extend obj.platform.state.routing, routing
Expand Down Expand Up @@ -248,7 +276,7 @@ class Negotiator
@renderer.buildChannel x2, y2, nK, channel
#@terrain.generateRoad x, y, x2, y2
#field = @game.map.getField(x2,y2)
#plOwner = field.platform?.state?.owner.id
#plOwner = field.platform?.state?.owner.id
#for k in [0..5]
# if k isnt nK and field.channels[k]?.state?
# chOwner = field.channels[k]?.state?.owner.id
Expand Down
152 changes: 101 additions & 51 deletions dev/client/renderer/MenuDisplayHelper.coffee
Expand Up @@ -3,79 +3,129 @@ class MenuDisplayHelper
@data = {}

@build =
'platforminfo' : () ->
field = @events.getField @i, @j
platformSt = field.platform.state
'price' :
start: () ->
console.log "[MDH] start - price"

@data.platformName = S.Types.Entities.Names[platformSt.type]
resources = @data

console.log resources

@data.resources = []
@data = _.map resources, ( v, k ) ->
name: k
value: v

if field.resource.state? and field.resource.state.type? and platformSt.type != S.Types.Entities.Platforms.HQ
resource = field.resource.state
res =
size: Math.floor resource.life
extraction: Math.round( (resource.extraction * 1000) / resource.delay )
name: S.Types.Resources.Names[resource.type-6]
console.log @data

@data.resources.push res
else if platformSt.type == S.Types.Entities.Platforms.HQ
res =
size: "Infinity"
extraction: Math.round( (platformSt.extraction * 1000) / platformSt.delay )
name: "Food"
@template = Handlebars.templates.costinfo
@html = $( @template resources: @data )
show: () ->
console.log "[MDH] show - price"

@data.resources.push res
@html.appendTo '#canvasWrapper .scrollIt'

res =
size: "Infinity"
extraction: Math.round( (platformSt.extraction * 1000) / platformSt.delay )
name: "Gold"
width = @html.width()
height = @html.height()

@data.resources.push res
p = @menu.button.localToGlobal 0, 0
x = p.x + 80
y = p.y - height/2

@data.life = platformSt.life
@data.space = platformSt.signals.length
@data.maxSpace = platformSt.capacity
@html.css
position: 'absolute'
top: y
left: x
'z-index': 500

@data.platform = @data
hide: () ->
console.log "[MDH] - hide - price"

@template = Handlebars.templates.platforminfo
@html = $('<div/>')
@html.hide()
@html.remove()

'platforminfo' :
start: () ->
field = @events.getField @i, @j
platformSt = field.platform.state

if @build[@type]?
@build[@type].call @
@data.platformName = S.Types.Entities.Names[platformSt.type]

show: () ->
@html.remove()
@html = $ @template @data

scrollX = @events.ui.scrollX
scrollY = @events.ui.scrollY
@data.resources = []

if field.resource.state? and field.resource.state.type? and platformSt.type != S.Types.Entities.Platforms.HQ
resource = field.resource.state
res =
size: Math.floor resource.life
extraction: Math.round( (resource.extraction * 1000) / resource.delay )
name: S.Types.Resources.Names[resource.type-6]

@data.resources.push res
else if platformSt.type == S.Types.Entities.Platforms.HQ
res =
size: "Infinity"
extraction: Math.round( (platformSt.extraction * 1000) / platformSt.delay )
name: "Food"

@data.resources.push res

res =
size: "Infinity"
extraction: Math.round( (platformSt.extraction * 1000) / platformSt.delay )
name: "Gold"

@data.resources.push res

@data.life = platformSt.life
@data.space = platformSt.signals.length
@data.maxSpace = platformSt.capacity

@html.hide()
@html.appendTo '#canvasWrapper .scrollIt'
@data.platform = @data

@template = Handlebars.templates.platforminfo
@html = $('<div/>')
show: () ->
@html.remove()
@html = $ @template @data

width = @html.width()
height = @html.height()
scrollX = @events.ui.scrollX
scrollY = @events.ui.scrollY

p = @menu.button.parent.localToGlobal @x, @y
x = p.x - width - 60
y = p.y - height/2
@html.hide()
@html.appendTo '#canvasWrapper .scrollIt'

@html.css
position: 'absolute'
top: y
left: x
'z-index': 500

@html.show()
width = @html.width()
height = @html.height()

p = @menu.button.parent.localToGlobal @x, @y
x = p.x - width - 60
y = p.y - height/2

@html.css
position: 'absolute'
top: y
left: x
'z-index': 500

@html.show()
hide: () ->
@html.hide()
@html.remove()

start: () ->
if @build[@type]? and @build[@type].start?
@build[@type].start.call @

setData: ( data ) ->
@data = data

show: () ->
if @build[@type].show?
@build[@type].show.call @

hide: () ->
@html.hide()
@html.remove()
if @build[@type].hide?
@build[@type].hide.call @

window.S.MenuDisplayHelper = MenuDisplayHelper
6 changes: 6 additions & 0 deletions dev/client/renderer/RadialMenu.coffee
Expand Up @@ -146,6 +146,9 @@ class RadialMenu

null

setPrice: ( price ) ->
@price = price

setDisplayHelper: ( displayHelper ) ->
@displayHelper = displayHelper

Expand Down Expand Up @@ -284,6 +287,7 @@ class RadialMenu

$( @canvas ).unbind "contextmenu", ( e ) ->
e.preventDefault()

Ticker.removeListener @

drawButtonOrange: ( button ) ->
Expand Down Expand Up @@ -457,6 +461,8 @@ class RadialMenu
@showing = false
@visible = true
@$title.visible = true
if @displayHelper
@displayHelper.show()
else
@steps--
@x += @stepX
Expand Down
25 changes: 20 additions & 5 deletions dev/client/renderer/UI.coffee
Expand Up @@ -296,17 +296,20 @@ class UI extends S.Drawer
@getWithoutPrefix( submenuName, menuStructure ),
submenuName,
menuStructure,
menuValidFields
menuValidFields,
i,
j,

menu.addChild subMenu
) for submenuName in submenuNames

menu.setActionHelper @menuHelper

if menuSpDisplay?
console.log menuSpDisplay
displayHelper = new S.MenuDisplayHelper( @events, menuSpDisplay, menu, i, j, p.x, p.y )

menu.setDisplayHelper displayHelper
displayHelper.start()

menu.setObj obj
menu.setRoot menu
Expand All @@ -315,24 +318,34 @@ class UI extends S.Drawer

#name of the event element, eventsStructure - Types.Events sub object
#eventsStructure [a:b:c] ...
buildMenu: ( name, eventsStructure, menuStructure, fullname, fullStructure, validFields ) =>
buildMenu: ( name, eventsStructure, menuStructure, fullname, fullStructure, validFields, i, j ) =>
p = @getPoint i, j

stName = name[0].toUpperCase() + name[1..]

title = eventsStructure[stName].title
desc = eventsStructure[stName].desc
price = eventsStructure[stName].cost

console.log name, stName, price, fullname, eventsStructure

title ?= ""
desc ?= ""


eventsStructure = eventsStructure[stName]
submenuNames = @getPrefixes menuStructure

if desc.length > 0 or not submenuNames.length
m = new S.RadialMenu @events, @stage.canvas, 0, 0, title, desc
m.setEvent fullname

if price?
displayHelper = new S.MenuDisplayHelper( @events, 'price', m, i, j, p.x, p.y )
displayHelper.setData price

m.setDisplayHelper displayHelper
displayHelper.start()

index = _.indexOf fullStructure, fullname
m.setValidFields validFields[index]

Expand All @@ -346,7 +359,9 @@ class UI extends S.Drawer
@getWithoutPrefix( submenuName, menuStructure ),
fullname + ':' + submenuName,
fullStructure,
validFields
validFields,
i,
j

m.addChild subMenu
) for submenuName in submenuNames
Expand Down
7 changes: 7 additions & 0 deletions dev/common/config/Types.coffee
Expand Up @@ -56,8 +56,15 @@ Types =
title: 'build'
Channel:
title: 'channel'
cost:
Gold: 100
Food: 40
Platform:
title: 'platform'
cost:
Gold: 400
Food: 300

Games:
FFA:
Number2: 0
Expand Down

0 comments on commit 385a95c

Please sign in to comment.