Skip to content

Commit

Permalink
UI now moves with camera.
Browse files Browse the repository at this point in the history
Added code to facilitate masking of countries. To be used in future to have entire countries flash upon selection.
  • Loading branch information
ra314 committed Aug 10, 2021
1 parent 6315e77 commit f3dc157
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 72 deletions.
1 change: 0 additions & 1 deletion Scenes/Levels/Level Components/Player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[ext_resource path="res://Scripts/Levels/Level Components/Player.gd" type="Script" id=1]


[node name="Player" type="Node2D"]
script = ExtResource( 1 )

Expand Down
1 change: 0 additions & 1 deletion Scenes/Levels/Level Creator.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ script = ExtResource( 1 )
[node name="Camera2D" parent="." instance=ExtResource( 3 )]

[node name="Our World" type="Sprite" parent="."]
visible = false
scale = Vector2( 0.5, 0.5 )
texture = ExtResource( 2 )
centered = false
Expand Down
90 changes: 59 additions & 31 deletions Scenes/Levels/Level Main.tscn
Original file line number Diff line number Diff line change
@@ -1,53 +1,42 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=7 format=2]

[ext_resource path="res://Assets/Our World.png" type="Texture" id=1]
[ext_resource path="res://Scripts/Levels/Level Main.gd" type="Script" id=2]
[ext_resource path="res://Scenes/Levels/Level Components/Drag_Camera.tscn" type="PackedScene" id=3]
[ext_resource path="res://Assets/Crucible.png" type="Texture" id=4]
[ext_resource path="res://Assets/No Mans Land.png" type="Texture" id=5]
[ext_resource path="res://Scenes/Levels/Level Components/Player.tscn" type="PackedScene" id=6]

[node name="Level 1" type="Node2D"]
script = ExtResource( 2 )

[node name="Our World" type="Sprite" parent="."]
scale = Vector2( 0.5, 0.5 )
z_index = -1
texture = ExtResource( 1 )
centered = false
[node name="CanvasLayer" type="CanvasLayer" parent="."]

[node name="Camera2D" parent="." instance=ExtResource( 3 )]
visible = false
[node name="Player Red" parent="CanvasLayer" instance=ExtResource( 6 )]
position = Vector2( 1142.62, 34.7058 )

[node name="Crucible" type="Sprite" parent="."]
visible = false
scale = Vector2( 0.5, 0.5 )
texture = ExtResource( 4 )
centered = false
[node name="Player Blue" parent="CanvasLayer" instance=ExtResource( 6 )]
position = Vector2( 755.299, 31.4596 )

[node name="No Mans Land" type="Sprite" parent="."]
visible = false
scale = Vector2( 0.5, 0.5 )
texture = ExtResource( 5 )
centered = false

[node name="Player and Round Tracker" type="Label" parent="."]
margin_right = 40.0
margin_bottom = 14.0
[node name="End Attack" type="Button" parent="CanvasLayer"]
margin_right = 125.0
margin_bottom = 30.0
text = "End Attack"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Play Red" type="Button" parent="."]
margin_left = 116.211
margin_top = 807.023
margin_right = 183.211
margin_bottom = 837.023
text = "Host plays as Red"
[node name="End Reinforcement" type="Button" parent="CanvasLayer"]
visible = false
margin_left = 125.0
margin_right = 258.0
margin_bottom = 30.0
text = "End Reinforcement"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Reroll Spawn" type="Button" parent="."]
[node name="Reroll Spawn" type="Button" parent="CanvasLayer"]
margin_left = 114.704
margin_top = 935.147
margin_right = 239.704
Expand All @@ -57,7 +46,7 @@ __meta__ = {
"_edit_use_anchors_": false
}

[node name="Start Game" type="Button" parent="."]
[node name="Start Game" type="Button" parent="CanvasLayer"]
margin_left = 113.677
margin_top = 968.514
margin_right = 238.677
Expand All @@ -67,7 +56,7 @@ __meta__ = {
"_edit_use_anchors_": false
}

