|
|
|
class_name Game
|
|
|
|
extends Node2D
|
|
|
|
|
|
|
|
var countries = [
|
|
|
|
{ color = Color8(0, 0, 0, 0), name = "Transparent"},
|
|
|
|
{ color = Color8(128, 0, 0), name = "Red"},
|
|
|
|
{ color = Color8(128,128, 0), name = "Yellow"},
|
|
|
|
{ color = Color8( 0,128, 0), name = "Green"},
|
|
|
|
{ color = Color8( 0,128,128), name = "Cyan"},
|
|
|
|
{ color = Color8( 0, 0,128), name = "Blue"},
|
|
|
|
{ color = Color8(128, 0,128), name = "Purple"},
|
|
|
|
{ color = Color8(128,128,128), name = "White"}
|
|
|
|
]
|
|
|
|
|
|
|
|
var players = []
|
|
|
|
|
|
|
|
var turn_id = -1
|
|
|
|
var turns = []
|
|
|
|
var turn
|
|
|
|
|
|
|
|
var player_count = 3
|
|
|
|
var button_selected = null
|
|
|
|
var mouse_click_mode = null
|
|
|
|
|
|
|
|
var stocks_ui = []
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
randomize()
|
|
|
|
_setup_players()
|
|
|
|
_setup_countries()
|
|
|
|
_setup_turns()
|
|
|
|
_setup_stock_ui()
|
|
|
|
|
|
|
|
|
|
|
|
$Grid.countries = countries
|
|
|
|
$Grid.setup()
|
|
|
|
|
|
|
|
turn = turns[turn_id]
|
|
|
|
_on_EndTurnButton_pressed()
|
|
|
|
|
|
|
|
func _setup_players():
|
|
|
|
var id = 0
|
|
|
|
for player_id in player_count:
|
|
|
|
var player = Player.new()
|
|
|
|
player.id = player_id
|
|
|
|
player.name = str(player_id)
|
|
|
|
players.append(player)
|
|
|
|
|
|
|
|
player.connect("win", self, "_on_player_win", [player])
|
|
|
|
|
|
|
|
func _on_player_win(player):
|
|
|
|
$CanvasLayer/Control/Winner.show()
|
|
|
|
$CanvasLayer/Control/Winner.text = "Player " + player.name + " wins!!!"
|
|
|
|
|
|
|
|
func _setup_countries():
|
|
|
|
var id = 0
|
|
|
|
var countries2 = []
|
|
|
|
for country in countries:
|
|
|
|
var country2 = Country.new()
|
|
|
|
country2.color = country.color
|
|
|
|
country2.id = id
|
|
|
|
country2.name = country.name
|
|
|
|
country2.grid = $Grid
|
|
|
|
countries2.append(country2)
|
|
|
|
id += 1
|
|
|
|
countries = countries2
|
|
|
|
|
|
|
|
|
|
|
|
func _setup_turns():
|
|
|
|
for player in players:
|
|
|
|
turns.append({
|
|
|
|
layer = "player",
|
|
|
|
id = player.id,
|
|
|
|
object = player
|
|
|
|
})
|
|
|
|
for country in countries:
|
|
|
|
if country.id == 0: continue
|
|
|
|
turns.append({
|
|
|
|
layer = "country",
|
|
|
|
id = country.id,
|
|
|
|
object = country
|
|
|
|
})
|
|
|
|
|
|
|
|
func _setup_stock_ui():
|
|
|
|
stocks_ui.append({})
|
|
|
|
for country in countries:
|
|
|
|
if country.id == 0: continue
|
|
|
|
var name_lbl = Label.new()
|
|
|
|
name_lbl.text = country.name
|
|
|
|
var stock_price_lbl = Label.new()
|
|
|
|
stock_price_lbl.text = str(country.stock_price)
|
|
|
|
var stock_lbl = Label.new()
|
|
|
|
stock_lbl.text = "??"
|
|
|
|
var buy_btn = Button.new()
|
|
|
|
buy_btn.text = "Buy"
|
|
|
|
var sell_btn = Button.new()
|
|
|
|
sell_btn.text = "Sell"
|
|
|
|
var proposal_lbl = Label.new()
|
|
|
|
proposal_lbl.text = "0$"
|
|
|
|
var acceptance_btn = Button.new()
|
|
|
|
acceptance_btn.text = "Accept"
|
|
|
|
acceptance_btn.toggle_mode = true
|
|
|
|
acceptance_btn.connect("toggled", self, "_on_acceptance_btn_toggled", [country])
|
|
|
|
|
|
|
|
stocks_ui.append({
|
|
|
|
stock_price_lbl = stock_price_lbl,
|
|
|
|
stock_lbl = stock_lbl,
|
|
|
|
proposal_lbl = proposal_lbl,
|
|
|
|
acceptance_btn = acceptance_btn,
|
|
|
|
})
|
|
|
|
|
|
|
|
buy_btn.connect("pressed", self, "_on_stock_buy_button_pressed", [country])
|
|
|
|
sell_btn.connect("pressed", self, "_on_stock_sell_button_pressed", [country])
|
|
|
|
|
|
|
|
$CanvasLayer/Control/VBoxContainer/Stocks.add_child(name_lbl)
|
|
|
|
$CanvasLayer/Control/VBoxContainer/Stocks.add_child(stock_price_lbl)
|
|
|
|
$CanvasLayer/Control/VBoxContainer/Stocks.add_child(stock_lbl)
|
|
|
|
$CanvasLayer/Control/VBoxContainer/Stocks.add_child(buy_btn)
|
|
|
|
$CanvasLayer/Control/VBoxContainer/Stocks.add_child(sell_btn)
|
|
|
|
$CanvasLayer/Control/VBoxContainer/Stocks.add_child(proposal_lbl)
|
|
|
|
$CanvasLayer/Control/VBoxContainer/Stocks.add_child(acceptance_btn)
|
|
|
|
|
|
|
|
func _refresh_stock_ui():
|
|
|
|
for country in countries:
|
|
|
|
if country.id == 0: continue
|
|
|
|
var ui = stocks_ui[country.id]
|
|
|
|
ui.stock_price_lbl.text = str(country.stock_price)
|
|
|
|
ui.stock_lbl.text = str(turn.object.stocks[country.id])
|
|
|
|
ui.proposal_lbl.text = str(country.proposal) + "$"
|
|
|
|
ui.acceptance_btn.pressed = country.player_acceptance[turn.object.id]
|
|
|
|
|
|
|
|
func set_proposal_display(cur_btn):
|
|
|
|
cur_btn.pressed = true
|
|
|
|
for button in $CanvasLayer/Control/VBoxContainer/CountryProposal.get_children():
|
|
|
|
if button is Button and button != cur_btn:
|
|
|
|
button.pressed = false
|
|
|
|
|
|
|
|
func _on_ProposalMinus1_pressed():
|
|
|
|
if turn.object.stock_price > 1:
|
|
|
|
set_proposal_display($CanvasLayer/Control/VBoxContainer/CountryProposal/ProposalMinus1)
|
|
|
|
turn.object.proposal = -1
|
|
|
|
else:
|
|
|
|
$CanvasLayer/Control/VBoxContainer/CountryProposal/ProposalMinus1.pressed = false
|
|
|
|
|
|
|
|
func _on_ProposalZero_pressed():
|
|
|
|
set_proposal_display($CanvasLayer/Control/VBoxContainer/CountryProposal/ProposalZero)
|
|
|
|
turn.object.proposal = 0
|
|
|
|
|
|
|
|
func _on_ProposalPlus1_pressed():
|
|
|
|
set_proposal_display($CanvasLayer/Control/VBoxContainer/CountryProposal/ProposalPlus1)
|
|
|
|
turn.object.proposal = 1
|
|
|
|
|
|
|
|
func _on_acceptance_btn_toggled(value, country):
|
|
|
|
country.player_acceptance[turn.object.id] = value
|
|
|
|
country.process_proposal()
|
|
|
|
_refresh_stock_ui()
|
|
|
|
|
|
|
|
func _on_stock_buy_button_pressed(country):
|
|
|
|
if turn.object.money >= country.stock_price and country.total_stocks < 7:
|
|
|
|
country.total_stocks += 1
|
|
|
|
country.player_stocks[turn.object.id] += 1
|
|
|
|
turn.object.stocks[country.id] += 1
|
|
|
|
turn.object.money -= country.stock_price
|
|
|
|
country.money += country.stock_price
|
|
|
|
_refresh_stock_ui()
|
|
|
|
|
|
|
|
func _on_stock_sell_button_pressed(country):
|
|
|
|
if country.money >= country.stock_price and turn.object.stocks[country.id] > 0:
|
|
|
|
country.total_stocks -= 1
|
|
|
|
country.player_stocks[turn.object.id] -= 1
|
|
|
|
turn.object.stocks[country.id] -= 1
|
|
|
|
turn.object.money += country.stock_price
|
|
|
|
country.money -= country.stock_price
|
|
|
|
_refresh_stock_ui()
|
|
|
|
|
|
|
|
func _on_PayDividends_pressed():
|
|
|
|
if turn.object.money >= 7:
|
|
|
|
turn.object.money -= 7
|
|
|
|
for i in len(turn.object.player_stocks):
|
|
|
|
var stocks = turn.object.player_stocks[i]
|
|
|
|
players[i].money += players[i].stocks[turn.object.id]
|
|
|
|
|
|
|
|
func _process(delta):
|
|
|
|
$CanvasLayer/Control/VBoxContainer/CountryMoneyLabel.text = str(turns[turn_id].object.money) + "$"
|
|
|
|
if turn.layer == "country":
|
|
|
|
$CanvasLayer/Control/VBoxContainer/StockPriceLabel.text = "STOCK PRICE: " + str(turn.object.stock_price) + "$"
|
|
|
|
$CanvasLayer/Control/VBoxContainer/StockPriceLabel.visible = turn.layer == "country"
|
|
|
|
$CanvasLayer/Control/VBoxContainer/BuyFactory.visible = turn.layer == "country"
|
|
|
|
$CanvasLayer/Control/VBoxContainer/BuyArmy.visible = turn.layer == "country"
|
|
|
|
$CanvasLayer/Control/VBoxContainer/BuyBoat.visible = turn.layer == "country"
|
|
|
|
$CanvasLayer/Control/VBoxContainer/Stocks.visible = turn.layer == "player"
|
|
|
|
$CanvasLayer/Control/VBoxContainer/PayDividends.visible = turn.layer == "country"
|
|
|
|
$CanvasLayer/Control/VBoxContainer/CountryProposal.visible = turn.layer == "country"
|
|
|
|
|
|
|
|
|
|
|
|
func _on_EndTurnButton_pressed():
|
|
|
|
if turn_id != -1:
|
|
|
|
if turn.layer == "country":
|
|
|
|
for army in turn.object.armies:
|
|
|
|
army.entity.current_turn_movable = true
|
|
|
|
for boat in turn.object.boats:
|
|
|
|
boat.entity.current_turn_movable = true
|
|
|
|
turn.object.compute_balance()
|
|
|
|
$Grid.selected_thing = null
|
|
|
|
|
|
|
|
turn_id += 1
|
|
|
|
if turn_id == len(turns): turn_id = 0
|
|
|
|
turn = turns[turn_id]
|
|
|
|
|
|
|
|
if turn.layer == "player":
|
|
|
|
_refresh_stock_ui()
|
|
|
|
else:
|
|
|
|
$CanvasLayer/Control/ControllerLabel.text = "Controller: " + (players[turn.object.get_controller_id()].name if turn.object.get_controller_id() >= 0 else "none")
|
|
|
|
var proposal_btns = [
|
|
|
|
$CanvasLayer/Control/VBoxContainer/CountryProposal/ProposalMinus1,
|
|
|
|
$CanvasLayer/Control/VBoxContainer/CountryProposal/ProposalZero,
|
|
|
|
$CanvasLayer/Control/VBoxContainer/CountryProposal/ProposalPlus1
|
|
|
|
]
|
|
|
|
set_proposal_display(proposal_btns[turn.object.proposal + 1])
|
|
|
|
$CanvasLayer/Control/ControllerLabel.visible = turn.layer == "country"
|
|
|
|
$CanvasLayer/Control/VBoxContainer/TurnLabel.text = turn.object.name + "'s turn."
|
|
|
|
|
|
|
|
if turn.layer == "country":
|
|
|
|
if turn.object.get_controller_id() == -1:
|
|
|
|
_on_EndTurnButton_pressed()
|
|
|
|
|
|
|
|
|
|
|
|
func _on_BuyFactory_pressed():
|
|
|
|
if button_selected: button_selected.pressed = false
|
|
|
|
button_selected = $CanvasLayer/Control/VBoxContainer/BuyFactory
|
|
|
|
mouse_click_mode = "buy_factory"
|
|
|
|
button_selected.pressed = true
|
|
|
|
|
|
|
|
func _on_BuyArmy_pressed():
|
|
|
|
if button_selected: button_selected.pressed = false
|
|
|
|
button_selected = $CanvasLayer/Control/VBoxContainer/BuyArmy
|
|
|
|
mouse_click_mode = "buy_army"
|
|
|
|
button_selected.pressed = true
|
|
|
|
|
|
|
|
func _on_BuyBoat_pressed():
|
|
|
|
if button_selected: button_selected.pressed = false
|
|
|
|
button_selected = $CanvasLayer/Control/VBoxContainer/BuyBoat
|
|
|
|
mouse_click_mode = "buy_boat"
|
|
|
|
button_selected.pressed = true
|
|
|
|
|
|
|
|
|
|
|
|
func _on_Grid_tile_pressed(v):
|
|
|
|
if turn.layer == "country":
|
|
|
|
var country = turn.object
|
|
|
|
var tile = $Grid.grid[v]
|
|
|
|
match mouse_click_mode:
|
|
|
|
"buy_factory":
|
|
|
|
if country.money >= 5 and tile.country == country and not tile.needs_boat and not tile.factory:
|
|
|
|
country.money -= 5
|
|
|
|
$Grid.set_factory(v, true)
|
|
|
|
"buy_army":
|
|
|
|
if country.money >= 3 and tile.country == country and tile.factory and len(tile.units) == 0:
|
|
|
|
country.money -= 3
|
|
|
|
country.add_unit({}, v)
|
|
|
|
"buy_boat":
|
|
|
|
if country.money >= 3 and $Grid.has_adjacent_factory(v, country) and tile.needs_boat and not $Grid.tile_have_boat_of_country(tile, country):
|
|
|
|
country.money -= 3
|
|
|
|
country.add_boat({}, v)
|
|
|
|
null:
|
|
|
|
if is_instance_valid($Grid.selected_thing) and $Grid.selected_thing is Boat and $Grid.selected_thing.get_tile_position() == v or len(tile.boats) == 0:
|
|
|
|
for army in tile.units:
|
|
|
|
if army.country == country:
|
|
|
|
$Grid.selected_thing = army.entity
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
for boat in tile.boats:
|
|
|
|
if boat.country == country:
|
|
|
|
$Grid.selected_thing = boat.entity
|
|
|
|
break
|
|
|
|
|
|
|
|
func _input(event):
|
|
|
|
if event is InputEventMouseButton:
|
|
|
|
if event.pressed and event.button_index == BUTTON_RIGHT:
|
|
|
|
if button_selected:
|
|
|
|
button_selected.pressed = false
|
|
|
|
mouse_click_mode = null
|
|
|
|
|
|
|
|
|
|
|
|
|