From e7d5747a7cdb3219de07f1afc5c12a3c45e0353c Mon Sep 17 00:00:00 2001 From: Victor Turgeon Date: Sun, 11 Jun 2023 09:10:21 -0400 Subject: [PATCH] Kinda can put buildings --- buildings/kash_buildings/factory.tscn | 2 +- game/game_manager.cs | 14 ++ levels/test_level.tscn | 2 +- player/player.cs | 198 +++++++++++++++++--------- player/player.tscn | 51 ++++++- project.godot | 6 + 6 files changed, 197 insertions(+), 76 deletions(-) create mode 100644 game/game_manager.cs diff --git a/buildings/kash_buildings/factory.tscn b/buildings/kash_buildings/factory.tscn index 274028a..419839a 100644 --- a/buildings/kash_buildings/factory.tscn +++ b/buildings/kash_buildings/factory.tscn @@ -5,7 +5,7 @@ [ext_resource type="PackedScene" uid="uid://m3lrwoup4i4r" path="res://buildings/grass_block.tscn" id="3_4p2cy"] [node name="factory" type="Node3D" node_paths=PackedStringArray("BlocksNode")] -transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 1.1, 0) +transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 0, 0) script = ExtResource("1_bcr1q") BlocksNode = NodePath("blocks") DisplayName = "Factory" diff --git a/game/game_manager.cs b/game/game_manager.cs new file mode 100644 index 0000000..112913a --- /dev/null +++ b/game/game_manager.cs @@ -0,0 +1,14 @@ +public static class game_manager +{ + public enum State + { + Play, + Building, + Pause, + } + + public static State CurrentState { get; set; } = State.Play; + public static int Kash { get; set; } = 0; + public static int Approval { get; set; } = 0; + public static int Megaton { get; set; } = 0; +} \ No newline at end of file diff --git a/levels/test_level.tscn b/levels/test_level.tscn index 6fc9ccf..9fa348d 100644 --- a/levels/test_level.tscn +++ b/levels/test_level.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=3 format=3 uid="uid://cbs0x36e3ranm"] -[ext_resource type="MeshLibrary" uid="uid://bo7ir5us230sa" path="res://assets/tilesets/tileset.tres" id="1_l447d"] +[ext_resource type="MeshLibrary" uid="uid://dwo1m72yajrb1" path="res://assets/tilesets/tileset.tres" id="1_l447d"] [ext_resource type="PackedScene" uid="uid://b5gb5paiupum" path="res://player/player.tscn" id="2_ojnju"] [node name="test_level" type="Node3D"] diff --git a/player/player.cs b/player/player.cs index e34bcab..a3ee043 100644 --- a/player/player.cs +++ b/player/player.cs @@ -3,76 +3,134 @@ using System; public partial class player : Camera3D { - private const string _path = "/root/player/"; - //private Camera3D _cam; - [Export] - private Label _con; - private bool _mUp, _mDown, _mLeft, _mRight, _wIn, _wOut; - - // Called when the node enters the scene tree for the first time. - public override void _Ready() - { - } + private const string _path = "/root/player/"; + //private Camera3D _cam; + [Export] + private Label _con; + [Export] + private Label _mousePosLabel; + [Export] + private Label _cursorPosLabel; + [Export] + private RayCast3D _cursor; + [Export] + private Node3D _cursorPoint; + private bool _mUp, _mDown, _mLeft, _mRight, _wIn, _wOut; - // Called every frame. 'delta' is the elapsed time since the previous frame. - public override void _Process(double delta) - { - Vector3 rot = this.Rotation; - bool zin = true, zout = true; - - if (Input.IsActionPressed("CamNorth")) - _mUp = true; - if (Input.IsActionPressed("CamSouth")) - _mDown = true; - if (Input.IsActionPressed("CamEast")) - _mRight = true; - if (Input.IsActionPressed("CamWest")) - _mLeft = true; - - this.Rotation -= rot; - if (this.Position.Y < 5) - zin = false; - else if (this.Position.Y > 20) - zout = false; - if (_mLeft && this.Position.X > -30) - this.Translate(new Vector3((float)-delta*10, 0, 0)); - if (_mRight && this.Position.X < 30) - this.Translate(new Vector3((float)delta*10, 0, 0)); - if (_mUp && this.Position.Z > -25) - this.Translate(new Vector3(0, 0, (float)-delta*10)); - if (_mDown && this.Position.Z < 25) - this.Translate(new Vector3(0, 0, (float)delta*10)); - this.Rotation += rot; - - if (_wIn && zin) - this.Translate(new Vector3(0, 0, (float)-delta*10)); - if (_wOut && zout) - this.Translate(new Vector3(0, 0, (float)delta*10)); - - _con.Set("text", this.Position); - } - - public override void _Input(InputEvent @event) - { - _mLeft = _mRight = _mUp = _mDown = _wIn = _wOut = false; - - if (@event is InputEventMouseMotion mouse) { - Vector2 vec = mouse.Position; - if (vec.X < 5) - _mLeft = true; - else if (vec.X > 1915) - _mRight = true; - if (vec.Y < 20) - _mUp = true; - else if (vec.Y > 1060) - _mDown = true; - } - else if (@event is InputEventMouseButton mousebtn) { - if (mousebtn.ButtonIndex == MouseButton.WheelUp) - _wIn = true; - else if (mousebtn.ButtonIndex == MouseButton.WheelDown) - _wOut = true; - } + [Export] + private PackedScene _test_building; - } + public Node3D CurrentBuilding { get; set; } + + + public override void _Ready() + { + //for debugging TODO: Remove + game_manager.CurrentState = game_manager.State.Building; + if (_test_building != null) + { + CurrentBuilding = _test_building.Instantiate(); + GD.Print(CurrentBuilding); + + GetTree().Root.CallDeferred("add_child", CurrentBuilding); + } + } + + public override void _Process(double delta) + { + Vector3 rot = this.Rotation; + bool zin = true, zout = true; + + if (Input.IsActionPressed("CamNorth")) + _mUp = true; + if (Input.IsActionPressed("CamSouth")) + _mDown = true; + if (Input.IsActionPressed("CamEast")) + _mRight = true; + if (Input.IsActionPressed("CamWest")) + _mLeft = true; + + this.Rotation -= rot; + if (this.Position.Y < 5) + zin = false; + else if (this.Position.Y > 20) + zout = false; + if (_mLeft && this.Position.X > -30) + this.Translate(new Vector3((float)-delta * 10, 0, 0)); + if (_mRight && this.Position.X < 30) + this.Translate(new Vector3((float)delta * 10, 0, 0)); + if (_mUp && this.Position.Z > -25) + this.Translate(new Vector3(0, 0, (float)-delta * 10)); + if (_mDown && this.Position.Z < 25) + this.Translate(new Vector3(0, 0, (float)delta * 10)); + this.Rotation += rot; + + if (_wIn && zin) + this.Translate(new Vector3(0, 0, (float)-delta * 10)); + if (_wOut && zout) + this.Translate(new Vector3(0, 0, (float)delta * 10)); + + _con.Set("text", this.Position); + + + if (_cursor != null && _cursorPoint != null) + { + //Get the collision with map and change its X and Z value to always be in the center of a tile + Vector3I collisionVector = (Vector3I)_cursor.GetCollisionPoint(); + _cursorPosLabel.Text = "Virtual Cursor Postion" + collisionVector.ToString(); + _cursorPoint.GlobalPosition = new Vector3(collisionVector.X - collisionVector.X % 2, collisionVector.Y + 0.1f, collisionVector.Z - collisionVector.Z % 2); + + if (game_manager.CurrentState == game_manager.State.Building) + HandleBuilding(); + } + } + + public override void _Input(InputEvent @event) + { + _mLeft = _mRight = _mUp = _mDown = _wIn = _wOut = false; + + if (@event is InputEventMouseMotion mouse) + { + Vector2 vec = mouse.Position; + if (vec.X < 5) + _mLeft = true; + else if (vec.X > 1915) + _mRight = true; + if (vec.Y < 20) + _mUp = true; + else if (vec.Y > 1060) + _mDown = true; + + + _mousePosLabel.Text = "Mouse position: " + vec.ToString(); + + _cursor.Position = new Vector3( + (vec.X - GetViewport().GetVisibleRect().Size.X / 2) * this.Position.Y / 700, + -((vec.Y - GetViewport().GetVisibleRect().Size.Y / 2) * this.Position.Y / 700) + , 0 + ); + } + else if (@event is InputEventMouseButton mousebtn) + { + if (mousebtn.ButtonIndex == MouseButton.WheelUp) + _wIn = true; + else if (mousebtn.ButtonIndex == MouseButton.WheelDown) + _wOut = true; + } + + } + + public void HandleBuilding() + { + if (CurrentBuilding != null) + { + CurrentBuilding.GlobalPosition = _cursorPoint.GlobalPosition; + + if (Input.IsActionJustPressed("build")) + { + Node3D tempBuilding = (Node3D)CurrentBuilding.Duplicate(); + GetTree().Root.CallDeferred("add_child", tempBuilding); + } + } + } } diff --git a/player/player.tscn b/player/player.tscn index 58d1dd2..ed709e2 100644 --- a/player/player.tscn +++ b/player/player.tscn @@ -1,14 +1,57 @@ -[gd_scene load_steps=2 format=3 uid="uid://b5gb5paiupum"] +[gd_scene load_steps=4 format=3 uid="uid://b5gb5paiupum"] [ext_resource type="Script" path="res://player/player.cs" id="1_of2l4"] +[ext_resource type="PackedScene" uid="uid://2pw0p13lcklv" path="res://buildings/kash_buildings/factory.tscn" id="2_mspwe"] -[node name="player" type="Camera3D" node_paths=PackedStringArray("_con")] +[sub_resource type="SphereMesh" id="SphereMesh_w0k5u"] + +[node name="player" type="Camera3D" node_paths=PackedStringArray("_con", "_mousePosLabel", "_cursorPosLabel", "_cursor", "_cursorPoint")] script = ExtResource("1_of2l4") -_con = NodePath("Label") +_con = NodePath("Debug info/Label") +_mousePosLabel = NodePath("Debug info/mousePos") +_cursorPosLabel = NodePath("Debug info/cursorPointPos") +_cursor = NodePath("cursor") +_cursorPoint = NodePath("cursorPoint") +_test_building = ExtResource("2_mspwe") -[node name="Label" type="Label" parent="."] +[node name="Debug info" type="Control" parent="."] +layout_mode = 3 +anchors_preset = 0 +offset_right = 40.0 +offset_bottom = 40.0 + +[node name="mousePos" type="Label" parent="Debug info"] +layout_mode = 0 +offset_left = 314.0 +offset_top = 228.0 +offset_right = 403.0 +offset_bottom = 254.0 +text = "Eat ze caca." + +[node name="cursorPointPos" type="Label" parent="Debug info"] +layout_mode = 0 +offset_left = 314.0 +offset_top = 195.0 +offset_right = 403.0 +offset_bottom = 221.0 +text = "Eat ze caca." + +[node name="Label" type="Label" parent="Debug info"] +layout_mode = 0 offset_left = 313.0 offset_top = 156.0 offset_right = 402.0 offset_bottom = 182.0 text = "Eat ze caca." + +[node name="cursor" type="RayCast3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1.17786) +target_position = Vector3(0, 0, -999) + +[node name="cursorPoint" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2) + +[node name="cursorPointMesh" type="MeshInstance3D" parent="cursorPoint"] +transform = Transform3D(0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0) +mesh = SubResource("SphereMesh_w0k5u") +skeleton = NodePath("../..") diff --git a/project.godot b/project.godot index 09a12bf..67cd3b8 100644 --- a/project.godot +++ b/project.godot @@ -51,3 +51,9 @@ CamEast={ , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"echo":false,"script":null) ] } +build={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"pressed":false,"double_click":false,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":66,"key_label":0,"unicode":98,"echo":false,"script":null) +] +}