[node name="Play Blue" type="Button" parent="."]
[node name="Play Blue" type="Button" parent="CanvasLayer"]
margin_left = 114.779
margin_top = 842.823
margin_right = 244.779
Expand All @@ -76,3 +65,42 @@ text = "Host plays as Blue"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Play Red" type="Button" parent="CanvasLayer"]
margin_left = 116.211
margin_top = 807.023
margin_right = 241.211
margin_bottom = 837.023
text = "Host plays as Red"
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Player and Round Tracker" type="Label" parent="CanvasLayer"]
margin_right = 40.0
margin_bottom = 14.0
__meta__ = {
"_edit_use_anchors_": false
}

[node name="Our World" type="Sprite" parent="."]
visible = false
scale = Vector2( 0.5, 0.5 )
z_index = -1
texture = ExtResource( 1 )
centered = false

[node name="Camera2D" parent="." instance=ExtResource( 3 )]
visible = false

[node name="Crucible" type="Sprite" parent="."]
visible = false
scale = Vector2( 0.5, 0.5 )
texture = ExtResource( 4 )
centered = false

[node name="No Mans Land" type="Sprite" parent="."]
visible = false
scale = Vector2( 0.5, 0.5 )
texture = ExtResource( 5 )
centered = false
11 changes: 8 additions & 3 deletions Scripts/Levels/Level Components/Player.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends Node2D
var owned_countries = []
var num_reinforcements = 0
var color = null
var color = ""
var network_id = null

func save():
Expand All @@ -11,9 +11,14 @@ func save():
save_dict["num_reinforcements"] = num_reinforcements
return save_dict

func init(_color, _x, _y):
func reset():
owned_countries = []
num_reinforcements = 0
color = ""
network_id = null

func init(_color):
self.color = _color
position = Vector2(_x, _y)
return self

func get_num_troops():
Expand Down
5 changes: 5 additions & 0 deletions Scripts/Levels/Level Creator.gd
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func change_phase(new_phase):
update_labels()

func _input(event):
var image = get_node("No Mans Land").texture.get_data()
image.lock()
print(image.get_size())
print(Vector2(get_local_mouse_position()[0]*2, get_local_mouse_position()[1]*2))
print(image.get_pixel(get_local_mouse_position()[0]*2, get_local_mouse_position()[1]*2)*256)
if phase == "add countries":
if event.is_pressed() and event.button_index == BUTTON_LEFT:
var coordinate = get_global_mouse_position()
Expand Down
59 changes: 23 additions & 36 deletions Scripts/Levels/Level Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ var reinforced_countries = {}
var round_number = 1
const max_rounds = 10

var end_attack_button = null
var end_reinforcement_button = null
var round_number_label = null

var curr_player = null
Expand All @@ -39,10 +37,8 @@ func load_world(world_str):

func spawn_and_allocate():
# Creating players
players = {"red": Player.instance().init("red", 200, 0), "blue": Player.instance().init("blue", 400, 0)}
for player in players.values():
add_child(player)

players = {"red": get_node("CanvasLayer/Player Red").init("red"), "blue": get_node("CanvasLayer/Player Blue").init("blue")}

# Randomizing players
randomize()
curr_player_index = randi() % num_players
Expand Down Expand Up @@ -73,44 +69,35 @@ func _ready():
spawn_and_allocate()

# Button to end attack
end_attack_button = Button.new()
end_attack_button.text = "End Attack Phase"
end_attack_button.connect("pressed", self, "change_to_reinforcement")
add_child(end_attack_button)
get_node("CanvasLayer/End Attack").connect("pressed", self, "change_to_reinforcement")

# Button to end reinforcement
end_reinforcement_button = Button.new()
end_reinforcement_button.text = "End Reinforcement Phase"
end_reinforcement_button.connect("pressed", self, "change_to_attack")
add_child(end_reinforcement_button)
end_reinforcement_button.visible = false
end_reinforcement_button.set_position(Vector2(0, 20))
get_node("CanvasLayer/End Reinforcement").connect("pressed", self, "change_to_attack")

