Compare commits

14 Commits

Author SHA1 Message Date
Morph01
32f8f2fcde Add Hitmarkers 2024-08-30 18:09:55 +02:00
Morph01
67f7335dba Crosshair really in the center (it was in the top left center of the 40x40 centered rect) 2024-08-30 16:18:16 +02:00
Morph01
791cea356d Fix SubViewport bug when opening the scene FirstPersonPlayer / Zombie (bone collision shape) / Bullet in the editor 2024-08-30 15:49:22 +02:00
Morph01
68d3cec08a simple dynamic crosshair 2024-08-25 18:25:22 +02:00
Morph01
5c6f82c5d7 Random spawn of zombies 2024-08-22 19:40:32 +02:00
Morph01
faf6a9116d Add Dying animation 2024-08-22 17:53:25 +02:00
Morph01
1f52eb2c52 Collision of zombie body parts completed and different damage added depending on the body part hit 2024-08-22 16:15:54 +02:00
Morph01
d88cb9fbff Added zombie spawn animation, red screen and hit stagger when the player is hit. 2024-08-20 16:53:43 +02:00
Morph01
0c667944dd fix attack angle 2024-08-20 16:49:18 +02:00
Morph01
1a14c78ede Zombies with PathFinding Run and Attack ! 2024-08-16 19:17:51 +02:00
Morph01
6160561498 Squashed commit of the following:
commit 4fd60e7874
Author: Morph01 <145839520+Morph01@users.noreply.github.com>
Date:   Fri Aug 16 14:19:29 2024 +0200

    bug animation ???
2024-08-16 14:56:57 +02:00
Morph01
1662691b6e Better Implementation of Timer 2024-08-16 10:36:32 +02:00
Morph01
91d492c2d8 Bullets Colliding and Disappearing 2024-08-15 13:28:16 +02:00
Morph01
b92c5169c4 LaserGun without collisions 2024-08-14 18:46:31 +02:00
77 changed files with 4812 additions and 5501 deletions

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 178 KiB

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@@ -3276,6 +3276,7 @@ node_connections = [&"ground_air_transition", 0, &"iwr_blend", &"ground_air_tran
velocity = Vector3(0, -5821.84, 0) velocity = Vector3(0, -5821.84, 0)
[node name="Mesh" type="Node3D" parent="."] [node name="Mesh" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.0730165, 0)
[node name="Armature" type="Node3D" parent="Mesh"] [node name="Armature" type="Node3D" parent="Mesh"]
transform = Transform3D(0.01, 0, 0, 0, -3.57628e-09, -0.01, 0, 0.01, -3.57628e-09, 0, 0, 0) transform = Transform3D(0.01, 0, 0, 0, -3.57628e-09, -0.01, 0, 0.01, -3.57628e-09, 0, 0, 0)
@@ -3749,6 +3750,15 @@ skin = SubResource("Skin_l3wpu")
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.908729, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.908729, 0)
shape = SubResource("CapsuleShape3D_mm42w") shape = SubResource("CapsuleShape3D_mm42w")
[node name="SpringArmPivot" type="SpringArmPivot" parent="."]
[node name="SpringArm3D" type="SpringArm3D" parent="SpringArmPivot"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.31667, 0)
spring_length = 2.0
margin = 0.2
[node name="Camera3D" type="Camera3D" parent="SpringArmPivot/SpringArm3D"]
[node name="AnimationPlayer" type="AnimationPlayer" parent="."] [node name="AnimationPlayer" type="AnimationPlayer" parent="."]
root_node = NodePath("../Mesh") root_node = NodePath("../Mesh")
libraries = { libraries = {
@@ -3761,6 +3771,6 @@ callback_mode_process = 0
tree_root = SubResource("AnimationNodeBlendTree_dptuf") tree_root = SubResource("AnimationNodeBlendTree_dptuf")
anim_player = NodePath("../AnimationPlayer") anim_player = NodePath("../AnimationPlayer")
parameters/ground_air_transition/current_state = "air" parameters/ground_air_transition/current_state = "air"
parameters/ground_air_transition/transition_request = "" parameters/ground_air_transition/transition_request = "air"
parameters/ground_air_transition/current_index = 1 parameters/ground_air_transition/current_index = 1
parameters/iwr_blend/blend_amount = -1.0 parameters/iwr_blend/blend_amount = -1.0

File diff suppressed because one or more lines are too long

View File

@@ -1,9 +1,10 @@
[gd_scene load_steps=17 format=3 uid="uid://coue2qehpn4fr"] [gd_scene load_steps=19 format=3 uid="uid://coue2qehpn4fr"]
[ext_resource type="Texture2D" path="res://Assets/Textures/Sky.png" id="1_mnexj"] [ext_resource type="Texture2D" uid="uid://dujfl12rge3p4" path="res://Assets/Textures/Sky.png" id="1_mnexj"]
[ext_resource type="Texture2D" path="res://Assets/Textures/Black.png" id="2_fkwcn"] [ext_resource type="Texture2D" uid="uid://b8n5rff2a8h2u" path="res://Assets/Textures/Black.png" id="2_fkwcn"]
[ext_resource type="Texture2D" path="res://Assets/Textures/Orange.png" id="3_ux02w"] [ext_resource type="Texture2D" uid="uid://lpbttkw7gpxj" path="res://Assets/Textures/Orange.png" id="3_ux02w"]
[ext_resource type="Texture2D" path="res://Assets/Textures/Green.png" id="4_wp15n"] [ext_resource type="Texture2D" uid="uid://di3uyny341483" path="res://Assets/Textures/Green.png" id="4_wp15n"]
[ext_resource type="PackedScene" uid="uid://d38w4ae3qj0k4" path="res://Scenes/Characters/first_person_player.tscn" id="5_8ctht"]
[sub_resource type="PanoramaSkyMaterial" id="PanoramaSkyMaterial_6c4vd"] [sub_resource type="PanoramaSkyMaterial" id="PanoramaSkyMaterial_6c4vd"]
panorama = ExtResource("1_mnexj") panorama = ExtResource("1_mnexj")
@@ -17,6 +18,12 @@ sky = SubResource("Sky_5ngqa")
tonemap_mode = 2 tonemap_mode = 2
glow_enabled = true glow_enabled = true
[sub_resource type="NavigationMesh" id="NavigationMesh_2pdqo"]
vertices = PackedVector3Array(-5.25, 0.5, 1, -5, 0.5, -1.75, -5.5, 0.5, -2, -11.25, 0.5, 1, -5.5, 0.5, -3.75, -3.25, 0.5, -4, -3.25, 0.5, -24.5, -11.25, 0.5, 1, -5.5, 0.5, -2, -5.5, 0.5, -3.75, -24.5, 0.5, 3.25, -11.5, 0.5, 3.25, -11.25, 0.5, 1, -24.5, 0.5, 3.25, -11.25, 0.5, 1, -5.5, 0.5, -3.75, -3.25, 0.5, -24.5, -24.5, 0.5, -24.5, -3.25, 0.5, -4, 3.5, 0.5, -4, 3.5, 0.5, -24.5, -3.25, 0.5, -24.5, 5.5, 0.5, -4, 5.75, 0.5, -3, 24.5, 0.5, -3, 24.5, 0.5, -24.5, 3.5, 0.5, -24.5, 3.5, 0.5, -24.5, 3.5, 0.5, -4, 5.5, 0.5, -4, -5, 0.5, -1.75, -5.25, 0.5, 1, -5, 0.75, 2, 5.25, 0.5, -1.75, -5, 0.75, 2, -7.25, 1.5, 2, -7.25, 1.5, 6, -5, 0.75, 6.25, 24.5, 0.5, -3, 5.75, 0.5, -3, 5.25, 0.5, -1.75, -5.25, 0.5, 7.25, -6.75, 0.5, 7.25, -6.5, 0.5, 24.5, 5.25, 0.5, -1.75, -5, 0.75, 2, -5, 0.75, 6.25, 5.25, 0.5, -1.75, -5, 0.75, 6.25, -5.25, 0.5, 7.25, -6.5, 0.5, 24.5, 24.5, 0.5, 24.5, 24.5, 0.5, -3, -7.25, 1.5, 6, -7.25, 1.5, 2, -14, 3.75, 2, -14, 3.75, 6, -11.5, 0.5, 7, -11.5, 0.5, 3.25, -24.5, 0.5, 3.25, -10, 0.5, 24.5, -9.75, 0.5, 7.25, -11.5, 0.5, 7, -11.5, 0.5, 7, -24.5, 0.5, 3.25, -24.5, 0.5, 24.5, -10, 0.5, 24.5, -10, 0.5, 24.5, -6.5, 0.5, 24.5, -6.75, 0.5, 7.25, -9.75, 0.5, 7.25)
polygons = [PackedInt32Array(1, 0, 2), PackedInt32Array(2, 0, 3), PackedInt32Array(6, 5, 4), PackedInt32Array(7, 9, 8), PackedInt32Array(12, 11, 10), PackedInt32Array(14, 13, 15), PackedInt32Array(15, 13, 16), PackedInt32Array(16, 13, 17), PackedInt32Array(21, 20, 18), PackedInt32Array(18, 20, 19), PackedInt32Array(23, 22, 24), PackedInt32Array(24, 22, 25), PackedInt32Array(25, 22, 26), PackedInt32Array(29, 28, 27), PackedInt32Array(31, 30, 32), PackedInt32Array(32, 30, 33), PackedInt32Array(35, 34, 36), PackedInt32Array(36, 34, 37), PackedInt32Array(40, 39, 38), PackedInt32Array(43, 42, 41), PackedInt32Array(46, 45, 44), PackedInt32Array(48, 47, 49), PackedInt32Array(49, 47, 50), PackedInt32Array(50, 47, 52), PackedInt32Array(50, 52, 51), PackedInt32Array(56, 55, 53), PackedInt32Array(53, 55, 54), PackedInt32Array(59, 58, 57), PackedInt32Array(62, 61, 60), PackedInt32Array(66, 65, 63), PackedInt32Array(63, 65, 64), PackedInt32Array(70, 69, 67), PackedInt32Array(67, 69, 68)]
agent_radius = 0.4
agent_max_slope = 60.0
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ajchh"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ajchh"]
albedo_texture = ExtResource("2_fkwcn") albedo_texture = ExtResource("2_fkwcn")
uv1_triplanar = true uv1_triplanar = true
@@ -52,38 +59,97 @@ data = PackedVector3Array(-12.5, 2.5, 2.5, 2.5, -2.5, 2.5, -2.5, -2.5, 2.5, -12.
[node name="World" type="World"] [node name="World" type="World"]
[node name="WorldEnvironment" type="WorldEnvironment" parent="."] [node name="Map" type="Node3D" parent="."]
[node name="WorldEnvironment" type="WorldEnvironment" parent="Map"]
environment = SubResource("Environment_ctwiv") environment = SubResource("Environment_ctwiv")
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] [node name="DirectionalLight3D" type="DirectionalLight3D" parent="Map"]
transform = Transform3D(-0.866026, -0.433013, 0.249999, 0.5, -0.75, 0.433012, -1.3411e-07, 0.499999, 0.866026, 0, 0, 0) transform = Transform3D(-0.866026, -0.433013, 0.249999, 0.5, -0.750001, 0.433012, -1.3411e-07, 0.499999, 0.866026, 0, 0, 0)
shadow_enabled = true shadow_enabled = true
[node name="Floor" type="MeshInstance3D" parent="."] [node name="NavigationRegion3D" type="NavigationRegion3D" parent="Map"]
navigation_mesh = SubResource("NavigationMesh_2pdqo")
[node name="Floor" type="MeshInstance3D" parent="Map/NavigationRegion3D"]
mesh = SubResource("PlaneMesh_mmup0") mesh = SubResource("PlaneMesh_mmup0")
skeleton = NodePath("../../..")
[node name="StaticBody3D" type="StaticBody3D" parent="Floor"] [node name="StaticBody3D" type="StaticBody3D" parent="Map/NavigationRegion3D/Floor"]
collision_layer = 3
collision_mask = 3
[node name="CollisionShape3D" type="CollisionShape3D" parent="Floor/StaticBody3D"] [node name="CollisionShape3D" type="CollisionShape3D" parent="Map/NavigationRegion3D/Floor/StaticBody3D"]
shape = SubResource("ConcavePolygonShape3D_26ptr") shape = SubResource("ConcavePolygonShape3D_26ptr")
[node name="Wall" type="MeshInstance3D" parent="."] [node name="Wall" type="MeshInstance3D" parent="Map/NavigationRegion3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, -3) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, -3)
mesh = SubResource("BoxMesh_plpqy") mesh = SubResource("BoxMesh_plpqy")
skeleton = NodePath("../../..")
[node name="StaticBody3D" type="StaticBody3D" parent="Wall"] [node name="StaticBody3D" type="StaticBody3D" parent="Map/NavigationRegion3D/Wall"]
collision_layer = 3
collision_mask = 3
[node name="CollisionShape3D" type="CollisionShape3D" parent="Wall/StaticBody3D"] [node name="CollisionShape3D" type="CollisionShape3D" parent="Map/NavigationRegion3D/Wall/StaticBody3D"]
shape = SubResource("ConcavePolygonShape3D_v7prx") shape = SubResource("ConcavePolygonShape3D_v7prx")
[node name="Slope" type="MeshInstance3D" parent="."] [node name="Slope" type="MeshInstance3D" parent="Map/NavigationRegion3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 1, 4) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -2, 1, 4)
mesh = SubResource("PrismMesh_0l7qq") mesh = SubResource("PrismMesh_0l7qq")
skeleton = NodePath("../../..")
surface_material_override/0 = SubResource("StandardMaterial3D_pfpgv") surface_material_override/0 = SubResource("StandardMaterial3D_pfpgv")
[node name="StaticBody3D" type="StaticBody3D" parent="Slope"] [node name="StaticBody3D" type="StaticBody3D" parent="Map/NavigationRegion3D/Slope"]
collision_layer = 3
collision_mask = 3
[node name="CollisionShape3D" type="CollisionShape3D" parent="Slope/StaticBody3D"] [node name="CollisionShape3D" type="CollisionShape3D" parent="Map/NavigationRegion3D/Slope/StaticBody3D"]
shape = SubResource("ConcavePolygonShape3D_rit6o") shape = SubResource("ConcavePolygonShape3D_rit6o")
[node name="Players" type="Node" parent="."] [node name="Spawns" type="Node3D" parent="Map"]
[node name="Spawn1" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 18.6089)
[node name="Spawn2" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 18.8451, 0, 18.6089)
[node name="Spawn3" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 18.8451, 0, 0.0245247)
[node name="Spawn4" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, 19.0777, 0, -20.2546)
[node name="Spawn5" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, 1.64303, 0, -20.2546)
[node name="Spawn6" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, -19.0255, 0, -20.2546)
[node name="Spawn7" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, -19.0255, 0, 0.256641)
[node name="Spawn8" type="Node3D" parent="Map/Spawns"]
transform = Transform3D(-0.269021, -0.963134, 0, 0.963134, -0.269021, 0, 0, 0, 1, -19.0255, 0, 16.4132)
[node name="FirstPersonPlayer" parent="." instance=ExtResource("5_8ctht")]
[node name="UI" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0
offset_right = 40.0
offset_bottom = 40.0
[node name="HitRect" type="ColorRect" parent="UI"]
visible = false
layout_mode = 0
offset_left = -177.0
offset_top = -96.0
offset_right = 1365.0
offset_bottom = 780.0
color = Color(1, 0, 0, 0.34902)
[node name="ZombieSpawnTimer" type="Timer" parent="."]
wait_time = 5.0
autostart = true

View File

@@ -1,41 +0,0 @@
[gd_scene format=3 uid="uid://bqfqg7xwwlxd8"]
[node name="MainMenu" type="MainMenu"]
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 4
size_flags_vertical = 4
[node name="Container" type="VBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -228.512
offset_top = -89.5
offset_right = 228.512
offset_bottom = 89.5
grow_horizontal = 2
grow_vertical = 2
[node name="JoinButton" type="Button" parent="Container"]
layout_mode = 2
theme_override_font_sizes/font_size = 35
text = "Join Game"
[node name="CreateButton" type="Button" parent="Container"]
layout_mode = 2
theme_override_font_sizes/font_size = 35
text = "Create Game"
[node name="QuitButton" type="Button" parent="Container"]
layout_mode = 2
theme_override_font_sizes/font_size = 35
text = "Quit"

View File

@@ -1,5 +0,0 @@
[gd_scene format=3 uid="uid://clafls1xhludi"]
[node name="Lobby" type="Lobby"]
[node name="NetworkInterface" type="NetworkInterface" parent="."]

View File

@@ -1,3 +0,0 @@
[gd_scene format=3 uid="uid://us5sb4a0kq8d"]
[node name="Server" type="Server"]

View File

@@ -0,0 +1,54 @@
[gd_scene load_steps=6 format=3 uid="uid://do601jl7p1u22"]
[sub_resource type="BoxMesh" id="BoxMesh_ibwn0"]
size = Vector3(0.05, 0.05, 1)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2qdhl"]
albedo_color = Color(1, 0.764706, 0.294118, 1)
emission_enabled = true
emission = Color(0.568627, 1, 0.313726, 1)
emission_energy_multiplier = 5.0
[sub_resource type="ParticleProcessMaterial" id="ParticleProcessMaterial_618v8"]
direction = Vector3(0, 0, 1)
initial_velocity_min = 3.0
initial_velocity_max = 5.0
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_gn2fy"]
albedo_color = Color(1, 1, 0.443137, 1)
emission_enabled = true
emission = Color(0.568627, 1, 0.313726, 1)
emission_energy_multiplier = 8.0
[sub_resource type="BoxMesh" id="BoxMesh_5un5e"]
material = SubResource("StandardMaterial3D_gn2fy")
size = Vector3(0.04, 0.04, 0.04)
[node name="Bullet" type="Bullet"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -56304.6)
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.6)
mesh = SubResource("BoxMesh_ibwn0")
surface_material_override/0 = SubResource("StandardMaterial3D_2qdhl")
[node name="RayCast3D" type="RayCast3D" parent="."]
target_position = Vector3(0, 0, -0.6)
collision_mask = 2
collide_with_areas = true
[node name="GPUParticles3D" type="GPUParticles3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.7)
emitting = false
one_shot = true
explosiveness = 1.0
visibility_aabb = AABB(-1.00001, -6.06334, -1.00001, 2.00002, 7.06267, 41.4102)
process_material = SubResource("ParticleProcessMaterial_618v8")
draw_pass_1 = SubResource("BoxMesh_5un5e")
[node name="Timer" type="Timer" parent="."]
wait_time = 10.0
one_shot = true
autostart = true
[connection signal="timeout" from="Timer" to="." method="_on_timer_timeout"]

View File

@@ -0,0 +1,36 @@
[gd_scene load_steps=4 format=3 uid="uid://ciex3x7rhv1bx"]
[ext_resource type="PackedScene" uid="uid://bkskefixbe2cw" path="res://Assets/Models/Weapons/Steampunk Rifle.glb" id="1_0bnv5"]
[sub_resource type="Animation" id="Animation_20uwx"]
resource_name = "Shoot"
length = 0.1
step = 0.01
tracks/0/type = "value"
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/path = NodePath("Node:position")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/keys = {
"times": PackedFloat32Array(0, 0.01, 0.1),
"transitions": PackedFloat32Array(1, 1, 1),
"update": 0,
"values": [Vector3(0, 0, 0), Vector3(0, 0, 0.05), Vector3(0, 0, 0)]
}
[sub_resource type="AnimationLibrary" id="AnimationLibrary_113bo"]
_data = {
"Shoot": SubResource("Animation_20uwx")
}
[node name="Steampunk Rifle" instance=ExtResource("1_0bnv5")]
[node name="AnimationPlayer" type="AnimationPlayer" parent="." index="1"]
libraries = {
"": SubResource("AnimationLibrary_113bo")
}
[node name="RayCast3D" type="RayCast3D" parent="." index="2"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -0.6)
target_position = Vector3(0, 0, -1)

View File

@@ -1,15 +0,0 @@
[gd_scene load_steps=3 format=3 uid="uid://4jt0v2b2l4rt"]
[ext_resource type="PackedScene" path="res://Scenes/Network/networking.tscn" id="1_06ibn"]
[ext_resource type="PackedScene" path="res://Scenes/Menus/mainmenu.tscn" id="2_lavg1"]
[node name="Main" type="Main"]
[node name="Lobby" parent="." instance=ExtResource("1_06ibn")]
[node name="MainMenu" parent="." instance=ExtResource("2_lavg1")]
[connection signal="local_player_connected" from="Lobby" to="MainMenu" method="on_connected"]
[connection signal="change_scene" from="MainMenu" to="." method="change_scene"]
[connection signal="create_game" from="MainMenu" to="Lobby" method="create_game"]
[connection signal="join_game" from="MainMenu" to="Lobby" method="join_game"]

View File

@@ -1,5 +1,5 @@
[icons] [icons]
Blitz3 = "res://icon.png" Blitz3 = "res://icon.svg"
[configuration] [configuration]

19
godot/bullet.tscn Normal file
View File

@@ -0,0 +1,19 @@
[gd_scene load_steps=3 format=3 uid="uid://bt0551kcc5tw4"]
[sub_resource type="BoxMesh" id="BoxMesh_ibwn0"]
size = Vector3(0.05, 0.05, 1)
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2qdhl"]
albedo_color = Color(1, 0.764706, 0.294118, 1)
emission_enabled = true
emission = Color(0.568627, 1, 0.313726, 1)
emission_energy_multiplier = 5.0
[node name="Bullet" type="Bullet"]
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
mesh = SubResource("BoxMesh_ibwn0")
surface_material_override/0 = SubResource("StandardMaterial3D_2qdhl")
[node name="RayCast3D" type="RayCast3D" parent="."]
target_position = Vector3(0, 0, -0.6)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 52 KiB

1
godot/icon.svg Normal file
View File

@@ -0,0 +1 @@
<svg height="128" width="128" xmlns="http://www.w3.org/2000/svg"><rect x="2" y="2" width="124" height="124" rx="14" fill="#363d52" stroke="#212532" stroke-width="4"/><g transform="scale(.101) translate(122 122)"><g fill="#fff"><path d="M105 673v33q407 354 814 0v-33z"/><path fill="#478cbf" d="m105 673 152 14q12 1 15 14l4 67 132 10 8-61q2-11 15-15h162q13 4 15 15l8 61 132-10 4-67q3-13 15-14l152-14V427q30-39 56-81-35-59-83-108-43 20-82 47-40-37-88-64 7-51 8-102-59-28-123-42-26 43-46 89-49-7-98 0-20-46-46-89-64 14-123 42 1 51 8 102-48 27-88 64-39-27-82-47-48 49-83 108 26 42 56 81zm0 33v39c0 276 813 276 813 0v-39l-134 12-5 69q-2 10-14 13l-162 11q-12 0-16-11l-10-65H447l-10 65q-4 11-16 11l-162-11q-12-3-14-13l-5-69z"/><path d="M483 600c3 34 55 34 58 0v-86c-3-34-55-34-58 0z"/><circle cx="725" cy="526" r="90"/><circle cx="299" cy="526" r="90"/></g><g fill="#414042"><circle cx="307" cy="532" r="60"/><circle cx="717" cy="532" r="60"/></g></g></svg>

After

Width:  |  Height:  |  Size: 950 B

View File

@@ -11,14 +11,9 @@ config_version=5
[application] [application]
config/name="Blitz3" config/name="Blitz3"
run/main_scene="res://Scenes/main.tscn" run/main_scene="res://Scenes/Levels/world.tscn"
config/features=PackedStringArray("4.2", "Forward Plus") config/features=PackedStringArray("4.2", "Forward Plus")
config/icon="res://icon.png" config/icon="res://icon.svg"
[display]
window/size/viewport_width=1920
window/size/viewport_height=1080
[input] [input]
@@ -54,6 +49,11 @@ jump={
} }
escape={ escape={
"deadzone": 0.5, "deadzone": 0.5,
"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"echo":false,"script":null) "events": [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":4194305,"key_label":0,"unicode":0,"echo":false,"script":null)
]
}
shoot={
"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":1,"position":Vector2(351, 9),"global_position":Vector2(355, 50),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
] ]
} }

