Added CollectibleComponent and refactored GoldBar, Wood and Axe.

main
Martin Felis 2024-01-04 15:45:26 +01:00
parent c34c704b38
commit ee859886f0
13 changed files with 119 additions and 133 deletions

View File

@ -0,0 +1,46 @@
using Godot;
public class CollectibleComponent : Component {
private Vector3 targetPosition;
private bool hasTarget;
// Called when the node enters the scene tree for the first time.
public override void _Ready() { }
public void SetTarget(Vector3 target) {
targetPosition = target;
hasTarget = true;
}
public void UnsetTarget() {
hasTarget = false;
}
public void PhysicsProcess(float delta, Entity entity) {
if (hasTarget) {
if (targetPosition.IsEqualApprox(entity.GlobalTransform.origin)) {
targetPosition = entity.GlobalTransform.origin;
entity.Velocity = Vector3.Zero;
}
Vector3 targetDirection = (targetPosition - entity.GlobalTransform.origin).Normalized();
entity.Velocity = targetDirection * (entity.Velocity.Length() + 10 * delta);
entity.Transform = new Transform(entity.Transform.basis.Rotated(Vector3.Up, delta * 2.0f),
entity.Transform.origin);
} else {
entity.Velocity = entity.Velocity - 9.81f * delta * Vector3.Up;
}
entity.Velocity = entity.MoveAndSlide(entity.Velocity, Vector3.Up);
if (entity.IsOnFloor() || Mathf.Abs(entity.Transform.origin.y) < 0.01) {
// apply damping when on ground
entity.Velocity = entity.Velocity - entity.Velocity.Normalized() * 0.9f * delta;
}
if (entity.Velocity.LengthSquared() < 0.01) {
entity.Velocity = Vector3.Zero;
}
}
}

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://components/CollectibleComponent.cs" type="Script" id=1]
[node name="CollectibleComponent" type="Node"]
script = ExtResource( 1 )

View File

@ -1,6 +1 @@
using Godot;
public class Axe : StaticBody {
// Called when the node enters the scene tree for the first time.
public override void _Ready() { }
}
public class Axe : Entity { }

View File

@ -1,13 +1,14 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=5 format=2]
[ext_resource path="res://assets/Objects/toolAxe.tscn" type="PackedScene" id=1]
[ext_resource path="res://entities/Axe.cs" type="Script" id=2]
[ext_resource path="res://components/CollectibleComponent.tscn" type="PackedScene" id=3]
[sub_resource type="CylinderShape" id=1]
height = 0.846435
radius = 0.687167
[node name="Axe" type="StaticBody"]
[node name="Axe" type="KinematicBody"]
collision_layer = 9
collision_mask = 0
script = ExtResource( 2 )
@ -18,3 +19,5 @@ shape = SubResource( 1 )
[node name="toolAxe" parent="." instance=ExtResource( 1 )]
transform = Transform( 0.707107, 0.707107, -3.09086e-08, 4.37114e-08, 1.91069e-15, 1, 0.707107, -0.707107, -3.09086e-08, -0.323064, 0.0760467, 0.348457 )
[node name="CollectibleComponent" parent="." instance=ExtResource( 3 )]

View File

