complete project reorganization
This commit is contained in:
196
scenes/player_controller/components/weapon/WeaponSystem.cs
Normal file
196
scenes/player_controller/components/weapon/WeaponSystem.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using GodotStateCharts;
|
||||
using Movementtests.interfaces;
|
||||
using Movementtests.systems.damage;
|
||||
|
||||
namespace Movementtests.systems;
|
||||
|
||||
[GlobalClass, Icon("res://assets/ui/IconGodotNode/node_3D/icon_sword.png")]
|
||||
public partial class WeaponSystem : RigidBody3D, IDamageDealer
|
||||
{
|
||||
[Signal]
|
||||
public delegate void WeaponThrownEventHandler();
|
||||
|
||||
[Signal]
|
||||
public delegate void WeaponRetrievedEventHandler();
|
||||
|
||||
[Export]
|
||||
public RDamage RDamage { get; set; }
|
||||
[Export(PropertyHint.Range, "0,100,1,or_greater")]
|
||||
public float ThrowForce { get; set; } = 1f;
|
||||
[Export(PropertyHint.Range, "0,0.2,0.01,or_greater")]
|
||||
public float StraightThrowDuration { get; set; } = 0.1f;
|
||||
|
||||
private StateChart _weaponState;
|
||||
public StateChartState InHandState;
|
||||
public StateChartState FlyingState;
|
||||
public StateChartState PlantedState;
|
||||
|
||||
private ShapeCast3D _dashCast3D;
|
||||
|
||||
private Transform3D _startTransform;
|
||||
private Vector3 _startMeshRotation;
|
||||
|
||||
private Vector3 _throwDirection;
|
||||
public Vector3 PlantLocation { get; set; }
|
||||
public Vector3 PlantNormal { get; set; }
|
||||
public Node PlantObject { get; set; }
|
||||
|
||||
public MeshInstance3D WeaponLocationIndicator { get; set; }
|
||||
public StandardMaterial3D WeaponLocationIndicatorMaterial { get; set; }
|
||||
public MeshInstance3D WeaponMesh { get; set; }
|
||||
|
||||
public void Init()
|
||||
{
|
||||
_weaponState = StateChart.Of(GetNode("StateChart"));
|
||||
InHandState = StateChartState.Of(GetNode("StateChart/Root/InHand"));
|
||||
FlyingState = StateChartState.Of(GetNode("StateChart/Root/Flying"));
|
||||
PlantedState = StateChartState.Of(GetNode("StateChart/Root/Planted"));
|
||||
|
||||
WeaponLocationIndicator = GetNode<MeshInstance3D>("WeaponLocationIndicator");
|
||||
WeaponLocationIndicator.Visible = false;
|
||||
WeaponLocationIndicatorMaterial = WeaponLocationIndicator.GetActiveMaterial(0) as StandardMaterial3D;
|
||||
|
||||
WeaponMesh = GetNode<MeshInstance3D>("Weapon");
|
||||
_startMeshRotation = WeaponMesh.Rotation;
|
||||
|
||||
_startTransform = Transform;
|
||||
Freeze = true;
|
||||
Visible = false;
|
||||
|
||||
BodyEntered += OnThrownWeaponReachesGround;
|
||||
|
||||
InHandState.StateExited += WeaponLeft;
|
||||
InHandState.StateEntered += WeaponBack;
|
||||
}
|
||||
|
||||
public void WeaponLeft()
|
||||
{
|
||||
Visible = true;
|
||||
// WeaponLocationIndicator.Visible = true;
|
||||
EmitSignalWeaponThrown();
|
||||
}
|
||||
|
||||
public void WeaponBack()
|
||||
{
|
||||
Visible = false;
|
||||
// WeaponLocationIndicator.Visible = false;
|
||||
EmitSignalWeaponRetrieved();
|
||||
}
|
||||
|
||||
public void PlaceWeaponForTutorial(Vector3 location)
|
||||
{
|
||||
_weaponState.SendEvent("plant");
|
||||
Freeze = true;
|
||||
GlobalPosition = location;
|
||||
PlantLocation = location;
|
||||
}
|
||||
|
||||
public void ThrowWeapon(Vector3 end, bool hasHit, Vector3 collisionLocation, Vector3 collisionNormal, Node collidedObject)
|
||||
{
|
||||
_weaponState.SendEvent("throw");
|
||||
|
||||
// WeaponLocationIndicatorMaterial.StencilColor = new Color(1f, 1f, 1f);
|
||||
|
||||
_throwDirection = (end - GlobalPosition).Normalized();
|
||||
PlantLocation = collisionLocation;
|
||||
PlantNormal = collisionNormal;
|
||||
LookAt(end);
|
||||
|
||||
|
||||
var tween = GetTree().CreateTween();
|
||||
tween.SetParallel(true);
|
||||
tween.TweenProperty(this, "global_position", end, StraightThrowDuration);
|
||||
if (hasHit)
|
||||
{
|
||||
PlantObject = collidedObject;
|
||||
tween.Finished += PlantWeaponInWall;
|
||||
}
|
||||
else
|
||||
tween.Finished += ThrowWeaponOnCurve;
|
||||
}
|
||||
|
||||
public void PlantInEnemy(Node3D enemy)
|
||||
{
|
||||
GetTree().GetRoot().CallDeferred(Node.MethodName.RemoveChild, this);
|
||||
enemy.CallDeferred(Node.MethodName.AddChild, this);
|
||||
|
||||
if (enemy is IDamageable damageable)
|
||||
{
|
||||
damageable.TakeDamage(new DamageRecord(GlobalPosition, RDamage));
|
||||
}
|
||||
}
|
||||
|
||||
public void RethrowWeapon()
|
||||
{
|
||||
_weaponState.SendEvent("throw");
|
||||
_throwDirection = Vector3.Up;
|
||||
ThrowWeaponOnCurve();
|
||||
}
|
||||
|
||||
public void ThrowWeaponOnCurve()
|
||||
{
|
||||
Freeze = false;
|
||||
ApplyImpulse(_throwDirection * ThrowForce);
|
||||
}
|
||||
|
||||
public void PlantWeaponInWall()
|
||||
{
|
||||
_weaponState.SendEvent("plant");
|
||||
|
||||
Freeze = true;
|
||||
WeaponMesh.Rotation = _startMeshRotation;
|
||||
|
||||
// WeaponLocationIndicatorMaterial.StencilColor = new Color(1f, 0.2f, 0.2f);
|
||||
if (PlantObject is Node3D node)
|
||||
{
|
||||
PlantInEnemy(node);
|
||||
}
|
||||
CallDeferred(Node3D.MethodName.SetGlobalPosition, PlantLocation);
|
||||
CallDeferred(Node3D.MethodName.LookAt, GlobalTransform.Origin + PlantNormal, Vector3.Up, true);
|
||||
}
|
||||
|
||||
public void OnThrownWeaponReachesGround(Node other)
|
||||
{
|
||||
PlantObject = other;
|
||||
PlantWeaponInWall();
|
||||
}
|
||||
|
||||
public void ResetWeapon()
|
||||
{
|
||||
_weaponState.SendEvent("recover");
|
||||
Transform = _startTransform;
|
||||
Freeze = true;
|
||||
Visible = false;
|
||||
}
|
||||
|
||||
public override void _IntegrateForces(PhysicsDirectBodyState3D state)
|
||||
{
|
||||
base._IntegrateForces(state);
|
||||
|
||||
if (!Freeze && state.GetContactCount() > 0)
|
||||
{
|
||||
PlantLocation = state.GetContactLocalPosition(0);
|
||||
PlantNormal = state.GetContactLocalNormal(0);
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
if (!FlyingState.Active) return;
|
||||
|
||||
WeaponMesh.Rotation = new Vector3(WeaponMesh.Rotation.X, WeaponMesh.Rotation.Y + (float) delta * 100, WeaponMesh.Rotation.Z);
|
||||
//LookAt(GlobalTransform.Origin + LinearVelocity.Normalized(), Vector3.Up, false);
|
||||
}
|
||||
|
||||
public bool IsPlantedUnderPlatform()
|
||||
{
|
||||
return PlantedState.Active && GlobalRotation.X > 1 && Math.Abs(GlobalRotation.Y) > 1;
|
||||
}
|
||||
|
||||
public bool IsPlantedInWall()
|
||||
{
|
||||
return PlantedState.Active && Math.Abs(GlobalRotation.X) + Math.Abs(GlobalRotation.Z) < 0.3;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://iii3wfto4t5b
|
||||
@@ -0,0 +1,5 @@
|
||||
extends MeshInstance3D
|
||||
|
||||
|
||||
func _on_weapon_retrieved_body_entered(body: Node3D) -> void:
|
||||
visible = false
|
||||
@@ -0,0 +1 @@
|
||||
uid://v4nnql2laqdn
|
||||
13
scenes/player_controller/components/weapon/weapon.tres
Normal file
13
scenes/player_controller/components/weapon/weapon.tres
Normal file
@@ -0,0 +1,13 @@
|
||||
[gd_resource type="CylinderMesh" format=3 uid="uid://b7vt0nk2htpo4"]
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_jv82r"]
|
||||
use_z_clip_scale = true
|
||||
z_clip_scale = 0.9
|
||||
use_fov_override = true
|
||||
fov_override = 70.0
|
||||
|
||||
[resource]
|
||||
material = SubResource("StandardMaterial3D_jv82r")
|
||||
top_radius = 0.0
|
||||
bottom_radius = 0.05
|
||||
height = 1.0
|
||||
109
scenes/player_controller/components/weapon/weapon.tscn
Normal file
109
scenes/player_controller/components/weapon/weapon.tscn
Normal file
@@ -0,0 +1,109 @@
|
||||
[gd_scene format=3 uid="uid://ckm3d6k08a72u"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://iii3wfto4t5b" path="res://scenes/player_controller/components/weapon/WeaponSystem.cs" id="1_csqwk"]
|
||||
[ext_resource type="Script" uid="uid://jitubgv6judn" path="res://scenes/components/damage/RDamage.cs" id="2_m0v1h"]
|
||||
[ext_resource type="Script" uid="uid://couw105c3bde4" path="res://addons/godot_state_charts/state_chart.gd" id="3_5owyf"]
|
||||
[ext_resource type="ArrayMesh" uid="uid://cho5fixitrbds" path="res://assets/meshes/swords/resources/sword23.tres" id="3_svc06"]
|
||||
[ext_resource type="Script" uid="uid://jk2jm1g6q853" path="res://addons/godot_state_charts/compound_state.gd" id="4_svc06"]
|
||||
[ext_resource type="Script" uid="uid://cytafq8i1y8qm" path="res://addons/godot_state_charts/atomic_state.gd" id="5_m0v1h"]
|
||||
[ext_resource type="Script" uid="uid://cf1nsco3w0mf6" path="res://addons/godot_state_charts/transition.gd" id="6_jpdh0"]
|
||||
|
||||
[sub_resource type="Resource" id="Resource_jpdh0"]
|
||||
script = ExtResource("2_m0v1h")
|
||||
DamageDealt = 2.0
|
||||
metadata/_custom_type_script = "uid://jitubgv6judn"
|
||||
|
||||
[sub_resource type="CylinderShape3D" id="CylinderShape3D_avini"]
|
||||
height = 1.0
|
||||
radius = 0.1
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_svc06"]
|
||||
render_priority = 1
|
||||
transparency = 1
|
||||
no_depth_test = true
|
||||
shading_mode = 0
|
||||
grow_amount = 0.1
|
||||
stencil_mode = 3
|
||||
stencil_flags = 1
|
||||
stencil_compare = 5
|
||||
metadata/_stencil_owned = true
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_m0v1h"]
|
||||
next_pass = SubResource("StandardMaterial3D_svc06")
|
||||
transparency = 1
|
||||
albedo_color = Color(1, 1, 1, 0)
|
||||
z_clip_scale = 0.1
|
||||
stencil_mode = 2
|
||||
stencil_flags = 2
|
||||
stencil_color = Color(1, 1, 1, 1)
|
||||
stencil_outline_thickness = 0.1
|
||||
|
||||
[sub_resource type="SphereMesh" id="SphereMesh_jpdh0"]
|
||||
material = SubResource("StandardMaterial3D_m0v1h")
|
||||
|
||||
[node name="Weapon" type="RigidBody3D" unique_id=831063023]
|
||||
collision_layer = 65536
|
||||
collision_mask = 304
|
||||
continuous_cd = true
|
||||
contact_monitor = true
|
||||
max_contacts_reported = 1
|
||||
script = ExtResource("1_csqwk")
|
||||
RDamage = SubResource("Resource_jpdh0")
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="." unique_id=884463982]
|
||||
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0)
|
||||
shape = SubResource("CylinderShape3D_avini")
|
||||
|
||||
[node name="Weapon" type="MeshInstance3D" parent="." unique_id=1970473659]
|
||||
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0.8673003)
|
||||
mesh = ExtResource("3_svc06")
|
||||
|
||||
[node name="WeaponLocationIndicator" type="MeshInstance3D" parent="." unique_id=406396593]
|
||||
mesh = SubResource("SphereMesh_jpdh0")
|
||||
|
||||
[node name="StateChart" type="Node" parent="." unique_id=1135887603]
|
||||
script = ExtResource("3_5owyf")
|
||||
metadata/_custom_type_script = "uid://couw105c3bde4"
|
||||
|
||||
[node name="Root" type="Node" parent="StateChart" unique_id=1816439862]
|
||||
script = ExtResource("4_svc06")
|
||||
initial_state = NodePath("InHand")
|
||||
|
||||
[node name="ToPlanted" type="Node" parent="StateChart/Root" unique_id=357013486]
|
||||
script = ExtResource("6_jpdh0")
|
||||
to = NodePath("../Planted")
|
||||
event = &"plant"
|
||||
delay_in_seconds = "0.0"
|
||||
|
||||
[node name="InHand" type="Node" parent="StateChart/Root" unique_id=1828871728]
|
||||
script = ExtResource("5_m0v1h")
|
||||
|
||||
[node name="ToFlying" type="Node" parent="StateChart/Root/InHand" unique_id=1644664016]
|
||||
script = ExtResource("6_jpdh0")
|
||||
to = NodePath("../../Flying")
|
||||
event = &"throw"
|
||||
delay_in_seconds = "0.0"
|
||||
|
||||
[node name="Flying" type="Node" parent="StateChart/Root" unique_id=861606667]
|
||||
script = ExtResource("5_m0v1h")
|
||||
|
||||
[node name="ToHand" type="Node" parent="StateChart/Root/Flying" unique_id=1236392249]
|
||||
script = ExtResource("6_jpdh0")
|
||||
to = NodePath("../../InHand")
|
||||
event = &"recover"
|
||||
delay_in_seconds = "0.0"
|
||||
|
||||
[node name="Planted" type="Node" parent="StateChart/Root" unique_id=1036062749]
|
||||
script = ExtResource("5_m0v1h")
|
||||
|
||||
[node name="ToFlying" type="Node" parent="StateChart/Root/Planted" unique_id=1472568793]
|
||||
script = ExtResource("6_jpdh0")
|
||||
to = NodePath("../../Flying")
|
||||
event = &"throw"
|
||||
delay_in_seconds = "0.0"
|
||||
|
||||
[node name="ToHand" type="Node" parent="StateChart/Root/Planted" unique_id=627081934]
|
||||
script = ExtResource("6_jpdh0")
|
||||
to = NodePath("../../InHand")
|
||||
event = &"recover"
|
||||
delay_in_seconds = "0.0"
|
||||
11
scenes/player_controller/components/weapon/weapon_tuto.tres
Normal file
11
scenes/player_controller/components/weapon/weapon_tuto.tres
Normal file
@@ -0,0 +1,11 @@
|
||||
[gd_resource type="CylinderMesh" format=3 uid="uid://bhkbwvuft1bpg"]
|
||||
|
||||
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_f5qr4"]
|
||||
z_clip_scale = 0.9
|
||||
fov_override = 70.0
|
||||
|
||||
[resource]
|
||||
material = SubResource("StandardMaterial3D_f5qr4")
|
||||
top_radius = 0.0
|
||||
bottom_radius = 0.05
|
||||
height = 1.0
|
||||
Reference in New Issue
Block a user