View File

@@ -1,25 +0,0 @@
#pragma once
/**
* \file NonCopyable.h
* \brief File containing the blitz::NonCopyable class
*/
namespace blitz {
/**
* \class NonCopyable
* \brief Class used to make a class non copyable
* \note Inherit from this class privately to make a class non copyable
*/
class NonCopyable {
public:
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
protected:
NonCopyable() {}
~NonCopyable() {}
};
} // namespace blitz

View File

@@ -1,11 +0,0 @@
#pragma once
#include <cstdint>
namespace blitz {
using EntityID = std::uint64_t;
using PeerID = std::int32_t;
using PlayerID = PeerID;
} // namespace blitz

View File

@@ -1,89 +0,0 @@
#pragma once
#include <godot_cpp/variant/packed_byte_array.hpp>
#include <godot_cpp/variant/string.hpp>
#include <vector>
namespace blitz {
namespace protocol {
class PlayerInfo;
#define Operators(Type, GodotType) \
ByteBuffer& operator>>(Type& a_Data) { \
a_Data = m_Buffer.decode_##GodotType(m_ReadOffset); \
m_ReadOffset += sizeof(a_Data); \
return *this; \
} \
\
ByteBuffer& operator<<(Type a_Data) { \
m_Buffer.resize(m_Buffer.size() + sizeof(a_Data)); \
m_Buffer.encode_##GodotType(m_Buffer.size() - sizeof(a_Data), a_Data); \
return *this; \
}
class ByteBuffer {
private:
godot::PackedByteArray m_Buffer;
std::size_t m_ReadOffset;
public:
ByteBuffer(godot::PackedByteArray&& a_Buffer) : m_Buffer(std::move(a_Buffer)), m_ReadOffset(0) {}
ByteBuffer() : m_ReadOffset(0) {
m_Buffer.resize(0);
}
const godot::PackedByteArray& GetByteArray() const {
return m_Buffer;
}
godot::PackedByteArray& GetByteArray() {
return m_Buffer;
}
// Integers
Operators(int8_t, s8);
Operators(uint8_t, u8);
Operators(int16_t, s16);
Operators(uint16_t, u16);
Operators(int32_t, s32);
Operators(uint32_t, u32);
Operators(int64_t, s64);
Operators(uint64_t, u64);
// Reals
Operators(float, float);
Operators(double, double);
ByteBuffer& operator<<(const godot::String& a_Data);
ByteBuffer& operator>>(godot::String& a_Data);
ByteBuffer& operator<<(const godot::Vector3& a_Data);
ByteBuffer& operator>>(godot::Vector3& a_Data);
template <typename T>
ByteBuffer& operator<<(const std::vector<T>& a_Data) {
*this << static_cast<std::uint32_t>(a_Data.size());
for (const T& data : a_Data) {
*this << data;
}
return *this;
}
template <typename T>
ByteBuffer& operator>>(std::vector<T>& a_Data) {
std::uint32_t arraySize;
*this >> arraySize;
a_Data.resize(arraySize);
for (std::uint32_t i = 0; i < arraySize; i++) {
*this >> a_Data[i];
}
return *this;
}
ByteBuffer& operator<<(const PlayerInfo& a_Data);
ByteBuffer& operator>>(PlayerInfo& a_Data);
};
} // namespace protocol
} // namespace blitz