# Buttons to select if host plays as red or blue
get_node("Play Red").connect("button_down", self, "set_host_color", ["red"])
get_node("Play Blue").connect("button_down", self, "set_host_color", ["blue"])
get_node("CanvasLayer/Play Red").connect("button_down", self, "set_host_color", ["red"])
get_node("CanvasLayer/Play Blue").connect("button_down", self, "set_host_color", ["blue"])
if not _root.online_game:
remove_color_select_buttons()

# Button to reroll the troop allocation to the countries
get_node("Reroll Spawn").connect("button_down", self, "reroll_spawn")
get_node("CanvasLayer/Reroll Spawn").connect("button_down", self, "reroll_spawn")
if _root.online_game:
get_node("Start Game").queue_free()
get_node("CanvasLayer/Start Game").queue_free()
else:
get_node("Start Game").connect("button_down", self, "hide_reroll_and_start_butttons")
get_node("CanvasLayer/Start Game").connect("button_down", self, "hide_reroll_and_start_butttons")

# Label keeping track of current player and round number
get_node("Player and Round Tracker").set_position(Vector2(get_viewport().size.x/2, 0))
get_node("CanvasLayer/Player and Round Tracker").set_position(Vector2(get_viewport().size.x/2, 0))

func hide_reroll_and_start_butttons():
remove_reroll_spawn_button()
get_node("Start Game").queue_free()
get_node("CanvasLayer/Start Game").queue_free()

# Clear the player dictionary, rerandomise troop allocation and redo player turn order and country allocation
func reroll_spawn():
for player in players.values():
player.queue_free()
players.clear()
player.reset()
for country in all_countries.values():
country.change_ownership_to(null)
country.randomise_troops()
Expand Down Expand Up @@ -151,12 +138,12 @@ func set_host_color(color):
rpc_id(players[other_color].network_id, "synchronise_players_and_round", curr_player_index, round_number, player_info)

remotesync func remove_reroll_spawn_button():
get_node("Reroll Spawn").queue_free()
get_node("CanvasLayer/Reroll Spawn").queue_free()

# Hiding the buttons
remotesync func remove_color_select_buttons():
get_node("Play Red").queue_free()
get_node("Play Blue").queue_free()
get_node("CanvasLayer/Play Red").queue_free()
get_node("CanvasLayer/Play Blue").queue_free()

func select_random(array):
var rng = RandomNumberGenerator.new()
Expand Down Expand Up @@ -203,8 +190,8 @@ func change_to_reinforcement():
curr_level.stop_flashing()
curr_player.give_reinforcements()

end_attack_button.visible = false
end_reinforcement_button.visible = true
get_node("CanvasLayer/End Attack").visible = false
get_node("CanvasLayer/End Reinforcement").visible = true
phase = "reinforcement"

func change_to_attack():
Expand All @@ -220,8 +207,8 @@ func change_to_attack():
round_number += 1
update_labels()

end_attack_button.visible = true
end_reinforcement_button.visible = false
get_node("CanvasLayer/End Attack").visible = true
get_node("CanvasLayer/End Reinforcement").visible = false
phase = "attack"

func get_player_with_most_troops():
Expand All @@ -234,13 +221,13 @@ func get_player_with_most_troops():
return player_with_most_troops

func end_game():
end_attack_button.visible = false
end_reinforcement_button.visible = false
get_node("CanvasLayer/End Attack").visible = false
get_node("CanvasLayer/End Reinforcement").visible = false
phase = "game over"
get_node("Player and Round Tracker").text = get_player_with_most_troops().color + " Wins"
get_node("CanvasLayer/Player and Round Tracker").text = get_player_with_most_troops().color + " Wins"

func update_labels():
get_node("Player and Round Tracker").text = "Player: " + curr_player.color + "\nRound: " + str(round_number)
get_node("CanvasLayer/Player and Round Tracker").text = "Player: " + curr_player.color + "\nRound: " + str(round_number)

# Network synchronisation
remote func synchronise_country(country_name, num_troops, color):
Expand Down

0 comments on commit f3dc157

Please sign in to comment.