@ -84,7 +84,7 @@ public class Chest : Entity, IInteractionInterface {
GoldBar bar = (GoldBar)_goldBarScene.Instance();
bar.Transform = new Transform(Transform.basis.Rotated(Vector3.Up, GD.Randf() * 2 * Mathf.Pi),
Transform.origin + Vector3.Up * 0.8f);
bar.velocity = new Vector3(
bar.Velocity = new Vector3(
(GD.Randf() * 2f - 1f) * 2,
5 + GD.Randf() * 0.3f,
(GD.Randf() * 2f - 1f) * 2

View File

@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using Godot;
public class Entity : KinematicBody {
@ -15,6 +16,31 @@ public class Entity : KinematicBody {
public Vector3 Velocity { get; set; } = Vector3.Zero;
public float RotationalVelocity { get; set; } = 0;
private CollectibleComponent _collectibleComponent;
public override void _Ready() {
base._Ready();
foreach (Node node in GetChildren()) {
if (node is CollectibleComponent) {
Debug.Assert(_collectibleComponent == null);
_collectibleComponent = node as CollectibleComponent;
}
}
}
public override void _PhysicsProcess(float delta) {
base._PhysicsProcess(delta);
if (_collectibleComponent != null) {
_collectibleComponent.PhysicsProcess(delta, this);
}
}
public CollectibleComponent GetCollectibleComponent() {
return _collectibleComponent;
}
/**
* Defines the angle in plane coordinates, 0 => pointing to the right/east, pi/2 pointing up/north, range [-pi,pi].
*/

View File

@ -1,49 +1 @@
using Godot;
public class GoldBar : KinematicBody {
private Vector3 targetPosition;
private bool hasTarget;
public Vector3 velocity;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
targetPosition = GlobalTransform.origin;
}
public void SetTarget(Vector3 target) {
targetPosition = target;
hasTarget = true;
}
public void UnsetTarget() {
hasTarget = false;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _PhysicsProcess(float delta) {
if (hasTarget) {
if (targetPosition.IsEqualApprox(GlobalTransform.origin)) {
targetPosition = GlobalTransform.origin;
velocity = Vector3.Zero;
}
Vector3 targetDirection = (targetPosition - GlobalTransform.origin).Normalized();
velocity = targetDirection * (velocity.Length() + 10 * delta);
Transform = new Transform(Transform.basis.Rotated(Vector3.Up, delta * 2.0f), Transform.origin);
} else {
velocity.y = velocity.y - 9.81f * delta;
}
velocity = MoveAndSlide(velocity, Vector3.Up);
if (IsOnFloor() || Mathf.Abs(Transform.origin.y) < 0.01) {
// apply damping when on ground
velocity = velocity - velocity.Normalized() * 0.9f * delta;
}
if (velocity.LengthSquared() < 0.01) {
velocity = Vector3.Zero;
}
}
}
public class GoldBar : Entity { }

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=5 format=2]
[ext_resource path="res://entities/GoldBar.cs" type="Script" id=1]
[ext_resource path="res://assets/CreatusPiratePack/Models/Items/gltf/Gold_Bar.glb" type="PackedScene" id=2]
[ext_resource path="res://components/CollectibleComponent.tscn" type="PackedScene" id=3]
[sub_resource type="BoxShape" id=21]
extents = Vector3( 0.354271, 0.0817164, 0.173406 )
@ -16,8 +17,10 @@ script = ExtResource( 1 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.084728, 0 )
shape = SubResource( 21 )
[node name="Spatial" type="Spatial" parent="."]
[node name="Geometry" type="Spatial" parent="."]
transform = Transform( 0.5, 0, 0, 0, 0.5, 0, 0, 0, 0.5, 0, 0, 0 )
[node name="Gold_Bar" parent="Spatial" instance=ExtResource( 2 )]
[node name="Gold_Bar" parent="Geometry" instance=ExtResource( 2 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 21.335, -0.022, -6.533 )
[node name="CollectibleComponent" parent="." instance=ExtResource( 3 )]

View File

@ -57,8 +57,6 @@ public class Player : Entity, IInteractionInterface {
_itemPickupArea.Connect("body_entered", this, nameof(OnItemPickupAreaBodyEntered));
}
_playerAnimationPlayer = GetNode<AnimationPlayer>("Geometry/AnimationPlayer");
Debug.Assert(_playerAnimationPlayer != null);
_animationTree = GetNode<AnimationTree>("Geometry/AnimationTree");
Debug.Assert(_animationTree != null);
AnimationNodeStateMachinePlayback stateMachine =
@ -113,14 +111,12 @@ public class Player : Entity, IInteractionInterface {
}
foreach (Node node in _attractedItemList) {
if (node is GoldBar) {
GoldBar bar = (GoldBar)node;
bar.SetTarget(GlobalTransform.origin);
}
if (node is Wood) {
Wood wood = (Wood)node;
wood.SetTarget(GlobalTransform.origin);
Entity entity = node as Entity;
if (entity != null) {
CollectibleComponent collectibleComponent = entity.GetCollectibleComponent();
if (collectibleComponent != null) {
collectibleComponent.SetTarget(GlobalTransform.origin);
}
}
}
@ -145,14 +141,12 @@ public class Player : Entity, IInteractionInterface {
}
public void OnItemAttractorBodyExited(Node node) {
if (node is GoldBar) {
GoldBar bar = (GoldBar)node;
bar.UnsetTarget();
}
if (node is Wood) {
Wood wood = (Wood)node;
wood.UnsetTarget();
Entity entity = node as Entity;
if (entity != null) {
CollectibleComponent collectibleComponent = entity.GetCollectibleComponent();
if (collectibleComponent != null) {
collectibleComponent.UnsetTarget();
}
}
_attractedItemList.Remove(node);

View File

@ -56,7 +56,7 @@ public class Tree : StaticBody, IInteractionInterface {
Wood wood = (Wood)_woodScene.Instance();
wood.Transform = new Transform(Transform.basis.Rotated(Vector3.Up, GD.Randf() * 2 * Mathf.Pi),
Transform.origin + Vector3.Up * 0.8f);
wood.velocity = new Vector3(
wood.Velocity = new Vector3(
(GD.Randf() * 2f - 1f) * 1.2f,
5 + GD.Randf() * 0.3f,
(GD.Randf() * 2f - 1f) * 1.2f

View File

@ -1,49 +1 @@
using Godot;
public class Wood : KinematicBody {
private Vector3 targetPosition;
private bool hasTarget;
public Vector3 velocity;
// Called when the node enters the scene tree for the first time.
public override void _Ready() {
targetPosition = GlobalTransform.origin;
}
public void SetTarget(Vector3 target) {
targetPosition = target;
hasTarget = true;
}
public void UnsetTarget() {
hasTarget = false;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _PhysicsProcess(float delta) {
if (hasTarget) {
if (targetPosition.IsEqualApprox(GlobalTransform.origin)) {
targetPosition = GlobalTransform.origin;
velocity = Vector3.Zero;
}
Vector3 targetDirection = (targetPosition - GlobalTransform.origin).Normalized();
velocity = targetDirection * (velocity.Length() + 10 * delta);
Transform = new Transform(Transform.basis.Rotated(Vector3.Up, delta * 2.0f), Transform.origin);
} else {
velocity.y = velocity.y - 9.81f * delta;
}
velocity = MoveAndSlide(velocity, Vector3.Up);
if (IsOnFloor() || Mathf.Abs(Transform.origin.y) < 0.01) {
// apply damping when on ground
velocity = velocity - velocity.Normalized() * 0.9f * delta;
}
if (velocity.LengthSquared() < 0.01) {
velocity = Vector3.Zero;
}
}
}
public class Wood : Entity { }

View File

@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=2]
[gd_scene load_steps=5 format=2]
[ext_resource path="res://entities/Wood.cs" type="Script" id=1]
[ext_resource path="res://assets/KenneySurvivalKit/Models/resourceWood.glb" type="PackedScene" id=2]
[ext_resource path="res://components/CollectibleComponent.tscn" type="PackedScene" id=3]
[sub_resource type="BoxShape" id=21]
extents = Vector3( 0.344943, 0.0817164, 0.173406 )
@ -16,7 +17,9 @@ script = ExtResource( 1 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.084728, 0 )
shape = SubResource( 21 )
[node name="Spatial" type="Spatial" parent="."]
[node name="Geometry" type="Spatial" parent="."]
transform = Transform( 2.12132, 0, -2.12132, 0, 3, 0, 2.12132, 0, 2.12132, 0, 0, 0 )
[node name="resourceWood" parent="Spatial" instance=ExtResource( 2 )]
[node name="resourceWood" parent="Geometry" instance=ExtResource( 2 )]
[node name="CollectibleComponent" parent="." instance=ExtResource( 3 )]

View File

@ -415,13 +415,19 @@ WorldNode = NodePath("../World")
[node name="WorldInfo" parent="Player" index="2"]
WorldPath = NodePath("../../World")
[node name="Skeleton" parent="Player/Geometry/PirateAsset/Armature" index="0"]
bones/4/bound_children = [ NodePath("ToolAttachement") ]
[node name="ToolAttachement" parent="Player/Geometry/PirateAsset/Armature/Skeleton" index="5"]
transform = Transform( 1, 8.68458e-08, -1.04308e-07, 1.74623e-07, -1, -1.30385e-07, 1.41561e-07, 1.50874e-07, -1, -0.72, 0.45, 3.28113e-08 )
[node name="AnimationTree" parent="Player/Geometry" index="1"]
parameters/playback = SubResource( 26 )
[node name="Entities" type="Spatial" parent="."]
[node name="Axe" parent="Entities" instance=ExtResource( 14 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1.79762, 0, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -1.03292, -2.38419e-07, -4.33215 )
input_ray_pickable = false
[node name="Chest" parent="Entities" instance=ExtResource( 11 )]