View File

@@ -1,72 +0,0 @@
#pragma once
#include <blitz/common/Types.h>
#include <vector>
#include <godot_cpp/variant/string.hpp>
#include <godot_cpp/variant/vector3.hpp>
namespace blitz {
namespace protocol {
struct PlayerInfo {
PlayerID m_PlayerId;
godot::String m_PlayerName;
};
namespace data {
struct PlayerLogin {
godot::String m_PlayerName;
};
struct UpdateHealth {
float m_NewHealth;
};
struct LoggingSuccess {
PlayerID m_PlayerId;
};
struct PlayerDeath {};
struct PlayerJoin {
PlayerInfo m_Player;
};
struct PlayerLeave {
PlayerID m_PlayerId;
};
struct PlayerStats {};
struct PlayerList {
std::vector<PlayerInfo> m_Players;
};
struct ServerConfig {};
struct ServerTps {};
struct UpdateGameState {};
struct KeepAlive {
std::uint64_t m_KeepAliveId;
};
struct Disconnect {};
struct ChatMessage {
godot::String m_Text;
};
struct PlayerPositionAndRotation {
PlayerID m_Player;
godot::Vector3 m_Position;
godot::Vector3 m_Rotation;
};
struct PlayerShoot {};
} // namespace data
} // namespace protocol
} // namespace blitz

View File

@@ -1,49 +0,0 @@
#pragma once
/**
* \file PacketDeclare.h
* \brief Holds the definitions of the packets (but not their content)
*/
namespace blitz {
namespace protocol {
/**
* \enum PacketSender
* \brief Indicate who should send a packet
*/
enum class PacketSender {
/** Sent by clients and server */
Both,
/** Sent by clients to the server */
Client,
/** Sent by server to the clients */
Server,
};
/**
* \def DeclareAllPacket
* \brief Avoids repetitive operations on packets
*/
#define DeclareAllPacket() \
DeclarePacket(ChatMessage, Reliable, Both) \
DeclarePacket(Disconnect, Reliable, Both) \
DeclarePacket(KeepAlive, Reliable, Both) \
DeclarePacket(LoggingSuccess, Reliable, Server) \
DeclarePacket(PlayerDeath, Reliable, Server) \
DeclarePacket(PlayerJoin, Reliable, Server) \
DeclarePacket(PlayerLeave, Reliable, Server) \
DeclarePacket(PlayerList, Reliable, Server) \
DeclarePacket(PlayerLogin, Reliable, Client) \
DeclarePacket(PlayerPositionAndRotation, Reliable, Both) \
DeclarePacket(PlayerShoot, Reliable, Both) \
DeclarePacket(PlayerStats, Reliable, Server) \
DeclarePacket(ServerConfig, Reliable, Server) \
DeclarePacket(ServerTps, Reliable, Server) \
DeclarePacket(UpdateGameState, Reliable, Server) \
DeclarePacket(UpdateHealth, Reliable, Client)
} // namespace protocol
} // namespace blitz

View File

@@ -1,58 +0,0 @@
#pragma once
/**
* \file PacketDispatcher.h
* \brief File containing the blitz::protocol::PacketDispatcher class
*/
#include <blitz/common/NonCopyable.h>
#include <blitz/protocol/Packets.h>
#include <map>
namespace blitz {
namespace protocol {
class PacketHandler;
/**
* \class PacketDispatcher
* \brief Class used to dispatch packets
*/
class PacketDispatcher : private NonCopyable {
private:
std::map<PacketType, std::vector<PacketHandler*>> m_Handlers;
public:
/**
* \brief Constructor
*/
PacketDispatcher() {}
/**
* \brief Dispatch a packet
* \param packet The packet to dispatch
*/
void Dispatch(const Packet& packet);
/**
* \brief Register a packet handler
* \param type The packet type
* \param handler The packet handler
*/
void RegisterHandler(PacketType type, PacketHandler& handler);
/**
* \brief Unregister a packet handler
* \param type The packet type
* \param handler The packet handler
*/
void UnregisterHandler(PacketType type, PacketHandler& handler);
/**
* \brief Unregister a packet handler
* \param handler The packet handler
*/
void UnregisterHandler(PacketHandler& handler);
};
} // namespace protocol
} // namespace blitz

View File

@@ -1,21 +0,0 @@
#pragma once
#include <blitz/protocol/Packets.h>
#include <memory>
namespace blitz {
namespace protocol {
namespace PacketFactory {
template<typename PacketDerived, typename = typename std::enable_if<std::is_base_of<Packet, PacketDerived>::value>::type>
std::unique_ptr<PacketDerived> CreatePacket() {
return std::make_unique<PacketDerived>();
}
const std::unique_ptr<Packet>& CreateReadOnlyPacket(PacketType a_Type);
void Init();
} // namespace PacketFactory
} // namespace protocol
} // namespace blitz

View File

@@ -1,34 +0,0 @@
#pragma once
/**
* \file PacketHandler.h
* \brief File containing the blitz::protocol::PacketHandler class
*/
#include <blitz/protocol/Packets.h>
#include <blitz/protocol/PacketVisitor.h>
namespace blitz {
namespace protocol {
class PacketDispatcher;
#define DeclarePacket(PacketName, ...) virtual void Visit(const packets::PacketName&); virtual void HandlePacket(const packets::PacketName&) {}
/**
* \class PacketHandler
* \brief Class used to handle packets
*/
class PacketHandler : public PacketVisitor {
public:
PacketHandler() {}
~PacketHandler() {}
DeclareAllPacket()
};
#undef DeclarePacket
} // namespace protocol
} // namespace blitz

View File

@@ -1,20 +0,0 @@
#pragma once
#include <blitz/protocol/Packets.h>
#include <godot_cpp/variant/packed_byte_array.hpp>
#include <memory>
namespace blitz {
namespace protocol {
using PacketPtr = std::unique_ptr<Packet>;
namespace PacketSerializer {
godot::PackedByteArray Serialize(const Packet& a_Packet);
std::unique_ptr<Packet> Deserialize(godot::PackedByteArray& a_Data);
}
} // namespace protocol
} // namespace blitz

View File

@@ -1,39 +0,0 @@
#pragma once
/**
* \file PacketVisitor.h
* \brief File containing the blitz::protocol::PacketVisitor class
*/
#include <blitz/protocol/Packets.h>
namespace blitz {
namespace protocol {
#define DeclarePacket(PacketName, ...) \
/** This function is called when the packet processed by PacketVisitor::Check is a PacketName */ \
virtual void Visit(const packets::PacketName&) {}
/**
* \class PacketVisitor
* \brief This class uses double-dispatch in order to find the real type of a packet
*/
class PacketVisitor : private NonCopyable {
protected:
PacketVisitor() {}
virtual ~PacketVisitor() {}
public:
/**
* \brief Calls the right PacketVisitor::Visit method corresponding to the real type of the packet
* \param packet the Packet to visit
*/
void Check(const Packet& packet);
DeclareAllPacket()
};
#undef DeclarePacket
} // namespace protocol
} // namespace blitz

View File

@@ -1,112 +0,0 @@
#pragma once
/**
* \file Packets.h
* \brief File containing the definitions of the packets
*/
#include <blitz/common/NonCopyable.h>
#include <blitz/protocol/PacketData.h>
#include <blitz/protocol/PacketDeclare.h>
namespace blitz {
namespace protocol {
class PacketVisitor;
/** A Packet id is 8 bits wide */
using PacketID = std::uint8_t;
#define DeclarePacket(PacketName, ...) /** PacketName */ PacketName,
/**
* \enum PacketType
* \brief Map a Packet to an id
*/
enum class PacketType : PacketID {
DeclareAllPacket()
/** The number of packets */
PACKET_COUNT
};
#undef DeclarePacket
class Packet : private NonCopyable {
public:
/**
* \return The real type of the packet
*/
virtual PacketType GetType() const = 0;
/**
* \brief The network peer who sent the packet
*/
PeerID m_Sender;
private:
/** Use a PacketVisitor to make double-dispatch possible */
virtual void Accept(PacketVisitor& a_Visitor) const = 0;
friend class PacketVisitor;
};
namespace packets {
/**
* \class ConcretePacket
* \brief A Packet associated with an id and holding data
* \tparam PT The packet type
* \tparam Data The structure holding the data of the packet (in blitz::protocol::data namespace)
*/
template <PacketType PT, typename Data>
class ConcretePacket : public Packet {
public:
/** The type of the struct holding the data */
using PacketDataType = Data;
/** The structure holding the actual data */
PacketDataType m_Data;
/** Construct the packet with data of type PacketDataType */
ConcretePacket(const PacketDataType& a_Data = {});
constexpr PacketType GetType() const override {
return PT;
};
private:
void Accept(PacketVisitor& a_Visitor) const override;
};
// define BLITZ_INSTANCIATE_PACKETS
// before including this file
// if you want to instantiate templates
#ifdef BLITZ_INSTANCIATE_PACKETS
#define DeclarePacket(PacketName, ...) \
using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>; \
template class ConcretePacket<PacketType::PacketName, data::PacketName>;
#else
#define DeclarePacket(PacketName, ...) /** Defines the PacketName packet */ \
using PacketName = ConcretePacket<PacketType::PacketName, data::PacketName>;
#endif
DeclareAllPacket()
#undef DeclarePacket
} // namespace packets
} // namespace protocol
} // namespace blitz

View File

@@ -0,0 +1,72 @@
#include "BoneCollisionShape.h"
#include <godot_cpp/classes/engine.hpp>
using namespace godot;
namespace blitz {
void BoneCollisionShape::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_head_damage"), &BoneCollisionShape::get_head_damage);
ClassDB::bind_method(D_METHOD("set_head_damage", "m_HeadDamage"), &BoneCollisionShape::set_head_damage);
ADD_PROPERTY(PropertyInfo(Variant::INT, "m_HeadDamage"), "set_head_damage", "get_head_damage");
ClassDB::bind_method(D_METHOD("get_body_damage"), &BoneCollisionShape::get_body_damage);
ClassDB::bind_method(D_METHOD("set_body_damage", "m_BodyDamage"), &BoneCollisionShape::set_body_damage);
ADD_PROPERTY(PropertyInfo(Variant::INT, "m_BodyDamage"), "set_head_damage", "get_body_damage");
ADD_SIGNAL(MethodInfo("a_ZombieHeadShotHit", PropertyInfo(Variant::INT, "dam")));
ADD_SIGNAL(MethodInfo("a_ZombieBodyShotHit", PropertyInfo(Variant::INT, "dam")));
ClassDB::bind_method(D_METHOD("headshot_hit"), &BoneCollisionShape::headshot_hit);
ClassDB::bind_method(D_METHOD("bodyshot_hit"), &BoneCollisionShape::bodyshot_hit);
}
BoneCollisionShape::BoneCollisionShape() {}
BoneCollisionShape::~BoneCollisionShape() {}
void BoneCollisionShape::_ready() {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
#endif
m_Crosshair = Object::cast_to<Crosshair>(
get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->find_child("Crosshair"));
DEV_ASSERT(m_Crosshair);
m_HitMarkers = Object::cast_to<HitMarkers>(
get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->get_parent()->find_child("HitMarkers"));
DEV_ASSERT(m_HitMarkers);
}
void BoneCollisionShape::set_head_damage(int a_D) {
m_HeadDamage = a_D;
}
int BoneCollisionShape::get_head_damage() const {
return m_HeadDamage;
}
void BoneCollisionShape::set_body_damage(int a_D) {
m_BodyDamage = a_D;
}
int BoneCollisionShape::get_body_damage() const {
return m_BodyDamage;
}
void BoneCollisionShape::headshot_hit() {
ERR_PRINT("zombie headshot hit");
m_Crosshair->set_dot_color(Color(1, 0, 0, 1));
m_HitMarkers->hsvisible(true);
emit_signal("a_ZombieHeadShotHit", m_HeadDamage);
}
void BoneCollisionShape::bodyshot_hit() {
ERR_PRINT("zombie body hit");
m_HitMarkers->visible(true);
emit_signal("a_ZombieBodyShotHit", m_BodyDamage);
}
} // namespace blitz

34
src/BoneCollisionShape.h Normal file
View File

@@ -0,0 +1,34 @@
#pragma once
#include "Crosshair.h"
#include "HitMarkers.h"
#include <godot_cpp/classes/area3d.hpp>
namespace blitz {
class BoneCollisionShape : public godot::Area3D {
GDCLASS(BoneCollisionShape, godot::Area3D)
protected:
static void _bind_methods();
public:
BoneCollisionShape();
~BoneCollisionShape();
void _ready();
void headshot_hit();
void bodyshot_hit();
private:
int m_HeadDamage = 2;
int m_BodyDamage = 1;
void set_head_damage(int d);
int get_head_damage() const;
void set_body_damage(int d);
int get_body_damage() const;
Crosshair* m_Crosshair;
HitMarkers* m_HitMarkers;
};
} // namespace blitz

73
src/Bullet.cpp Normal file
View File

