From 8a4096970323e57375ca6f7223a922581e8b11b3 Mon Sep 17 00:00:00 2001 From: Anthony Foxclaw Date: Thu, 11 Jun 2020 00:32:25 -0400 Subject: [PATCH] Merged varies scenes into game scene - Merged GUI and world scenes into game scene with the GUI existing on a canvas layer - Ported over camera movement script --- scenes/game.tscn | 104 +++++++++++++++++++++++++++++++++++--- scenes/gui.tscn | 79 ----------------------------- scenes/world.tscn | 20 -------- scripts/cameramovement.gd | 65 ++++++++++++++++++++++++ scripts/gpanel.gd | 4 +- scripts/world.gd | 12 ++--- 6 files changed, 171 insertions(+), 113 deletions(-) delete mode 100644 scenes/gui.tscn delete mode 100644 scenes/world.tscn create mode 100644 scripts/cameramovement.gd diff --git a/scenes/game.tscn b/scenes/game.tscn index 12b2c8e..5c7a9d0 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -1,12 +1,104 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=6 format=2] -[ext_resource path="res://scenes/world.tscn" type="PackedScene" id=1] -[ext_resource path="res://scenes/gui.tscn" type="PackedScene" id=2] +[ext_resource path="res://sprites/water.tres" type="TileSet" id=1] +[ext_resource path="res://scripts/gpanel.gd" type="Script" id=2] +[ext_resource path="res://sprites/terrian.tres" type="TileSet" id=3] +[ext_resource path="res://scripts/world.gd" type="Script" id=4] +[ext_resource path="res://scripts/cameramovement.gd" type="Script" id=5] +[node name="game" type="Node"] +[node name="world" type="Node2D" parent="."] +script = ExtResource( 4 ) -[node name="Game" type="Node"] +[node name="water" type="TileMap" parent="world"] +tile_set = ExtResource( 1 ) +cell_size = Vector2( 16, 16 ) +format = 1 -[node name="World" parent="." instance=ExtResource( 1 )] +[node name="terrian" type="TileMap" parent="world"] +tile_set = ExtResource( 3 ) +cell_size = Vector2( 16, 16 ) +format = 1 -[node name="GUI" parent="." instance=ExtResource( 2 )] +[node name="Camera2D" type="Camera2D" parent="world"] +position = Vector2( 503.506, 302.104 ) +current = true +script = ExtResource( 5 ) + +[node name="ui" type="CanvasLayer" parent="."] + +[node name="base" type="Control" parent="ui"] +anchor_right = 1.0 +anchor_bottom = 1.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="gpanel" type="Panel" parent="ui/base"] +anchor_right = 1.0 +margin_bottom = 23.0 +script = ExtResource( 2 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="citymenus" type="HBoxContainer" parent="ui/base/gpanel"] +anchor_top = 0.5 +anchor_bottom = 0.5 +margin_left = 3.0 +margin_top = -10.0 +margin_right = 166.0 +margin_bottom = 10.0 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="citynamelbl" type="Label" parent="ui/base/gpanel/citymenus"] +margin_top = 3.0 +margin_right = 66.0 +margin_bottom = 17.0 +text = "City Name" + +[node name="gamespeed" type="MenuButton" parent="ui/base/gpanel/citymenus"] +margin_left = 70.0 +margin_right = 163.0 +margin_bottom = 20.0 +text = "Game Speed" +items = [ "Slow", null, 0, false, false, 0, 0, null, "", false, "Medium", null, 0, false, false, 1, 0, null, "", false, "Fast", null, 0, false, false, 2, 0, null, "", false ] + +[node name="citystatus" type="HBoxContainer" parent="ui/base/gpanel"] +anchor_left = 1.0 +anchor_top = 0.5 +anchor_right = 1.0 +anchor_bottom = 0.5 +margin_left = -276.0 +margin_top = -9.5 +margin_right = -4.0 +margin_bottom = 10.5 +alignment = 2 +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="currencylbl" type="Label" parent="ui/base/gpanel/citystatus"] +margin_left = 251.0 +margin_top = 3.0 +margin_right = 259.0 +margin_bottom = 17.0 +text = "$" + +[node name="moneylbl" type="Label" parent="ui/base/gpanel/citystatus"] +margin_left = 263.0 +margin_top = 3.0 +margin_right = 272.0 +margin_bottom = 17.0 +text = "#" + +[node name="debugbtn" type="Button" parent="ui/base/gpanel"] +visible = false +margin_left = 932.754 +margin_top = 563.202 +margin_right = 985.754 +margin_bottom = 583.202 +text = "Debug" diff --git a/scenes/gui.tscn b/scenes/gui.tscn deleted file mode 100644 index adb3b41..0000000 --- a/scenes/gui.tscn +++ /dev/null @@ -1,79 +0,0 @@ -[gd_scene load_steps=2 format=2] - -[ext_resource path="res://scripts/gpanel.gd" type="Script" id=2] - - -[node name="GUI" type="Control"] -anchor_right = 1.0 -anchor_bottom = 1.0 -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="GPanel" type="Panel" parent="."] -anchor_right = 1.0 -margin_bottom = 23.0 -script = ExtResource( 2 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="CityMenus" type="HBoxContainer" parent="GPanel"] -anchor_top = 0.5 -anchor_bottom = 0.5 -margin_left = 3.0 -margin_top = -10.0 -margin_right = 166.0 -margin_bottom = 10.0 -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="CityNameLbl" type="Label" parent="GPanel/CityMenus"] -margin_top = 3.0 -margin_right = 66.0 -margin_bottom = 17.0 -text = "City Name" - -[node name="GameSpeed" type="MenuButton" parent="GPanel/CityMenus"] -margin_left = 70.0 -margin_right = 163.0 -margin_bottom = 20.0 -text = "Game Speed" -items = [ "Slow", null, 0, false, false, 0, 0, null, "", false, "Medium", null, 0, false, false, 1, 0, null, "", false, "Fast", null, 0, false, false, 2, 0, null, "", false ] - -[node name="CityStatus" type="HBoxContainer" parent="GPanel"] -anchor_left = 1.0 -anchor_top = 0.5 -anchor_right = 1.0 -anchor_bottom = 0.5 -margin_left = -276.0 -margin_top = -9.5 -margin_right = -4.0 -margin_bottom = 10.5 -alignment = 2 -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="CurrencyLbl" type="Label" parent="GPanel/CityStatus"] -margin_left = 251.0 -margin_top = 3.0 -margin_right = 259.0 -margin_bottom = 17.0 -text = "$" - -[node name="MoneyLbl" type="Label" parent="GPanel/CityStatus"] -margin_left = 263.0 -margin_top = 3.0 -margin_right = 272.0 -margin_bottom = 17.0 -text = "#" - -[node name="DebugBtn" type="Button" parent="GPanel"] -visible = false -margin_left = 932.754 -margin_top = 563.202 -margin_right = 985.754 -margin_bottom = 583.202 -text = "Debug" diff --git a/scenes/world.tscn b/scenes/world.tscn deleted file mode 100644 index 9b3d4f1..0000000 --- a/scenes/world.tscn +++ /dev/null @@ -1,20 +0,0 @@ -[gd_scene load_steps=4 format=2] - -[ext_resource path="res://sprites/terrian.tres" type="TileSet" id=1] -[ext_resource path="res://scripts/world.gd" type="Script" id=3] -[ext_resource path="res://sprites/water.tres" type="TileSet" id=4] - - - -[node name="World" type="Node"] -script = ExtResource( 3 ) - -[node name="Water" type="TileMap" parent="."] -tile_set = ExtResource( 4 ) -cell_size = Vector2( 16, 16 ) -format = 1 - -[node name="Terrian" type="TileMap" parent="."] -tile_set = ExtResource( 1 ) -cell_size = Vector2( 16, 16 ) -format = 1 diff --git a/scripts/cameramovement.gd b/scripts/cameramovement.gd new file mode 100644 index 0000000..42df9ad --- /dev/null +++ b/scripts/cameramovement.gd @@ -0,0 +1,65 @@ +extends Camera2D + +export var panSpeed = 10.0 +export var speed = 25.0 +export var zoomspeed = 50.0 +export var zoommargin = 0.3 + +export var zoomMin = 0.5 +export var zoomMax = 3.0 +export var marginX = 200.0 +export var marginY = 100.0 + +var mousepos = Vector2() +var zoompos = Vector2() +var zoomfactor = 1.0 + + +func _ready(): + pass + + +func _process(delta): + # Smooth Movement + var inputx = (int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))) + var inputy = (int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))) + position.x = lerp(position.x, position.x + inputx * speed * zoom.x, speed * delta) + position.y = lerp(position.y, position.y + inputy * speed * zoom.y, speed * delta) + + # Edge scrolling via. Ctrl + Right Click + if Input.is_key_pressed(KEY_CONTROL): + # check mouse postion + if mousepos.x < marginX: + position.x = lerp(position.x, position.x - abs(mousepos.x - marginX) / marginX * panSpeed * zoom.x, panSpeed * delta) + elif mousepos.x > OS.window_size.x - marginX: + position.x = lerp(position.x, position.x + abs(mousepos.x - OS.window_size.x + marginX) / marginX * panSpeed * zoom.x, panSpeed * delta) + + if mousepos.y < marginY: + position.y = lerp(position.y, position.y - abs(mousepos.y - marginY) / marginY * panSpeed * zoom.y, panSpeed * delta) + elif mousepos.y > OS.window_size.y - marginY: + position.y = lerp(position.y, position.y + abs(mousepos.y - OS.window_size.y + marginX) / marginX * panSpeed * zoom.y, panSpeed * delta) + + # Zooming + zoom.x = lerp(zoom.x, zoom.x * zoomfactor, zoomspeed * delta) + zoom.y = lerp(zoom.y, zoom.y * zoomfactor, zoomspeed * delta) + + zoom.x = clamp(zoom.x, zoomMin, zoomMax) + zoom.y = clamp(zoom.y, zoomMin, zoomMax) + +func _input(event): + if abs(zoompos.x - get_global_mouse_position().x) > zoommargin: + zoomfactor = 1.0 + if abs(zoompos.y - get_global_mouse_position().y) > zoommargin: + zoomfactor = 1.0 + + if event is InputEventMouseButton: + if event.is_pressed(): + if event.button_index == BUTTON_WHEEL_UP: + zoomfactor -= 0.01 + zoompos = get_global_mouse_position() + if event.button_index == BUTTON_WHEEL_DOWN: + zoomfactor += 0.01 + zoompos = get_global_mouse_position() + + if event is InputEventMouse: + mousepos = event.position diff --git a/scripts/gpanel.gd b/scripts/gpanel.gd index 7b5b2ed..e3f942f 100644 --- a/scripts/gpanel.gd +++ b/scripts/gpanel.gd @@ -1,7 +1,7 @@ extends Panel func _ready(): - $CityMenus/CityNameLbl.text = CityData.city_name + $citymenus/citynamelbl.text = CityData.city_name func _process(delta): - $CityStatus/MoneyLbl.text = str(CityData.budget) + $citystatus/moneylbl.text = str(CityData.budget) diff --git a/scripts/world.gd b/scripts/world.gd index 4dbbafc..46d536a 100644 --- a/scripts/world.gd +++ b/scripts/world.gd @@ -1,4 +1,4 @@ -extends Node +extends Node2D var noise: OpenSimplexNoise var map_size = Vector2(80, 60) @@ -20,14 +20,14 @@ func make_terrian_map(): for y in map_size.y: var a = noise.get_noise_2d(x, y) if a < terrian_cap: - $Terrian.set_cell(x, y, 0) + $terrian.set_cell(x, y, 0) - $Terrian.update_bitmask_region(Vector2(0.0, 0.0), Vector2(map_size.x, map_size.y)) + $terrian.update_bitmask_region(Vector2(0.0, 0.0), Vector2(map_size.x, map_size.y)) func make_water(): for x in map_size.x: for y in map_size.y: - if $Terrian.get_cell(x, y): - $Water.set_cell(x, y, 0) + if $terrian.get_cell(x, y): + $water.set_cell(x, y, 0) - $Water.update_bitmask_region(Vector2(0.0, 0.0), Vector2(map_size.x, map_size.y)) + $water.update_bitmask_region(Vector2(0.0, 0.0), Vector2(map_size.x, map_size.y))