From e4d130f581778b2c89f7171541614a87050b5d39 Mon Sep 17 00:00:00 2001 From: Lukas Moungos Date: Sat, 10 Dec 2022 07:15:02 +0100 Subject: [PATCH] start MapNodes --- Nodes/game.tscn | 88 ++++++++------------------------ project.godot | 24 +++++++++ scripts/Logic/Game.gd | 35 ++++--------- scripts/Logic/MapNode.gd | 14 +++++ scripts/Logic/StraightMapNode.gd | 8 +++ scripts/Logic/TurnMapNode.gd | 18 +++++++ scripts/Modules/Cannon.gd | 14 ++--- scripts/Train.gd | 30 ++++++++--- 8 files changed, 126 insertions(+), 105 deletions(-) create mode 100644 scripts/Logic/MapNode.gd create mode 100644 scripts/Logic/StraightMapNode.gd create mode 100644 scripts/Logic/TurnMapNode.gd diff --git a/Nodes/game.tscn b/Nodes/game.tscn index 3cae605..15bcb41 100644 --- a/Nodes/game.tscn +++ b/Nodes/game.tscn @@ -1,78 +1,32 @@ -[gd_scene load_steps=5 format=3 uid="uid://bl1yrgr7g06db"] +[gd_scene load_steps=5 format=3 uid="uid://n7w0ff7u25yc"] [ext_resource type="Script" path="res://scripts/Logic/Game.gd" id="1_iox18"] +[ext_resource type="PackedScene" uid="uid://byo1m0n20yl45" path="res://Nodes/cannon.tscn" id="2_vrf6k"] +[ext_resource type="Script" path="res://scripts/Train.gd" id="2_xl0he"] -[sub_resource type="Curve3D" id="Curve3D_42kdk"] -_data = { -"points": PackedVector3Array(-8, 0, 0, 8, 0, 0, 0, 0, -8, -16, 0, 0, 16, 0, 0, 72, 0, -16, 0, 0, -16, 0, 0, 16, 88, 0, 0, 16, 0, 0, -16, 0, 0, 72, 0, 16, 0, 0, 16, 0, 0, -16, 56, 0, 0, -16, 0, 0, 16, 0, 0, 72, 0, -16, 0, 0, -16, 0, 0, 16, 88, 0, 0, 16, 0, 0, -16, 0, 0, 72, 0, 16, 8, 0, 0, -8, 0, 0, 0, 0, 8, 0, 0, 8, 0, 0, -8, -8, 0, 0, -8, 0, 0, 8, 0, 0, 0, 0, -8, 0, 0, -8, 0, 0, 8, 8, 0, 0, 8, 0, 0, -8, 0, 0, 0, 0, 8, 0, 0, 8, 0, 0, -8, -8, 0, 0, -8, 0, 0, 8, 0, 0, 0, 0, -8), -"tilts": PackedFloat32Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) -} -point_count = 15 +[sub_resource type="BoxMesh" id="BoxMesh_ognqx"] +size = Vector3(13, 1, 25) -[sub_resource type="BoxMesh" id="BoxMesh_3vwcb"] -size = Vector3(1, 1, 2) - -[sub_resource type="Curve3D" id="Curve3D_ycdkn"] -_data = { -"points": PackedVector3Array(-16, 0, 0, 16, 0, 0, 0, 0, -16, 0, 0, -16, 0, 0, 16, 16, 0, 0, 16, 0, 0, -16, 0, 0, 0, 0, 16, 0, 0, 16, 0, 0, -16, -15.9109, 0.0032196, 0.0159729, -16, 0, 0, 16, 0, 0, 0, 0, -16, -8, 0, 0, 8, 0, 0, 72, 0, -8, 0, 0, -8, 0, 0, 8, 80, 0, 0, 8, 0, 0, -8, 0, 0, 72, 0, 8, 0, 0, 8, 0, 0, -8, 64, 0, 0, -8, 0, 0, 8, 0, 0, 72, 0, -8, 0, 0, -8, 0, 0, 8, 80, 0, 0, 8, 0, 0, -8, 0, 0, 72, 0, 8, 16, 0, 0, -16, 0, 0, 0, 0, 16, 0, 0, 16, 0, 0, -16, -16, 0, 0, -16, 0, 0, 16, 0, 0, 0, 0, -16), -"tilts": PackedFloat32Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) -} -point_count = 15 - -[node name="Game" type="Node" node_paths=PackedStringArray("p1", "p2")] +[node name="Game" type="Node"] script = ExtResource("1_iox18") -p1 = NodePath("HBoxContainer/SubViewportContainer/SubViewport/Player1Path/PathFollow3D") -p2 = NodePath("HBoxContainer/SubViewportContainer2/SubViewport/Player2Path/PathFollow3D") -[node name="HBoxContainer" type="HBoxContainer" parent="."] -offset_right = 40.0 -offset_bottom = 40.0 +[node name="Camera3D" type="Camera3D" parent="."] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 20, 0) -[node name="SubViewportContainer" type="SubViewportContainer" parent="HBoxContainer"] -layout_mode = 2 +[node name="Player1" type="Node3D" parent="."] +transform = Transform3D(1, 0, 3.55271e-15, 0, 1, 0, -3.55271e-15, 0, 1, -8, 0, 8) +script = ExtResource("2_xl0he") -[node name="SubViewport" type="SubViewport" parent="HBoxContainer/SubViewportContainer"] -handle_input_locally = false -size = Vector2i(960, 1080) -render_target_update_mode = 4 +[node name="Cannon" parent="Player1" instance=ExtResource("2_vrf6k")] -[node name="Player1Path" type="Path3D" parent="HBoxContainer/SubViewportContainer/SubViewport"] -curve = SubResource("Curve3D_42kdk") +[node name="MeshInstance3D2" type="MeshInstance3D" parent="Player1"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 0, -8) +mesh = SubResource("BoxMesh_ognqx") -[node name="PathFollow3D" type="PathFollow3D" parent="HBoxContainer/SubViewportContainer/SubViewport/Player1Path"] -transform = Transform3D(-4.37107e-08, 0, 0.999984, 0, 1, 0, -0.999984, 0, -4.37107e-08, 0, 0, -8) +[node name="Player2" type="Node3D" parent="."] +transform = Transform3D(-0.0154456, -6.75145e-10, -0.999879, -5.21344e-12, 1, -6.75146e-10, 0.999879, -5.2134e-12, -0.0154456, -8, 0, -32) +script = ExtResource("2_xl0he") -[node name="root" type="Node3D" parent="HBoxContainer/SubViewportContainer/SubViewport/Player1Path/PathFollow3D"] - -[node name="Zug1" type="MeshInstance3D" parent="HBoxContainer/SubViewportContainer/SubViewport/Player1Path/PathFollow3D/root"] -transform = Transform3D(1, 0, -7.10543e-15, 0, 1, 0, 7.10543e-15, 0, 1, 0, 0, 0) -mesh = SubResource("BoxMesh_3vwcb") -skeleton = NodePath("../..") - -[node name="Camera3D" type="Camera3D" parent="HBoxContainer/SubViewportContainer/SubViewport/Player1Path/PathFollow3D/root"] -transform = Transform3D(1.00002, 4.37121e-08, 4.37128e-08, -4.37121e-08, -4.37121e-08, 1, 4.37121e-08, -1.00002, -4.37128e-08, 0, 20, 0) - -[node name="SubViewportContainer2" type="SubViewportContainer" parent="HBoxContainer"] -layout_mode = 2 - -[node name="SubViewport" type="SubViewport" parent="HBoxContainer/SubViewportContainer2"] -handle_input_locally = false -size = Vector2i(960, 1080) -render_target_update_mode = 4 - -[node name="Player2Path" type="Path3D" parent="HBoxContainer/SubViewportContainer2/SubViewport"] -curve = SubResource("Curve3D_ycdkn") - -[node name="PathFollow3D" type="PathFollow3D" parent="HBoxContainer/SubViewportContainer2/SubViewport/Player2Path"] -transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, 0, -16) - -[node name="root" type="Node3D" parent="HBoxContainer/SubViewportContainer2/SubViewport/Player2Path/PathFollow3D"] -transform = Transform3D(-0.0154456, -6.75146e-10, -0.999881, -5.21344e-12, 1, -6.75146e-10, 0.999881, -5.21341e-12, -0.0154456, 0, 0, 0) - -[node name="Zug1" type="MeshInstance3D" parent="HBoxContainer/SubViewportContainer2/SubViewport/Player2Path/PathFollow3D/root"] -transform = Transform3D(1, 0, -7.10543e-15, 0, 1, 0, 7.10543e-15, 0, 1, 0, 0, 0) -mesh = SubResource("BoxMesh_3vwcb") -skeleton = NodePath("../..") - -[node name="Camera3D2" type="Camera3D" parent="HBoxContainer/SubViewportContainer2/SubViewport/Player2Path/PathFollow3D/root"] -transform = Transform3D(-0.0154455, -0.99988, -4.43872e-08, -4.43872e-08, -4.37069e-08, 1, -0.99988, 0.0154455, -4.37069e-08, 0, 20, 0) +[node name="MeshInstance3D" type="MeshInstance3D" parent="Player2"] +transform = Transform3D(-0.0154456, -5.21524e-12, 0.999881, -6.75146e-10, 1, -5.21521e-12, -0.999881, -6.75146e-10, -0.0154456, 31.7182, -1.23195e-08, -18.4921) +mesh = SubResource("BoxMesh_ognqx") diff --git a/project.godot b/project.godot index 0e16438..9c13efa 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,16 @@ _global_script_classes=[{ "path": "res://scripts/Modules/Cannon.gd" }, { "base": "Node", +"class": &"Game", +"language": &"GDScript", +"path": "res://scripts/Logic/Game.gd" +}, { +"base": "Node", +"class": &"MapNode", +"language": &"GDScript", +"path": "res://scripts/Logic/MapNode.gd" +}, { +"base": "Node", "class": &"Module", "language": &"GDScript", "path": "res://scripts/Modules/Module.gd" @@ -24,6 +34,16 @@ _global_script_classes=[{ "language": &"GDScript", "path": "res://scripts/Modules/Storage.gd" }, { +"base": "MapNode", +"class": &"StraightMapNode", +"language": &"GDScript", +"path": "res://scripts/Logic/StraightMapNode.gd" +}, { +"base": "Node", +"class": &"Train", +"language": &"GDScript", +"path": "res://scripts/Train.gd" +}, { "base": "Module", "class": &"TrainEngine", "language": &"GDScript", @@ -31,8 +51,12 @@ _global_script_classes=[{ }] _global_script_class_icons={ "Cannon": "", +"Game": "", +"MapNode": "", "Module": "", "Storage": "", +"StraightMapNode": "", +"Train": "", "TrainEngine": "" } diff --git a/scripts/Logic/Game.gd b/scripts/Logic/Game.gd index 38813c1..c0ab824 100644 --- a/scripts/Logic/Game.gd +++ b/scripts/Logic/Game.gd @@ -1,4 +1,5 @@ extends Node +class_name Game #Signals signal ammo_pickup @@ -6,36 +7,22 @@ signal shoot var t = 0.0 -@onready var tween = create_tween() as Tween +@onready var p1_train = $Player1 as Train +@onready var p2_train = $Player2 as Train -@export var p1 : PathFollow3D -@export var p2 : PathFollow3D -var speed_p1 = 10 -var speed_p2 = 10 func _ready(): - p1.get_node("root").test.connect(func(): print("player1")) - p2.get_node("root").test.connect(func(): print("player2")) - start_tween() - -func start_tween(): - tween.stop() - tween = create_tween() - tween.set_parallel() - tween.set_trans(Tween.TRANS_LINEAR) - tween.tween_property(p1, "progress_ratio", 1, speed_p1) - tween.tween_property(p2, "progress_ratio", -1, speed_p2) - + p2_train.is_P1 = false + p1_train.hit.connect(_on_hit_player) + +func _on_hit_player(player1,dmg): + if not player1: + p2_train.current_speed -= dmg + else: + p1_train.current_speed -= dmg func _process(delta): if Input.is_action_pressed("test"): - speed_p1 = 5 - start_tween() - - if round(p1.position.distance_to(p2.position)) == 9: emit_signal("shoot") - - - diff --git a/scripts/Logic/MapNode.gd b/scripts/Logic/MapNode.gd new file mode 100644 index 0000000..30135d3 --- /dev/null +++ b/scripts/Logic/MapNode.gd @@ -0,0 +1,14 @@ +extends Node +class_name MapNode + +const LENGTH = 10 +const PICKUP = 0 +var previous : MapNode + +var current_trains = [] + +func _on_train_entered(train): + current_trains.append(train) + +func _on_train_exit(train): + current_trains.erase(train) diff --git a/scripts/Logic/StraightMapNode.gd b/scripts/Logic/StraightMapNode.gd new file mode 100644 index 0000000..7a9763f --- /dev/null +++ b/scripts/Logic/StraightMapNode.gd @@ -0,0 +1,8 @@ +extends MapNode +class_name StraightMapNode + +var next : MapNode + +func _on_train_exit(train): + next._on_train_entered(train) + super._on_train_exit(train) diff --git a/scripts/Logic/TurnMapNode.gd b/scripts/Logic/TurnMapNode.gd new file mode 100644 index 0000000..f737172 --- /dev/null +++ b/scripts/Logic/TurnMapNode.gd @@ -0,0 +1,18 @@ +extends StraightMapNode + +signal turnEvent + +var is_left_turn = false +var turn = false +var turn_node : MapNode + +func _on_train_entered(train): + super._on_train_entered(train) + turnEvent.emit() + +func _on_train_exit(train): + if(turn): + turn_node._on_train_entered(train) + current_trains.erase(train) + else: + super._on_train_exit(train) diff --git a/scripts/Modules/Cannon.gd b/scripts/Modules/Cannon.gd index 83e5b63..8711c12 100644 --- a/scripts/Modules/Cannon.gd +++ b/scripts/Modules/Cannon.gd @@ -5,13 +5,13 @@ class_name Cannon enum STATE {INACTIVE, RELOADING, SHOOTING} var CAN_ENGAGE = true +var DAMAGE = 5 +@onready var train = get_parent() as Train #------------Methods-------------# func _ready(): - var root = get_tree().root.get_child(0) - root.shoot.connect(_on_signal_shooting) maxStashValue = 1 - currentStashValue = 0 + currentStashValue = 1 currentState = STATE.INACTIVE #TODO: FINISH func interact(): @@ -24,15 +24,15 @@ func interact(): return -func _on_signal_shooting(): +func shoot(): if not CAN_ENGAGE: - return + return false CAN_ENGAGE = false create_tween().tween_callback(func(): CAN_ENGAGE = true).set_delay(2) if currentStashValue >= 1 and currentState == STATE.INACTIVE: currentState = STATE.SHOOTING currentStashValue -= 1 - print("hit") currentState = STATE.INACTIVE - return + return true + return false diff --git a/scripts/Train.gd b/scripts/Train.gd index ecd8c53..6bfb2f1 100644 --- a/scripts/Train.gd +++ b/scripts/Train.gd @@ -1,17 +1,33 @@ extends Node +class_name Train -signal test +signal hit(player, dmg) +signal game_over -var speed = 10 +var is_P1 = true +var current_speed = 10 var CANNONS = [] var STORAGES = [] #@onready var ENGINE = $Module/Engine -# Called when the node enters the scene tree for the first time. +@onready var root = get_tree().root.get_child(0) as Game + func _ready(): - pass + for child in get_children(): + if(child is Cannon): + CANNONS.append(child) + if(child is Storage): + STORAGES.append(child) + root.shoot.connect(_on_signal_shooting) -# Called every frame. 'delta' is the elapsed time since the previous frame. -func _process(delta): - test.emit() # Replace with function body. +func _on_signal_shooting(): + for cannon in CANNONS: + if cannon.shoot(): + hit.emit(is_P1,cannon.DAMAGE) + print("Shot P2") + +func is_dead(): + if current_speed <= 0: + game_over.emit() +