@@ -0,0 +1,73 @@
#include "Bullet.h"
#include "BoneCollisionShape.h"
#include <godot_cpp/classes/engine.hpp>
using namespace godot;
namespace blitz {
static constexpr float BULLET_SPEED = 40.0f;
void Bullet::_bind_methods() {
ClassDB::bind_method(D_METHOD("_on_timer_timeout"), &Bullet::_on_timer_timeout);
}
Bullet::Bullet() {}
Bullet::~Bullet() {}
void Bullet::_ready() {
m_Particles = Object::cast_to<GPUParticles3D>(find_child("GPUParticles3D"));
DEV_ASSERT(m_Particles);
m_Ray = Object::cast_to<RayCast3D>(find_child("RayCast3D"));
DEV_ASSERT(m_Ray);
m_Mesh = Object::cast_to<MeshInstance3D>(find_child("MeshInstance3D"));
DEV_ASSERT(m_Mesh);
m_Timer = memnew(Timer);
add_child(m_Timer);
m_Timer->connect("timeout", callable_mp(this, &Bullet::_on_timer_timeout));
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
#endif
m_Crosshair = Object::cast_to<Crosshair>(get_parent()->find_child("Crosshair"));
DEV_ASSERT(m_Crosshair);
m_HitMarkers = Object::cast_to<HitMarkers>(get_parent()->find_child("HitMarkers"));
DEV_ASSERT(m_HitMarkers);
}
void Bullet::_physics_process(float a_Delta) {
Vector3 movement = Vector3(0, 0, -BULLET_SPEED) * a_Delta;
set_position(get_transform().xform(movement));
if (m_Ray->is_colliding()) {
m_Mesh->set_visible(false);
m_Particles->set_emitting(true);
m_Ray->set_enabled(false);
Object* collider = m_Ray->get_collider();
if (!collider)
return;
auto* area_collider = Object::cast_to<BoneCollisionShape>(collider);
if (!(area_collider && area_collider->is_in_group("enemy")))
return;
const StringName& area_name = area_collider->get_name();
area_collider->call(area_name.match("HeadArea3D") ? "headshot_hit" : "bodyshot_hit");
m_Timer->set_wait_time(1.0);
m_Timer->start();
}
}
void Bullet::_on_timer_timeout() {
m_Crosshair->set_dot_color(Color(1, 1, 1, 1));
m_HitMarkers->visible();
m_HitMarkers->hsvisible();
queue_free();
}
} // namespace blitz

35
src/Bullet.h Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include "Crosshair.h"
#include "HitMarkers.h"
#include <godot_cpp/classes/gpu_particles3d.hpp>
#include <godot_cpp/classes/mesh_instance3d.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/ray_cast3d.hpp>
#include <godot_cpp/classes/timer.hpp>
namespace blitz {
class Bullet : public godot::Node3D {
GDCLASS(Bullet, godot::Node3D)
protected:
static void _bind_methods();
public:
Bullet();
~Bullet();
void _ready();
void _physics_process(float delta);
private:
godot::GPUParticles3D* m_Particles;
godot::RayCast3D* m_Ray;
godot::MeshInstance3D* m_Mesh;
godot::Timer* m_Timer;
Crosshair* m_Crosshair;
HitMarkers* m_HitMarkers;
void _on_timer_timeout();
};
} // namespace blitz

70
src/Crosshair.cpp Normal file
View File

@@ -0,0 +1,70 @@
#include "Crosshair.h"
using namespace godot;
namespace blitz {
void Crosshair::_bind_methods() {}
Crosshair::Crosshair() :
m_DotRadius(1.0f),
m_DotColor(Color(1, 1, 1, 1)),
m_CrosshairSpeed(0.25f),
m_CrosshairDistance(2.0f),
m_Origin(Vector3(0, 0, 0)),
m_Pos(Vector2(0, 0)) {}
Crosshair::~Crosshair() {}
void Crosshair::_ready() {
// Catch the parent node directly without SubViewport problem in the editor thx to "recursivity" (if we go up the tree one more
// notch its bugging)
Node* root_node = get_parent();
while (root_node != nullptr && !root_node->is_class("FirstPersonPlayer")) {
root_node = root_node->get_parent();
}
if (root_node != nullptr && root_node->is_class("FirstPersonPlayer")) {
m_Player = Object::cast_to<FirstPersonPlayer>(root_node);
DEV_ASSERT(m_Player);
} else {
ERR_PRINT("FirstPersonPlayer not found in parent hierarchy.");
}
m_CrosshairLines = {Object::cast_to<Line2D>(find_child("Top")), Object::cast_to<Line2D>(find_child("Right")),
Object::cast_to<Line2D>(find_child("Bottom")), Object::cast_to<Line2D>(find_child("Left"))};
for (size_t i = 0; i < m_CrosshairLines.size(); i++) {
DEV_ASSERT(m_CrosshairLines[i]);
}
queue_redraw();
}
void Crosshair::_process(float a_Delta) {
adjust_crosshair_lines();
}
void Crosshair::_draw() {
draw_circle(Vector2(20, 20), m_DotRadius, m_DotColor);
}
void Crosshair::adjust_crosshair_lines() {
Vector3 vel = m_Player->get_real_velocity();
float speed = m_Origin.distance_to(vel);
m_CrosshairLines[0]->set_position(
m_CrosshairLines[0]->get_position().lerp(m_Pos + Vector2(0, -speed * m_CrosshairDistance), m_CrosshairSpeed));
m_CrosshairLines[1]->set_position(
m_CrosshairLines[1]->get_position().lerp(m_Pos + Vector2(speed * m_CrosshairDistance, 0), m_CrosshairSpeed));
m_CrosshairLines[2]->set_position(
m_CrosshairLines[2]->get_position().lerp(m_Pos + Vector2(0, speed * m_CrosshairDistance), m_CrosshairSpeed));
m_CrosshairLines[3]->set_position(
m_CrosshairLines[3]->get_position().lerp(m_Pos + Vector2(-speed * m_CrosshairDistance, 0), m_CrosshairSpeed));
}
void Crosshair::set_dot_color(Color a_Color) {
m_DotColor = a_Color;
queue_redraw();
}
} // namespace blitz

35
src/Crosshair.h Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include "FirstPersonPlayer.h"
#include <godot_cpp/classes/center_container.hpp>
#include <godot_cpp/classes/line2d.hpp>
namespace blitz {
class Crosshair : public godot::CenterContainer {
GDCLASS(Crosshair, godot::CenterContainer)
protected:
static void _bind_methods();
public:
Crosshair();
~Crosshair();
void _ready();
void _process(float delta);
void _draw();
void set_dot_color(godot::Color col);
private:
godot::Color m_DotColor;
float m_DotRadius;
std::array<godot::Line2D*, 4> m_CrosshairLines;
FirstPersonPlayer* m_Player;
float m_CrosshairSpeed;
float m_CrosshairDistance;
godot::Vector3 m_Origin;
godot::Vector2 m_Pos;
void adjust_crosshair_lines();
};
} // namespace blitz

View File

@@ -1,12 +1,12 @@
#include "FirstPersonPlayer.h" #include "FirstPersonPlayer.h"
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/engine.hpp> #include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/input.hpp> #include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/input_event_mouse_motion.hpp> #include <godot_cpp/classes/input_event_mouse_motion.hpp>
#include <godot_cpp/classes/input_map.hpp> #include <godot_cpp/classes/input_map.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/core/math.hpp> #include <godot_cpp/core/math.hpp>
#include <godot_cpp/variant/utility_functions.hpp> #include <godot_cpp/variant/signal.hpp>
using namespace godot; using namespace godot;
@@ -21,6 +21,8 @@ static constexpr float GRAVITY = 9.81f;
static constexpr float SENSITIVITY = 0.003f; static constexpr float SENSITIVITY = 0.003f;
static constexpr float HIT_STAGGER = 8.0f;
static constexpr float BOB_FREQ = 2.0f; static constexpr float BOB_FREQ = 2.0f;
static constexpr float BOB_AMP = 0.08f; static constexpr float BOB_AMP = 0.08f;
@@ -34,10 +36,10 @@ static constexpr float FOV_TRANSITION = 8.0f;
static constexpr float MIN_FOV_VELOCITY = 0.5; static constexpr float MIN_FOV_VELOCITY = 0.5;
static constexpr float MAX_FOV_VELOCITY = SPRINT_SPEED * 2.0f; static constexpr float MAX_FOV_VELOCITY = SPRINT_SPEED * 2.0f;
static const float LerpValue = 0.10;
static const float AnimationBlend = 7.0;
void FirstPersonPlayer::_bind_methods() {} void FirstPersonPlayer::_bind_methods() {
ADD_SIGNAL(MethodInfo("a_PlayerHit"));
}
FirstPersonPlayer::FirstPersonPlayer() : m_BobTime(0) {} FirstPersonPlayer::FirstPersonPlayer() : m_BobTime(0) {}
@@ -49,9 +51,17 @@ void FirstPersonPlayer::_ready() {
Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED); Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
} }
m_Head = Object::cast_to<Node3D>(find_child("Head")); m_Head = Object::cast_to<Node3D>(find_child("Head"));
DEV_ASSERT(m_Head);
m_Camera = Object::cast_to<Camera3D>(m_Head->find_child("Camera")); m_Camera = Object::cast_to<Camera3D>(m_Head->find_child("Camera"));
m_AnimationTree = Object::cast_to<AnimationTree>(find_child("AnimationTree")); DEV_ASSERT(m_Camera);
m_Mesh = Object::cast_to<Node3D>(find_child("Mesh")); m_WeaponAnimation = Object::cast_to<AnimationPlayer>(find_child("AnimationPlayer"));
DEV_ASSERT(m_WeaponAnimation);
m_GunBarrel = Object::cast_to<RayCast3D>(find_child("RayCast3D"));
DEV_ASSERT(m_GunBarrel);
m_BulletScene = ResourceLoader::get_singleton()->load("res://Scenes/Weapons/bullet.tscn");
if (!m_BulletScene.is_valid()) {
ERR_PRINT("Failed to load bullet scene.");
}
} }
void FirstPersonPlayer::_unhandled_input(const godot::Ref<godot::InputEvent>& a_Event) { void FirstPersonPlayer::_unhandled_input(const godot::Ref<godot::InputEvent>& a_Event) {
@@ -89,7 +99,15 @@ void FirstPersonPlayer::_physics_process(float a_Delta) {
UpdateFOV(a_Delta); UpdateFOV(a_Delta);
UpdateBobbing(a_Delta); UpdateBobbing(a_Delta);
UpdateAnimation(a_Delta); if (Input->is_action_pressed("shoot")) {
if (!m_WeaponAnimation->is_playing()) {
m_WeaponAnimation->play("Shoot");
m_BulletInstance = m_BulletScene->instantiate();
m_BulletInstance->set("position", m_GunBarrel->get_global_transform().origin);
m_BulletInstance->set("rotation", m_GunBarrel->get_global_transform().basis.get_euler());
get_parent()->add_child(m_BulletInstance);
}
}
move_and_slide(); move_and_slide();
} }
@@ -105,11 +123,10 @@ void FirstPersonPlayer::UpdateBobbing(float a_Delta) {
void FirstPersonPlayer::UpdateCamera(const InputEventMouseMotion& a_Event) { void FirstPersonPlayer::UpdateCamera(const InputEventMouseMotion& a_Event) {
m_Head->rotate_y(-a_Event.get_relative().x * SENSITIVITY); m_Head->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
m_Mesh->rotate_y(-a_Event.get_relative().x * SENSITIVITY);
m_Camera->rotate_x(-a_Event.get_relative().y * SENSITIVITY); m_Camera->rotate_x(-a_Event.get_relative().y * SENSITIVITY);
float rotationX = m_Camera->get_rotation().x; float rotationX = m_Camera->get_rotation().x;
rotationX = CLAMP(rotationX, Math::deg_to_rad(-80.0), Math::deg_to_rad(80.0)); CLAMP(rotationX, Math::deg_to_rad(-40.0), Math::deg_to_rad(60.0));
m_Camera->set_rotation({rotationX, get_rotation().y, get_rotation().z}); m_Camera->set_rotation({rotationX, get_rotation().y, get_rotation().z});
} }
@@ -136,14 +153,6 @@ void FirstPersonPlayer::UpdatePosition(float delta) {
Math::lerp(static_cast<float>(get_velocity().z), static_cast<float>(direction.z * m_Speed), Math::lerp(static_cast<float>(get_velocity().z), static_cast<float>(direction.z * m_Speed),
static_cast<float>(delta * AIR_MOVEMENT))}); static_cast<float>(delta * AIR_MOVEMENT))});
} }
if (!direction.is_zero_approx()) {
godot::Vector3 newRotation = m_Mesh->get_rotation();
newRotation.y = godot::UtilityFunctions::lerp_angle(
newRotation.y, godot::UtilityFunctions::atan2(get_velocity().x, get_velocity().z), LerpValue);
m_Mesh->set_rotation(newRotation);
}
} }
void FirstPersonPlayer::UpdateFOV(float a_Delta) { void FirstPersonPlayer::UpdateFOV(float a_Delta) {
@@ -152,25 +161,9 @@ void FirstPersonPlayer::UpdateFOV(float a_Delta) {
m_Camera->set_fov(Math::lerp(m_Camera->get_fov(), targetFOV, a_Delta * FOV_TRANSITION)); m_Camera->set_fov(Math::lerp(m_Camera->get_fov(), targetFOV, a_Delta * FOV_TRANSITION));
} }
void FirstPersonPlayer::UpdateAnimation(float delta) { void FirstPersonPlayer::hit(Vector3 a_Dir) {
if (is_on_floor()) { emit_signal("a_PlayerHit");
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "grounded"); this->set_velocity(a_Dir * HIT_STAGGER);
if (get_velocity().length() > 0.2f) {
if (m_Speed == SPRINT_SPEED) {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 1.0, delta * AnimationBlend));
} else {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), 0.0, delta * AnimationBlend));
}
} else {
m_AnimationTree->set("parameters/iwr_blend/blend_amount",
UtilityFunctions::lerp(m_AnimationTree->get("parameters/iwr_blend/blend_amount"), -1.0, delta * AnimationBlend));
}
} else {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air");
}
} }
} // namespace blitz } // namespace blitz

View File

@@ -1,11 +1,17 @@
#pragma once #pragma once
#include "Player.h" #include <godot_cpp/classes/animation_player.hpp>
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/input_event_mouse_motion.hpp> #include <godot_cpp/classes/input_event_mouse_motion.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/ray_cast3d.hpp>
#include <godot_cpp/classes/ref.hpp>
namespace blitz { namespace blitz {
class FirstPersonPlayer : public Player { class FirstPersonPlayer : public godot::CharacterBody3D {
GDCLASS(FirstPersonPlayer, godot::CharacterBody3D) GDCLASS(FirstPersonPlayer, godot::CharacterBody3D)
protected: protected:
static void _bind_methods(); static void _bind_methods();
@@ -17,11 +23,16 @@ class FirstPersonPlayer : public Player {
// Godot overrides // Godot overrides
void _unhandled_input(const godot::Ref<godot::InputEvent>&); void _unhandled_input(const godot::Ref<godot::InputEvent>&);
void _physics_process(float delta); void _physics_process(float delta);
void _ready() override; void _ready();
void hit(godot::Vector3 dir);
private: private:
godot::Camera3D* m_Camera; godot::Camera3D* m_Camera;
godot::Node3D* m_Head; godot::Node3D* m_Head;
godot::AnimationPlayer* m_WeaponAnimation;
godot::RayCast3D* m_GunBarrel;
godot::Node* m_BulletInstance;
godot::Ref<godot::PackedScene> m_BulletScene;
float m_BobTime; float m_BobTime;
float m_Speed; float m_Speed;
@@ -29,8 +40,7 @@ class FirstPersonPlayer : public Player {
void UpdateFOV(float delta); void UpdateFOV(float delta);
void UpdateCamera(const godot::InputEventMouseMotion&); void UpdateCamera(const godot::InputEventMouseMotion&);
void UpdatePosition(float delta); void UpdatePosition(float delta);
void UpdateAnimation(float delta);
}; };
} // namespace blitz } // namespace blitz

37
src/HitMarkers.cpp Normal file
View File

@@ -0,0 +1,37 @@
#include "HitMarkers.h"
using namespace godot;
namespace blitz {
void HitMarkers::_bind_methods() {}
HitMarkers::HitMarkers() {}
HitMarkers::~HitMarkers() {}
void HitMarkers::_ready() {
m_HitMarkerLines = {Object::cast_to<Line2D>(find_child("TopRight")), Object::cast_to<Line2D>(find_child("BottomRight")),
Object::cast_to<Line2D>(find_child("BottomLeft")), Object::cast_to<Line2D>(find_child("TopLeft"))};
for (size_t i = 0; i < m_HitMarkerLines.size(); i++) {
DEV_ASSERT(m_HitMarkerLines[i]);
}
m_HitMarkerHSLines = {Object::cast_to<Line2D>(find_child("TopRightHS")), Object::cast_to<Line2D>(find_child("BottomRightHS")),
Object::cast_to<Line2D>(find_child("BottomLeftHS")), Object::cast_to<Line2D>(find_child("TopLeftHS"))};
for (size_t i = 0; i < m_HitMarkerHSLines.size(); i++) {
DEV_ASSERT(m_HitMarkerHSLines[i]);
}
this->visible();
}
void HitMarkers::visible(bool a_Bool) {
for (size_t i = 0; i < m_HitMarkerLines.size(); i++) {
m_HitMarkerLines[i]->set_visible(a_Bool);
}
}
void HitMarkers::hsvisible(bool a_Bool) {
for (size_t i = 0; i < m_HitMarkerHSLines.size(); i++) {
m_HitMarkerHSLines[i]->set_visible(a_Bool);
}
}
} // namespace blitz

25
src/HitMarkers.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include <godot_cpp/classes/center_container.hpp>
#include <godot_cpp/classes/line2d.hpp>
namespace blitz {
class HitMarkers : public godot::CenterContainer {
GDCLASS(HitMarkers, godot::CenterContainer)
protected:
static void _bind_methods();
public:
HitMarkers();
~HitMarkers();
void _ready();
void visible(bool visiblity = false);
void hsvisible(bool visiblity = false);
private:
std::array<godot::Line2D*, 4> m_HitMarkerLines;
std::array<godot::Line2D*, 4> m_HitMarkerHSLines;
};
} // namespace blitz

View File

@@ -1,99 +0,0 @@
#include "Lobby.h"
#include <godot_cpp/classes/e_net_multiplayer_peer.hpp>
#include <godot_cpp/classes/multiplayer_api.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/variant/dictionary.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
using namespace godot;
static const char ServerScenePath[] = "res://Scenes/Network/server.tscn";
namespace blitz {
void Lobby::_bind_methods() {
godot::ClassDB::bind_method(godot::D_METHOD("create_game", "port", "dedicated"), &Lobby::CreateGame);
godot::ClassDB::bind_method(godot::D_METHOD("join_game", "address", "port"), &Lobby::JoinGame);
ADD_SIGNAL(MethodInfo("player_connected", PropertyInfo(Variant::INT, "peer_id")));
ADD_SIGNAL(MethodInfo("player_disconnected", PropertyInfo(Variant::INT, "peer_id")));
ADD_SIGNAL(MethodInfo("server_disconnected"));
ADD_SIGNAL(MethodInfo("local_player_connected"));
}
Lobby::Lobby() {}
Lobby::~Lobby() {}
void Lobby::_ready() {
get_multiplayer()->connect("peer_connected", callable_mp(this, &Lobby::OnPlayerConnected));
get_multiplayer()->connect("peer_disconnected", callable_mp(this, &Lobby::OnPlayerDisconnected));
get_multiplayer()->connect("connected_to_server", callable_mp(this, &Lobby::OnConnectOk));
get_multiplayer()->connect("connection_failed", callable_mp(this, &Lobby::OnConnectFail));
get_multiplayer()->connect("server_disconnected", callable_mp(this, &Lobby::OnServerDisconnected));
}
Error Lobby::JoinGame(const String& a_Address, uint16_t a_Port) {
auto* peer = memnew(ENetMultiplayerPeer);
Error error = peer->create_client(a_Address, a_Port);
if (error)
return error;
get_multiplayer()->set_multiplayer_peer(peer);
return Error::OK;
}
Error Lobby::CreateGame(uint16_t a_Port, bool a_Dedicated) {
auto* peer = memnew(ENetMultiplayerPeer);
Error error = peer->create_server(a_Port);
if (error)
return error;
get_multiplayer()->set_multiplayer_peer(peer);
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(ServerScenePath);
add_child(serverScene->instantiate());
if (!a_Dedicated) {
emit_signal("local_player_connected");
emit_signal("player_connected", get_multiplayer()->get_unique_id());
}
return Error::OK;
}
void Lobby::Shutdown() {
get_multiplayer()->set_multiplayer_peer(nullptr);
if (auto* server = find_child("Server")) {
remove_child(server);
}
}
void Lobby::OnPlayerConnected(PeerID a_PeerId) {
if (get_multiplayer()->is_server()) {
emit_signal("player_connected", a_PeerId);
}
}
void Lobby::OnPlayerDisconnected(PeerID a_PeerId) {
if (get_multiplayer()->is_server()) {
emit_signal("player_disconnected", a_PeerId);
}
}
void Lobby::OnConnectOk() {
int32_t peerId = get_multiplayer()->get_unique_id();
emit_signal("local_player_connected");
}
void Lobby::OnConnectFail() {
Shutdown();
}
void Lobby::OnServerDisconnected() {
Shutdown();
emit_signal("server_disconnected");
}
} // namespace blitz

View File

@@ -1,32 +0,0 @@
#pragma once
#include <godot_cpp/classes/node.hpp>
#include "NetworkInterface.h"
namespace blitz {
class Lobby : public godot::Node {
GDCLASS(Lobby, godot::Node)
protected:
static void _bind_methods();
public:
Lobby();
~Lobby();
void _ready() override;
godot::Error JoinGame(const godot::String& a_Address, uint16_t a_Port);
godot::Error CreateGame(uint16_t a_Port, bool a_Dedicated = false);
void Shutdown();
private:
void OnPlayerConnected(PeerID a_PeerId);
void OnPlayerDisconnected(PeerID a_PeerId);
void OnConnectOk();
void OnConnectFail();
void OnServerDisconnected();
};
} // namespace blitz

View File

@@ -1,33 +0,0 @@
#include "Main.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/window.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
#include "Lobby.h"
#include "World.h"
using namespace godot;
namespace blitz {
static constexpr char MainScenePath[] = "res://Scenes/Levels/world.tscn";
void Main::_bind_methods() {
godot::ClassDB::bind_method(godot::D_METHOD("change_scene"), &Main::ChangeScene);
}
Main::Main() {}
Main::~Main() {}
void Main::ChangeScene() {
Ref<PackedScene> sceneData = ResourceLoader::get_singleton()->load(MainScenePath);
World* world = Object::cast_to<World>(sceneData->instantiate());
add_child(world);
}
} // namespace blitz

View File

@@ -1,19 +0,0 @@
#pragma once
#include <godot_cpp/classes/node.hpp>
namespace blitz {
class Main : public godot::Node {
GDCLASS(Main, godot::Node)
protected:
static void _bind_methods();
public:
Main();
~Main();
void ChangeScene();
};
} // namespace blitz

View File

@@ -1,55 +0,0 @@
#include "MainMenu.h"
#include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
using namespace godot;
namespace blitz {
void MainMenu::_bind_methods() {
godot::ClassDB::bind_method(godot::D_METHOD("on_connected"), &MainMenu::OnConnected);
ADD_SIGNAL(MethodInfo("create_game", PropertyInfo(Variant::INT, "port"), PropertyInfo(Variant::BOOL, "dedicated")));
ADD_SIGNAL(MethodInfo("join_game", PropertyInfo(Variant::STRING, "address"), PropertyInfo(Variant::INT, "port")));
ADD_SIGNAL(MethodInfo("change_scene"));
}
MainMenu::MainMenu() {}
MainMenu::~MainMenu() {}
void MainMenu::_ready() {
Node* container = find_child("Container");
DEV_ASSERT(container);
m_JoinButton = Object::cast_to<Button>(container->find_child("JoinButton"));
m_CreateButton = Object::cast_to<Button>(container->find_child("CreateButton"));
m_QuitButton = Object::cast_to<Button>(container->find_child("QuitButton"));
DEV_ASSERT(m_JoinButton);
DEV_ASSERT(m_CreateButton);
DEV_ASSERT(m_QuitButton);
m_JoinButton->connect("pressed", callable_mp(this, &MainMenu::OnJoinPressed));
m_CreateButton->connect("pressed", callable_mp(this, &MainMenu::OnCreatePressed));
m_QuitButton->connect("pressed", callable_mp(this, &MainMenu::OnQuitPressed));
}
void MainMenu::OnConnected() {
emit_signal("change_scene");
set_visible(false);
}
void MainMenu::OnJoinPressed() {
emit_signal("join_game", "localhost", 25565);
}
void MainMenu::OnCreatePressed() {
emit_signal("create_game", 25565, false);
}
void MainMenu::OnQuitPressed() {
get_tree()->quit();
}
} // namespace blitz

View File

@@ -1,32 +0,0 @@
#pragma once
#include <godot_cpp/classes/button.hpp>
#include <godot_cpp/classes/control.hpp>
namespace blitz {
class MainMenu : public godot::Control {
GDCLASS(MainMenu, godot::Control)
protected:
static void _bind_methods();
public:
MainMenu();
~MainMenu();
// Godot overrides
void _ready() override;
private:
godot::Button* m_JoinButton;
godot::Button* m_CreateButton;
godot::Button* m_QuitButton;
void OnConnected();
void OnJoinPressed();
void OnCreatePressed();
void OnQuitPressed();
};
} // namespace blitz

View File

@@ -1,50 +0,0 @@
#include "NetworkInterface.h"
#include <blitz/protocol/PacketFactory.h>
#include <blitz/protocol/PacketSerializer.h>
#include <godot_cpp/classes/multiplayer_api.hpp>
#include <godot_cpp/classes/multiplayer_peer.hpp>
#include <godot_cpp/variant/utility_functions.hpp>
namespace blitz {
using namespace godot;
void NetworkInterface::_bind_methods() {
ClassDB::bind_method(D_METHOD("RecievePacketDataReliable", "a_PacketData"), &NetworkInterface::RecievePacketDataReliable);
protocol::PacketFactory::Init();
}
NetworkInterface::NetworkInterface() {}
NetworkInterface::~NetworkInterface() {}
void NetworkInterface::_ready() {
// TODO: unreliable
Dictionary config;
config["rpc_mode"] = MultiplayerAPI::RPC_MODE_ANY_PEER;
config["transfer_mode"] = MultiplayerPeer::TRANSFER_MODE_RELIABLE;
config["call_local"] = true;
config["channel"] = 0;
rpc_config("RecievePacketDataReliable", config);
}
void NetworkInterface::BroadcastPacket(const protocol::Packet& a_Packet) {
PackedByteArray byteArray = protocol::PacketSerializer::Serialize(a_Packet);
rpc("RecievePacketDataReliable", byteArray);
}
void NetworkInterface::SendPacket(PeerID a_Peer, const protocol::Packet& a_Packet) {
PackedByteArray byteArray = protocol::PacketSerializer::Serialize(a_Packet);
rpc_id(a_Peer, "RecievePacketDataReliable", byteArray);
}
void NetworkInterface::RecievePacketDataReliable(godot::PackedByteArray a_PacketData) {
auto packet = protocol::PacketSerializer::Deserialize(a_PacketData);
if (packet) {
packet->m_Sender = get_multiplayer()->get_remote_sender_id();
Dispatch(*packet);
}
}
} // namespace blitz

View File

@@ -1,26 +0,0 @@
#pragma once
#include <blitz/protocol/Packets.h>
#include <godot_cpp/classes/node.hpp>
#include <blitz/protocol/PacketDispatcher.h>
namespace blitz {
class NetworkInterface : public godot::Node, public protocol::PacketDispatcher {
GDCLASS(NetworkInterface, godot::Node)
protected:
static void _bind_methods();
public:
NetworkInterface();
~NetworkInterface();
void BroadcastPacket(const protocol::Packet& a_Packet);
void SendPacket(PeerID a_Peer, const protocol::Packet& a_Packet);
void _ready() override;
private:
void RecievePacketDataReliable(godot::PackedByteArray a_PacketData);
};
} // namespace blitz

View File

@@ -16,19 +16,18 @@ static const float AnimationBlend = 7.0;
namespace blitz { namespace blitz {
using namespace godot;
void Player::_bind_methods() {} void Player::_bind_methods() {}
Player::Player() {} Player::Player() {}
Player::~Player() {} Player::~Player() {}
void Player::_ready() { void Player::_ready() {
godot::InputMap::get_singleton()->load_from_project_settings(); godot::InputMap::get_singleton()->load_from_project_settings();
m_Mesh = Object::cast_to<godot::Node3D>(find_child("Mesh")); m_PlayerMesh = Object::cast_to<godot::Node3D>(get_child(0));
m_AnimationTree = Object::cast_to<godot::AnimationTree>(find_child("AnimationTree")); m_SpringArmPivot = Object::cast_to<godot::Node3D>(get_child(2));
DEV_ASSERT(m_Mesh); m_AnimationTree = Object::cast_to<godot::AnimationTree>(get_child(4));
DEV_ASSERT(m_PlayerMesh);
DEV_ASSERT(m_SpringArmPivot);
DEV_ASSERT(m_AnimationTree); DEV_ASSERT(m_AnimationTree);
apply_floor_snap(); apply_floor_snap();
@@ -39,6 +38,50 @@ void Player::_physics_process(float delta) {
if (godot::Engine::get_singleton()->is_editor_hint()) if (godot::Engine::get_singleton()->is_editor_hint())
return; return;
auto* Input = godot::Input::get_singleton();
godot::Vector3 move_direction{0, 0, 0};
move_direction.x = Input->get_action_strength("move_right") - Input->get_action_strength("move_left");
move_direction.z = Input->get_action_strength("move_backwards") - Input->get_action_strength("move_forwards");
move_direction = move_direction.rotated({0, 1, 0}, m_SpringArmPivot->get_rotation().y);
godot::Vector3 newVelocity = get_velocity();
newVelocity.y -= Gravity * delta;
set_velocity(newVelocity);
if (Input->is_action_pressed("run"))
m_Speed = RunSpeed;
else
m_Speed = WalkSpeed;
newVelocity = get_velocity();
newVelocity.x = move_direction.x * m_Speed;
newVelocity.z = move_direction.z * m_Speed;
set_velocity(newVelocity);
if (move_direction != godot::Vector3{0, 0, 0}) {
godot::Vector3 newRotation = m_PlayerMesh->get_rotation();
newRotation.y = godot::UtilityFunctions::lerp_angle(
newRotation.y, godot::UtilityFunctions::atan2(get_velocity().x, get_velocity().z), LerpValue);
m_PlayerMesh->set_rotation(newRotation);
}
bool justLanded = is_on_floor() && m_SnapVector == godot::Vector3{0, 0, 0};
bool isJumping = is_on_floor() && Input->is_action_just_pressed("jump");
if (isJumping) {
newVelocity = get_velocity();
newVelocity.y = JumpStrength;
set_velocity(newVelocity);
m_SnapVector.zero();
} else if (justLanded) {
m_SnapVector = {0, -1, 0};
}
apply_floor_snap();
move_and_slide();
animate(delta); animate(delta);
} }
@@ -65,13 +108,4 @@ void Player::animate(float delta) {
m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air"); m_AnimationTree->set("parameters/ground_air_transition/transition_request", "air");
} }
} }
Vector3 Player::GetCameraRotation() const {
return m_Mesh->get_rotation();
}
void Player::SetCameraRotation(const Vector3& a_Rotation) {
m_Mesh->set_rotation(a_Rotation);
}
} // namespace blitz } // namespace blitz

View File

@@ -3,12 +3,8 @@
#include <godot_cpp/classes/animation_tree.hpp> #include <godot_cpp/classes/animation_tree.hpp>
#include <godot_cpp/classes/character_body3d.hpp> #include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/node3d.hpp> #include <godot_cpp/classes/node3d.hpp>
#include <blitz/common/Types.h>
namespace blitz { namespace blitz {
class World;
class Player : public godot::CharacterBody3D { class Player : public godot::CharacterBody3D {
GDCLASS(Player, godot::CharacterBody3D); GDCLASS(Player, godot::CharacterBody3D);
@@ -24,21 +20,18 @@ class Player : public godot::CharacterBody3D {
void _physics_process(float delta); void _physics_process(float delta);
void animate(float delta); void animate(float delta);
godot::Vector3 GetCameraRotation() const; private:
void SetCameraRotation(const godot::Vector3& a_Rotation); godot::Node3D* m_PlayerMesh;
godot::Node3D* m_SpringArmPivot;
PlayerID GetId() const {
return m_PeerId;
}
protected:
godot::Node3D* m_Mesh;
godot::AnimationTree* m_AnimationTree; godot::AnimationTree* m_AnimationTree;
godot::Vector3 m_SnapVector; godot::Vector3 m_SnapVector;
float m_Speed; float m_Speed;
PeerID m_PeerId; /*
@onready var player_mesh : Node3D = $Mesh
@onready var spring_arm_pivot : Node3D = $SpringArmPivot
@onready var animator : AnimationTree = $AnimationTree
friend class World; */
}; };
} // namespace blitz } // namespace blitz

View File

@@ -1,11 +0,0 @@
#pragma once
#include <godot_cpp/variant/string.hpp>
namespace blitz {
struct PlayerInfo {
godot::String m_Name;
};
} // namespace blitz

View File

@@ -1,45 +0,0 @@
#include "Server.h"
#include "Lobby.h"
#include "NetworkInterface.h"
#include <godot_cpp/classes/engine.hpp>
using namespace godot;
namespace blitz {
void Server::_bind_methods() {}
Server::Server() {}
Server::~Server() {}
void Server::_ready() {
if (Engine::get_singleton()->is_editor_hint())
return;
m_Lobby = Object::cast_to<Lobby>(get_parent());
DEV_ASSERT(m_Lobby);
m_NetworkInterface = Object::cast_to<NetworkInterface>(m_Lobby->find_child("NetworkInterface"));
DEV_ASSERT(m_NetworkInterface);
m_Lobby->connect("player_connected", callable_mp(this, &Server::OnPlayerConnect));
m_Lobby->connect("player_disconnected", callable_mp(this, &Server::OnPlayerDisconnect));
}
void Server::OnPlayerConnect(PeerID a_PeerId) {
protocol::PlayerInfo playerInfo{a_PeerId, "whoami"};
for (int i = 0; i < m_Peers.size(); i++) {
m_NetworkInterface->SendPacket(a_PeerId, protocol::packets::PlayerJoin({m_Peers[i], "whoami"}));
}
m_Peers.push_back(a_PeerId);
m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerJoin({playerInfo}));
}
void Server::OnPlayerDisconnect(PeerID a_PeerId) {
m_Peers.erase(a_PeerId);
m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerLeave({a_PeerId}));
}
} // namespace blitz

View File

@@ -1,32 +0,0 @@
#pragma once
#include <godot_cpp/classes/node.hpp>
#include <blitz/common/Types.h>
namespace blitz {
class Lobby;
class NetworkInterface;
class Server : public godot::Node {
GDCLASS(Server, godot::Node)
protected:
static void _bind_methods();
public:
Server();
~Server();
void _ready() override;
void OnPlayerConnect(PeerID a_PeerId);
void OnPlayerDisconnect(PeerID a_PeerId);
private:
Lobby* m_Lobby;
NetworkInterface* m_NetworkInterface;
godot::TypedArray<PeerID> m_Peers;
};
} // namespace blitz

View File

@@ -1,142 +1,87 @@
#include "World.h" #include "World.h"
#include "FirstPersonPlayer.h" #include "FirstPersonPlayer.h"
#include "NetworkInterface.h"
#include "Player.h"
#include <godot_cpp/classes/engine.hpp> #include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/multiplayer_api.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/resource_loader.hpp> #include <godot_cpp/classes/resource_loader.hpp>
#include <godot_cpp/variant/utility_functions.hpp> #include <godot_cpp/variant/utility_functions.hpp>
using namespace godot; using namespace godot;
namespace blitz { namespace blitz {
static const char FirstPersonPlayerScenePath[] = "res://Scenes/Characters/first_person_player.tscn"; void World::_bind_methods() {
static const char PlayerScenePath[] = "res://Scenes/Characters/player.tscn"; ClassDB::bind_method(D_METHOD("_on_first_person_player_hit"), &World::_on_first_person_player_hit);
ClassDB::bind_method(D_METHOD("_on_timer_timeout"), &World::_on_timer_timeout);
void World::_bind_methods() {} ClassDB::bind_method(D_METHOD("_on_zombie_spawn_timer_timeout"), &World::_on_zombie_spawn_timer_timeout);
void World::_ready() {
if (Engine::get_singleton()->is_editor_hint())
return;
m_Players = find_child("Players");
DEV_ASSERT(m_Players);
auto* lobby = get_parent()->find_child("Lobby");
DEV_ASSERT(lobby);
m_NetworkInterface = Object::cast_to<NetworkInterface>(lobby->find_child("NetworkInterface"));
DEV_ASSERT(m_NetworkInterface);
m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerJoin, *this);
m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerLeave, *this);
m_NetworkInterface->RegisterHandler(protocol::PacketType::PlayerPositionAndRotation, *this);
} }
World::World() {} World::World() {}
World::~World() {}
World::~World() { void World::_ready() {
if (Engine::get_singleton()->is_editor_hint()) FirstPersonPlayer* player = Object::cast_to<FirstPersonPlayer>(find_child("FirstPersonPlayer"));
return; DEV_ASSERT(player);
if (player) {
player->connect("a_PlayerHit", callable_mp(this, &World::_on_first_person_player_hit));
}
m_NetworkInterface->UnregisterHandler(*this); m_RedRect = Object::cast_to<ColorRect>(find_child("HitRect"));
DEV_ASSERT(m_RedRect);
m_Timer = memnew(Timer);
add_child(m_Timer);
m_Timer->connect("timeout", callable_mp(this, &World::_on_timer_timeout));
m_ZombieSpawnTimer = memnew(Timer);
add_child(m_ZombieSpawnTimer);
m_ZombieSpawnTimer->connect("timeout", callable_mp(this, &World::_on_zombie_spawn_timer_timeout));
m_Spawns = Object::cast_to<Node3D>(find_child("Spawns"));
DEV_ASSERT(m_Spawns);
m_NavigationRegion = Object::cast_to<NavigationRegion3D>(find_child("NavigationRegion3D"));
DEV_ASSERT(m_NavigationRegion);
m_ZombieScene = ResourceLoader::get_singleton()->load("res://Scenes/Characters/zombie.tscn");
if (!m_ZombieScene.is_valid()) {
ERR_PRINT("Failed to load zombie scene.");
}
m_RandomId.instantiate();
m_RandomId->randomize();
m_ZombieSpawnTimer->start();
} }
void World::_process(float delta) { void World::_on_first_person_player_hit() {
#if DEBUG_ENABLED #if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) if (Engine::get_singleton()->is_editor_hint()) {
return; return;
}
#endif #endif
m_PassedTime += delta; UtilityFunctions::print("Player hit detected in World.");
if (m_PassedTime < 0.05f) m_RedRect->set_visible(true);
m_Timer->set_wait_time(0.2);
m_Timer->start();
}
void World::_on_timer_timeout() {
m_RedRect->set_visible(false);
}
Node* World::get_random_child(Node* a_ParentNode) {
m_ZombieSpawnTimer->set_wait_time(4.0);
if (!a_ParentNode || a_ParentNode->get_child_count() == 0) {
return nullptr;
}
int32_t id = m_RandomId->randi() % a_ParentNode->get_child_count();
return a_ParentNode->get_child(id);
}
void World::_on_zombie_spawn_timer_timeout() {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return; return;
// UtilityFunctions::print(m_PassedTime);
// m_PassedTime -= 0.05f;
// if (m_PassedTime > 0.5f)
// m_PassedTime = 0.0f;
if (get_multiplayer()->is_server()) {
for (int i = 0; i < m_Players->get_child_count(); i++) {
Player* player = Object::cast_to<Player>(m_Players->get_child(i));
DEV_ASSERT(player);
m_NetworkInterface->BroadcastPacket(
protocol::packets::PlayerPositionAndRotation({player->GetId(), player->get_position(), player->GetCameraRotation()}));
}
} else {
Player* player = GetPlayerById(get_multiplayer()->get_unique_id());
if (player) {
m_NetworkInterface->BroadcastPacket(protocol::packets::PlayerPositionAndRotation(
{get_multiplayer()->get_unique_id(), player->get_position(), player->GetCameraRotation()}));
}
}
}
Player* World::GetPlayerById(PlayerID a_PlayerId) {
String stringId = UtilityFunctions::var_to_str(a_PlayerId);
for (int i = 0; i < m_Players->get_child_count(); i++) {
Node* player = m_Players->get_child(i);
if (player->get_name() == stringId) {
return Object::cast_to<Player>(player);
}
}
return nullptr;
}
void World::HandlePacket(const protocol::packets::PlayerJoin& a_PlayerJoin) {
const protocol::PlayerInfo& playerInfo = a_PlayerJoin.m_Data.m_Player;
AddPlayer(playerInfo.m_PlayerId, playerInfo.m_PlayerName);
}
void World::HandlePacket(const protocol::packets::PlayerLeave& a_PlayerLeave) {
RemovePlayer(a_PlayerLeave.m_Data.m_PlayerId);
}
void World::HandlePacket(const protocol::packets::PlayerPositionAndRotation& a_PlayerPos) {
const auto& data = a_PlayerPos.m_Data;
if (data.m_Player == get_multiplayer()->get_unique_id() || data.m_Player != a_PlayerPos.m_Sender)
return;
SetPlayerPositionAndRotation(data.m_Player, data.m_Position, data.m_Rotation);
}
void World::AddPlayer(PlayerID a_PlayerId, String a_PlayerName) {
UtilityFunctions::print("New Player with id : ", a_PlayerId, " and name ", a_PlayerName);
if (a_PlayerId == get_multiplayer()->get_unique_id()) {
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(FirstPersonPlayerScenePath);
FirstPersonPlayer* player = Object::cast_to<FirstPersonPlayer>(serverScene->instantiate());
player->set_name(UtilityFunctions::var_to_str(a_PlayerId));
player->m_PeerId = a_PlayerId;
m_Players->add_child(player);
} else {
Ref<PackedScene> serverScene = ResourceLoader::get_singleton()->load(PlayerScenePath);
Player* player = Object::cast_to<Player>(serverScene->instantiate());
player->set_name(UtilityFunctions::var_to_str(a_PlayerId));
player->m_PeerId = a_PlayerId;
m_Players->add_child(player);
}
}
void World::RemovePlayer(PlayerID a_PlayerId) {
UtilityFunctions::print("Removing Player with id : ", a_PlayerId);
Player* player = GetPlayerById(a_PlayerId);
if (player) {
player->queue_free();
}
}
void World::SetPlayerPositionAndRotation(PlayerID a_PlayerId, const Vector3& a_Position, const Vector3& a_Rotation) {
Player* player = GetPlayerById(a_PlayerId);
if (player) {
player->set_position(a_Position);
player->SetCameraRotation(a_Rotation);
} }
#endif
auto spawns = Object::cast_to<Node3D>(World::get_random_child(m_Spawns));
auto spawn_points = spawns->get_global_position();
m_ZombieInstance = Object::cast_to<Zombie>(m_ZombieScene->instantiate());
DEV_ASSERT(m_ZombieInstance);
m_ZombieInstance->set_position(spawn_points);
m_NavigationRegion->add_child(m_ZombieInstance);
} }
} // namespace blitz } // namespace blitz

View File

@@ -1,16 +1,18 @@
#pragma once #pragma once
#include <godot_cpp/classes/node3d.hpp> #include "Zombie.h"
#include <godot_cpp/classes/color_rect.hpp>
#include <blitz/protocol/PacketHandler.h> #include <godot_cpp/classes/navigation_region3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/random_number_generator.hpp>
#include <godot_cpp/classes/ref.hpp>
#include <godot_cpp/classes/timer.hpp>
namespace blitz { namespace blitz {
class World : public godot::Node3D {
class Player; GDCLASS(World, godot::Node3D);
class NetworkInterface;
class World : public godot::Node3D, public protocol::PacketHandler {
GDCLASS(World, godot::Node3D)
protected: protected:
static void _bind_methods(); static void _bind_methods();
@@ -18,24 +20,21 @@ class World : public godot::Node3D, public protocol::PacketHandler {
World(); World();
~World(); ~World();
// Godot overrides void _ready();
void _ready() override;
void _process(float delta);
Player* GetPlayerById(PlayerID a_PlayerId);
void HandlePacket(const protocol::packets::PlayerJoin&) override;
void HandlePacket(const protocol::packets::PlayerLeave&) override;
void HandlePacket(const protocol::packets::PlayerPositionAndRotation&) override;
private: private:
NetworkInterface* m_NetworkInterface; godot::ColorRect* m_RedRect;
godot::Node* m_Players; godot::Timer* m_Timer;
float m_PassedTime; godot::Timer* m_ZombieSpawnTimer;
godot::Node3D* m_Spawns;
godot::NavigationRegion3D* m_NavigationRegion;
Zombie* m_ZombieInstance;
godot::Ref<godot::PackedScene> m_ZombieScene;
godot::Ref<godot::RandomNumberGenerator> m_RandomId;
void _on_timer_timeout();
void AddPlayer(PlayerID a_PlayerId, godot::String a_PlayerName); void _on_zombie_spawn_timer_timeout();
void RemovePlayer(PlayerID a_PlayerId); void _on_first_person_player_hit();
void SetPlayerPositionAndRotation(PlayerID a_PlayerId, const godot::Vector3& a_Position, const godot::Vector3& a_Rotation); Node* get_random_child(Node* parent_node);
}; };
} // namespace blitz } // namespace blitz

134
src/Zombie.cpp Normal file
View File

@@ -0,0 +1,134 @@
#include "Zombie.h"
#include <godot_cpp/classes/engine.hpp>
#include <godot_cpp/classes/scene_tree.hpp>
#include <godot_cpp/classes/window.hpp>
#include <godot_cpp/core/math.hpp>
using namespace godot;
namespace blitz {
static constexpr float SPEED = 2.0f;
static constexpr float ATTACK_RANGE = 2.0f;
void Zombie::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_m_PlayerPath", "m_PlayerPath"), &Zombie::set_m_PlayerPath);
ClassDB::bind_method(D_METHOD("get_m_PlayerPath"), &Zombie::get_m_PlayerPath);
ClassDB::bind_method(D_METHOD("hit_finished"), &Zombie::hit_finished);
ClassDB::bind_method(D_METHOD("_on_area_3d_a_zombie_head_shot_hit", "m_Health"), &Zombie::_on_area_3d_a_zombie_head_shot_hit);
ClassDB::bind_method(D_METHOD("_on_area_3d_a_zombie_body_shot_hit", "m_Health"), &Zombie::_on_area_3d_a_zombie_body_shot_hit);
ClassDB::bind_method(D_METHOD("_on_timer_timeout"), &Zombie::_on_timer_timeout);
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "m_PlayerPath"), "set_m_PlayerPath", "get_m_PlayerPath");
}
Zombie::Zombie() {}
Zombie::~Zombie() {}
void Zombie::_ready() {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
#endif
godot::Node* scene_root = Object::cast_to<godot::Node>(get_tree()->get_root()->get_child(0));
if (scene_root) {
m_Player = Object::cast_to<FirstPersonPlayer>(scene_root->find_child("FirstPersonPlayer", true));
DEV_ASSERT(m_Player);
} else {
ERR_PRINT("Scene root not found or invalid.");
}
m_NavigationAgent = Object::cast_to<NavigationAgent3D>(find_child("NavigationAgent3D"));
DEV_ASSERT(m_NavigationAgent);
m_AnimationTree = Object::cast_to<AnimationTree>(find_child("AnimationTree"));
DEV_ASSERT(m_AnimationTree);
m_StateMachine = Object::cast_to<AnimationNodeStateMachinePlayback>(m_AnimationTree->get("parameters/playback"));
DEV_ASSERT(m_StateMachine);
this->set_velocity(Vector3(0, 0, 0));
m_HeadCollision = Object::cast_to<BoneCollisionShape>(find_child("HeadArea3D"));
DEV_ASSERT(m_HeadCollision);
m_HeadCollision->connect("a_ZombieHeadShotHit", Callable(this, "_on_area_3d_a_zombie_head_shot_hit"));
connect_collision_shapes(m_BodyPartsCollision);
m_Timer = memnew(Timer);
add_child(m_Timer);
m_Timer->connect("timeout", callable_mp(this, &Zombie::_on_timer_timeout));
}
void Zombie::_process(float a_Delta) {
#if DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
#endif
if (m_StateMachine->get_current_node().match("Run")) {
m_NavigationAgent->set_target_position(m_Player->get_global_position());
m_Velocity = (m_NavigationAgent->get_next_path_position() - this->get_global_position()).normalized() * SPEED;
this->set_velocity(m_Velocity);
look_at(Vector3(this->get_global_position().x + this->get_velocity().x, this->get_global_position().y,
this->get_global_position().z + this->get_velocity().z));
} else if (m_StateMachine->get_current_node().match("Attack")) {
this->set_velocity(Vector3(0, 0, 0));
look_at(Vector3(m_Player->get_global_position().x + this->get_velocity().x, this->get_global_position().y,
m_Player->get_global_position().z + this->get_velocity().z));
}
m_AnimationTree->set("parameters/conditions/run", !_target_in_range());
m_AnimationTree->set("parameters/conditions/attack", _target_in_range());
move_and_slide();
}
bool Zombie::_target_in_range() {
return this->get_global_position().distance_to(m_Player->get_global_position()) < ATTACK_RANGE;
}
void Zombie::set_m_PlayerPath(const NodePath& path) {
m_PlayerPath = path;
}
NodePath Zombie::get_m_PlayerPath() const {
return m_PlayerPath;
}
void Zombie::hit_finished() {
if (this->get_global_position().distance_to(m_Player->get_global_position()) < (ATTACK_RANGE + 1.0)) {
Vector3 dir = this->get_global_position().direction_to(m_Player->get_global_position());
m_Player->hit(dir);
}
}
void Zombie::apply_damage(int dam) {
m_Health -= dam;
if (m_Health <= 0) {
m_AnimationTree->set("parameters/conditions/die", true);
this->set_velocity(Vector3(0, 0, 0));
m_Timer->set_wait_time(4.0);
m_Timer->start();
}
}
void Zombie::_on_area_3d_a_zombie_head_shot_hit(int dam) {
apply_damage(dam);
}
void Zombie::_on_area_3d_a_zombie_body_shot_hit(int dam) {
apply_damage(dam);
}
void Zombie::connect_collision_shapes(const std::vector<StringName>& body_parts) {
for (const StringName& part : body_parts) {
StringName area_name = String(part) + "Area3D";
BoneCollisionShape* collision_shape = Object::cast_to<BoneCollisionShape>(find_child(area_name));
DEV_ASSERT(collision_shape);
collision_shape->connect("a_ZombieBodyShotHit", Callable(this, "_on_area_3d_a_zombie_body_shot_hit"));
}
}
void Zombie::_on_timer_timeout() {
queue_free();
}
} // namespace blitz

51
src/Zombie.h Normal file
View File

@@ -0,0 +1,51 @@
#pragma once
#include "BoneCollisionShape.h"
#include "FirstPersonPlayer.h"
#include <godot_cpp/classes/animation_node_state_machine_playback.hpp>
#include <godot_cpp/classes/animation_tree.hpp>
#include <godot_cpp/classes/character_body3d.hpp>
#include <godot_cpp/classes/navigation_agent3d.hpp>
#include <godot_cpp/variant/node_path.hpp>
#include <godot_cpp/variant/string_name.hpp>
#include <godot_cpp/classes/timer.hpp>
namespace blitz {
class Zombie : public godot::CharacterBody3D {
GDCLASS(Zombie, godot::CharacterBody3D)
protected:
static void _bind_methods();
public:
Zombie();
~Zombie();
void _ready();
void _process(float delta);
bool _target_in_range();
void _on_area_3d_a_zombie_head_shot_hit(int dam);
void _on_area_3d_a_zombie_body_shot_hit(int dam);
private:
godot::NavigationAgent3D* m_NavigationAgent;
FirstPersonPlayer* m_Player;
godot::NodePath m_PlayerPath;
godot::Vector3 m_Velocity;
godot::AnimationTree* m_AnimationTree;
godot::AnimationNodeStateMachinePlayback* m_StateMachine;
BoneCollisionShape* m_HeadCollision;
std::vector<godot::StringName> m_BodyPartsCollision = {"Neck", "Spine", "Hips", "LeftArm", "LeftForearm", "LeftHand", "RightArm",
"RightForearm", "RightHand", "LeftUpLeg", "RightUpLeg", "LeftLeg", "RightLeg", "LeftFoot", "RightFoot"};
int m_Health = 6;
godot::Timer* m_Timer;
void set_m_PlayerPath(const godot::NodePath& path);
godot::NodePath get_m_PlayerPath() const;
void hit_finished();
void apply_damage(int dam);
void connect_collision_shapes(const std::vector<godot::StringName>& body_parts);
void _on_timer_timeout();
};
} // namespace blitz

View File

@@ -1,49 +0,0 @@
#include <blitz/protocol/ByteBuffer.h>
#include <blitz/protocol/PacketData.h>
namespace blitz {
namespace protocol {
ByteBuffer& ByteBuffer::operator>>(PlayerInfo& a_Data) {
*this >> a_Data.m_PlayerId >> a_Data.m_PlayerName;
return *this;
}
ByteBuffer& ByteBuffer::operator<<(const PlayerInfo& a_Data) {
*this << a_Data.m_PlayerId << a_Data.m_PlayerName;
return *this;
}
ByteBuffer& ByteBuffer::operator<<(const godot::Vector3& a_Data) {
*this << a_Data.x << a_Data.y << a_Data.z;
return *this;
}
ByteBuffer& ByteBuffer::operator>>(godot::Vector3& a_Data) {
*this >> a_Data.x >> a_Data.y >> a_Data.z;
return *this;
}
ByteBuffer& ByteBuffer::operator>>(godot::String& a_Data) {
int nullPos = m_Buffer.find(0, m_ReadOffset);
// TODO: error handling
if (nullPos < 0)
return *this;
godot::PackedByteArray stringBuffer = m_Buffer.slice(m_ReadOffset, nullPos);
a_Data = stringBuffer.get_string_from_utf8();
m_ReadOffset = nullPos + 1;
return *this;
}
ByteBuffer& ByteBuffer::operator<<(const godot::String& a_Data) {
godot::PackedByteArray stringBuffer = a_Data.to_utf8_buffer();
m_Buffer.append_array(stringBuffer);
// ends the string
*this << static_cast<std::uint8_t>(0);
return *this;
}
} // namespace protocol
} // namespace blitz

View File

@@ -1,37 +0,0 @@
#include <blitz/protocol/PacketDispatcher.h>
#include <algorithm>
#include <blitz/protocol/PacketHandler.h>
namespace blitz {
namespace protocol {
void PacketDispatcher::RegisterHandler(PacketType type, PacketHandler& handler) {
auto found = std::find(m_Handlers[type].begin(), m_Handlers[type].end(), &handler);
if (found == m_Handlers[type].end())
m_Handlers[type].push_back(&handler);
}
void PacketDispatcher::UnregisterHandler(PacketType type, PacketHandler& handler) {
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
}
void PacketDispatcher::UnregisterHandler(PacketHandler& handler) {
for (auto& pair : m_Handlers) {
if (pair.second.empty())
continue;
PacketType type = pair.first;
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
}
}
void PacketDispatcher::Dispatch(const Packet& packet) {
PacketType type = packet.GetType();
for (PacketHandler* handler : m_Handlers[type])
handler->Check(packet);
}
} // namespace protocol
} // namespace blitz

View File

@@ -1,32 +0,0 @@
#include <blitz/protocol/PacketFactory.h>
#include <array>
#include <cassert>
#include <functional>
namespace blitz {
namespace protocol {
namespace PacketFactory {
using PacketCreator = std::function<std::unique_ptr<Packet>()>;
#define DeclarePacket(PacketName, ...) std::make_unique<packets::PacketName>(),
static std::array<std::unique_ptr<Packet>, static_cast<std::size_t>(PacketType::PACKET_COUNT)> Packets;
void Init() {
Packets = {
DeclareAllPacket()
};
}
const std::unique_ptr<Packet>& CreateReadOnlyPacket(PacketType a_Type) {
assert(a_Type < PacketType::PACKET_COUNT);
return Packets[static_cast<std::size_t>(a_Type)];
}
} // namespace PacketFactory
} // namespace protocol
} // namespace blitz

View File

@@ -1,14 +0,0 @@
#include <blitz/protocol/PacketHandler.h>
namespace blitz {
namespace protocol {
#define DeclarePacket(PacketName, ...) \
void PacketHandler::Visit(const packets::PacketName& a_Packet) { \
HandlePacket(a_Packet); \
}
DeclareAllPacket()
} // namespace protocol
} // namespace blitz

View File

@@ -1,294 +0,0 @@
#include <blitz/protocol/PacketSerializer.h>
#include <blitz/protocol/ByteBuffer.h>
#include <blitz/protocol/PacketFactory.h>
#include <blitz/protocol/PacketVisitor.h>
#include <godot_cpp/variant/utility_functions.hpp>
#include <godot_cpp/variant/variant.hpp>
namespace blitz {
namespace protocol {
namespace PacketSerializer {
#define DeclarePacket(PacketName, ...) \
void Visit(const packets::PacketName& a_Packet) override { \
const auto& packetData = a_Packet.m_Data; \
SerializePacketData(packetData); \
} \
\
void SerializePacketData(const packets::PacketName::PacketDataType& a_Packet);
class Serializer : public PacketVisitor {
private:
ByteBuffer& m_Buffer;
public:
Serializer(ByteBuffer& a_Buffer) : m_Buffer(a_Buffer) {}
void Serialize(const Packet& a_Packet) {
m_Buffer << static_cast<PacketID>(a_Packet.GetType());
Check(a_Packet);
}
DeclareAllPacket()
};
#undef DeclarePacket
#define DeclarePacket(PacketName, ...) \
void Visit(const packets::PacketName& a_Packet) override { \
auto packetPtr = PacketFactory::CreatePacket<packets::PacketName>(); \
auto& packetData = packetPtr->m_Data; \
\
DeserializePacketData(packetData); \
\
m_Packet = std::move(packetPtr); \
} \
\
void DeserializePacketData(packets::PacketName::PacketDataType& a_Packet);
class Deserializer : public PacketVisitor {
private:
ByteBuffer& m_Buffer;
PacketPtr m_Packet;
public:
Deserializer(ByteBuffer&& a_Buffer) : m_Buffer(a_Buffer) {}
bool Deserialize(const PacketPtr& a_Packet) {
try {
Check(*a_Packet.get());
} catch (std::exception& e) {
return false;
}
return true;
}
PacketPtr& GetPacket() {
return m_Packet;
}
DeclareAllPacket()
};
godot::PackedByteArray Serialize(const Packet& a_Packet) {
ByteBuffer stream;
Serializer serializer(stream);
serializer.Serialize(a_Packet);
return stream.GetByteArray();
}
std::unique_ptr<Packet> Deserialize(godot::PackedByteArray& a_Data) {
ByteBuffer stream(std::move(a_Data));
PacketID packetId;
stream >> packetId;
if (packetId >= static_cast<PacketID>(PacketType::PACKET_COUNT))
return nullptr;
PacketType packetType = PacketType(packetId);
// for double-dispatch
const PacketPtr& emptyPacket = PacketFactory::CreateReadOnlyPacket(packetType);
Deserializer deserializer(std::move(stream));
if (deserializer.Deserialize(emptyPacket)) {
PacketPtr packet = std::move(deserializer.GetPacket());
return packet;
}
return nullptr;
}
//---------------------------------------------
// Packet serializer implementation
//----------------------------------------------
void Serializer::SerializePacketData(const data::PlayerLogin& a_Packet) {
m_Buffer << a_Packet.m_PlayerName;
}
void Deserializer::DeserializePacketData(data::PlayerLogin& a_Packet) {
m_Buffer >> a_Packet.m_PlayerName;
}
void Serializer::SerializePacketData(const data::UpdateHealth& a_Packet) {
m_Buffer << a_Packet.m_NewHealth;
}
void Deserializer::DeserializePacketData(data::UpdateHealth& a_Packet) {
m_Buffer >> a_Packet.m_NewHealth;
}
void Serializer::SerializePacketData(const data::LoggingSuccess& a_Packet) {
m_Buffer << a_Packet.m_PlayerId;
}
void Deserializer::DeserializePacketData(data::LoggingSuccess& a_Packet) {
m_Buffer >> a_Packet.m_PlayerId;
}
void Serializer::SerializePacketData(const data::PlayerDeath& a_Packet) {}
void Deserializer::DeserializePacketData(data::PlayerDeath& a_Packet) {}
void Serializer::SerializePacketData(const data::PlayerJoin& a_Packet) {
m_Buffer << a_Packet.m_Player;
}
void Deserializer::DeserializePacketData(data::PlayerJoin& a_Packet) {
m_Buffer >> a_Packet.m_Player;
}
void Serializer::SerializePacketData(const data::PlayerLeave& a_Packet) {
m_Buffer << a_Packet.m_PlayerId;
}
void Deserializer::DeserializePacketData(data::PlayerLeave& a_Packet) {
m_Buffer >> a_Packet.m_PlayerId;
}
void Serializer::SerializePacketData(const data::PlayerList& a_Packet) {
m_Buffer << a_Packet.m_Players;
// m_Buffer << static_cast<std::uint8_t>(a_Packet.m_Players.size());
// for (auto player : a_Packet.m_Players) {
// m_Buffer << player.m_PlayerId << player.m_PlayerName;
// }
}
void Deserializer::DeserializePacketData(data::PlayerList& a_Packet) {
m_Buffer >> a_Packet.m_Players;
// std::uint8_t playerCount;
// m_Buffer >> playerCount;
// for (std::uint8_t i = 0; i < playerCount; i++) {
// PlayerInfo player;
// m_Buffer >> player.m_PlayerId >> player.m_PlayerName;
// a_Packet.m_Players.push_back(player);
// }
}
void Serializer::SerializePacketData(const data::PlayerStats& a_Packet) {}
void Deserializer::DeserializePacketData(data::PlayerStats& a_Packet) {}
void Serializer::SerializePacketData(const data::ServerConfig& a_Packet) {}
void Deserializer::DeserializePacketData(data::ServerConfig& a_Packet) {}
void Serializer::SerializePacketData(const data::ServerTps& a_Packet) {}
void Deserializer::DeserializePacketData(data::ServerTps& a_Packet) {}
void Serializer::SerializePacketData(const data::UpdateGameState& a_Packet) {}
void Deserializer::DeserializePacketData(data::UpdateGameState& a_Packet) {}
void Serializer::SerializePacketData(const data::KeepAlive& a_Packet) {
m_Buffer << a_Packet.m_KeepAliveId;
}
void Deserializer::DeserializePacketData(data::KeepAlive& a_Packet) {
m_Buffer >> a_Packet.m_KeepAliveId;
}
void Serializer::SerializePacketData(const data::Disconnect& a_Packet) {}
void Deserializer::DeserializePacketData(data::Disconnect& a_Packet) {}
void Serializer::SerializePacketData(const data::ChatMessage& a_Packet) {
m_Buffer << a_Packet.m_Text;
}
void Deserializer::DeserializePacketData(data::ChatMessage& a_Packet) {
m_Buffer >> a_Packet.m_Text;
}
void Serializer::SerializePacketData(const data::PlayerPositionAndRotation& a_Packet) {
m_Buffer << a_Packet.m_Player << a_Packet.m_Position << a_Packet.m_Rotation;
}
void Deserializer::DeserializePacketData(data::PlayerPositionAndRotation& a_Packet) {
m_Buffer >> a_Packet.m_Player >> a_Packet.m_Position >> a_Packet.m_Rotation;
}
void Serializer::SerializePacketData(const data::PlayerShoot& a_Packet) {}
void Deserializer::DeserializePacketData(data::PlayerShoot& a_Packet) {}
} // namespace PacketSerializer
} // namespace protocol
} // namespace blitz

View File

@@ -1,11 +0,0 @@
#include <blitz/protocol/PacketVisitor.h>
namespace blitz {
namespace protocol {
void PacketVisitor::Check(const Packet& a_Packet) {
a_Packet.Accept(*this);
}
} // namespace protocol
} // namespace blitz

View File

@@ -1,18 +0,0 @@
#define BLITZ_INSTANCIATE_PACKETS
#include <blitz/protocol/Packets.h>
#include <blitz/protocol/PacketVisitor.h>
namespace blitz {
namespace protocol {
template <PacketType PT, typename Data>
packets::ConcretePacket<PT, Data>::ConcretePacket(const PacketDataType& a_Data) : m_Data(a_Data) {}
template <PacketType PT, typename Data>
void packets::ConcretePacket<PT, Data>::Accept(PacketVisitor& a_Visitor) const {
a_Visitor.Visit(*this);
}
} // namespace protocol
} // namespace blitz

View File

@@ -1,14 +1,14 @@
#include "register_types.h" #include "register_types.h"
#include "BoneCollisionShape.h"
#include "Bullet.h"
#include "Crosshair.h"
#include "FirstPersonPlayer.h" #include "FirstPersonPlayer.h"
#include "Lobby.h" #include "HitMarkers.h"
#include "Main.h"
#include "MainMenu.h"
#include "NetworkInterface.h"
#include "Player.h" #include "Player.h"
#include "Server.h"
#include "SpringArmPivot.h" #include "SpringArmPivot.h"
#include "World.h" #include "World.h"
#include "Zombie.h"
#include <gdextension_interface.h> #include <gdextension_interface.h>
#include <godot_cpp/core/defs.hpp> #include <godot_cpp/core/defs.hpp>
@@ -16,24 +16,20 @@
using namespace godot; using namespace godot;
static void RegisterClasses() {
GDREGISTER_CLASS(blitz::Player);
GDREGISTER_CLASS(blitz::SpringArmPivot);
GDREGISTER_CLASS(blitz::FirstPersonPlayer);
GDREGISTER_CLASS(blitz::MainMenu);
GDREGISTER_CLASS(blitz::Lobby);
GDREGISTER_CLASS(blitz::World);
GDREGISTER_CLASS(blitz::Main);
GDREGISTER_CLASS(blitz::NetworkInterface);
GDREGISTER_CLASS(blitz::Server);
}
void initialize_example_module(ModuleInitializationLevel p_level) { void initialize_example_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) { if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return; return;
} }
RegisterClasses(); ClassDB::register_class<blitz::Player>();
ClassDB::register_class<blitz::SpringArmPivot>();
ClassDB::register_class<blitz::FirstPersonPlayer>();
ClassDB::register_class<blitz::Bullet>();
ClassDB::register_class<blitz::Zombie>();
ClassDB::register_class<blitz::BoneCollisionShape>();
ClassDB::register_class<blitz::World>();
ClassDB::register_class<blitz::Crosshair>();
ClassDB::register_class<blitz::HitMarkers>();
} }
void uninitialize_example_module(ModuleInitializationLevel p_level) { void uninitialize_example_module(ModuleInitializationLevel p_level) {

View File

@@ -16,7 +16,7 @@ add_rules("mode.debug", "mode.release")
set_languages("c++20") set_languages("c++20")
-- use latest 4.x version by default -- use latest 4.x version by default
add_requires("godotcpp4 4.2") add_requires("godotcpp4")
includes("tasks.lua") includes("tasks.lua")

View File

@@ -14,8 +14,6 @@ target(PROJECT_NAME)
-- more on https://xmake.io/#/manual/project_target?id=targetadd_files -- more on https://xmake.io/#/manual/project_target?id=targetadd_files
add_files("../src/**.cpp") add_files("../src/**.cpp")
add_includedirs("../include")
-- change the output name -- change the output name
set_basename(PROJECT_NAME .. ".$(os)_$(mode)_$(arch)") set_basename(PROJECT_NAME .. ".$(os)_$(mode)_$(arch)")