Add collision detection

This commit is contained in:
Steven Long
2023-07-19 09:40:16 -07:00
parent f15e1031bd
commit e2bb5ec656
10 changed files with 213 additions and 198 deletions

View File

@@ -2,49 +2,49 @@ using Godot;
public partial class CameraSystem : SpringArm3D
{
[Export]
private float _sensitivityHorizontal = 0.05f;
[Export]
private float _sensitivityVertical = 0.05f;
[Export]
private float _joystickDeadZone = 0.5f;
[Export]
private float _sensitivityHorizontal = 0.05f;
[Export]
private float _sensitivityVertical = 0.05f;
[Export]
private float _joystickDeadZone = 0.5f;
private Node3D _pivot;
private CharacterBody3D _characterBody;
private Node3D _pivot;
private CharacterBody3D _characterBody;
public override void _Ready()
{
_pivot = GetNode<Node3D>("/root/Main/Player/Pivot");
_characterBody = GetNode<CharacterBody3D>("/root/Main/Player");
}
public override void _Ready()
{
_pivot = GetNode<Node3D>("/root/Main/Player/Pivot");
_characterBody = GetNode<CharacterBody3D>("/root/Main/Player");
}
public override void _Process(double delta)
{
var joyRightX = Input.GetJoyAxis(0, JoyAxis.RightX);
var joyRightY = Input.GetJoyAxis(0, JoyAxis.RightY);
public override void _Process(double delta)
{
var joyRightX = Input.GetJoyAxis(0, JoyAxis.RightX);
var joyRightY = Input.GetJoyAxis(0, JoyAxis.RightY);
if (Mathf.Abs(joyRightX) > _joystickDeadZone)
{
var rotateAmount = joyRightX * _sensitivityHorizontal;
_characterBody.RotateY(-rotateAmount);
_pivot.RotateY(rotateAmount);
}
if (Mathf.Abs(joyRightX) > _joystickDeadZone)
{
var rotateAmount = joyRightX * _sensitivityHorizontal;
_characterBody.RotateY(-rotateAmount);
_pivot.RotateY(rotateAmount);
}
if (Mathf.Abs(joyRightY) > _joystickDeadZone)
{
RotateX(joyRightY * _sensitivityVertical);
Rotation = new Vector3(Mathf.Clamp(Rotation.X, -0.7f, 0.7f), Rotation.Y, Rotation.Z);
}
}
if (Mathf.Abs(joyRightY) > _joystickDeadZone)
{
RotateX(joyRightY * _sensitivityVertical);
Rotation = new Vector3(Mathf.Clamp(Rotation.X, -0.7f, 0.7f), Rotation.Y, Rotation.Z);
}
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseMotion mouseMotion)
{
_characterBody.RotateY(Mathf.DegToRad(-mouseMotion.Relative.X * _sensitivityHorizontal));
_pivot.RotateY(Mathf.DegToRad(mouseMotion.Relative.X * _sensitivityHorizontal));
RotateX(Mathf.DegToRad(mouseMotion.Relative.Y * _sensitivityVertical));
Rotation = new Vector3(Mathf.Clamp(Rotation.X, -0.7f, 0.7f), Rotation.Y, Rotation.Z);
}
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseMotion mouseMotion)
{
_characterBody.RotateY(Mathf.DegToRad(-mouseMotion.Relative.X * _sensitivityHorizontal));
_pivot.RotateY(Mathf.DegToRad(mouseMotion.Relative.X * _sensitivityHorizontal));
RotateX(Mathf.DegToRad(mouseMotion.Relative.Y * _sensitivityVertical));
Rotation = new Vector3(Mathf.Clamp(Rotation.X, -0.7f, 0.7f), Rotation.Y, Rotation.Z);
}
}
}

View File

@@ -1,21 +0,0 @@
[remap]
importer="wavefront_obj"
importer_version=1
type="Mesh"
uid="uid://cj246dmena0if"
path="res://.godot/imported/Cookie.obj-9c7fa2a3d48746d9f2fd322d3854502d.mesh"
[deps]
files=["res://.godot/imported/Cookie.obj-9c7fa2a3d48746d9f2fd322d3854502d.mesh"]
source_file="res://Scripts/Cookie.obj"
dest_files=["res://.godot/imported/Cookie.obj-9c7fa2a3d48746d9f2fd322d3854502d.mesh", "res://.godot/imported/Cookie.obj-9c7fa2a3d48746d9f2fd322d3854502d.mesh"]
[params]
generate_tangents=true
scale_mesh=Vector3(1, 1, 1)
offset_mesh=Vector3(0, 0, 0)
optimize_mesh=true

13
Scripts/Enemy.cs Normal file
View File

@@ -0,0 +1,13 @@
using Godot;
public partial class Enemy : Node
{
public void OnHit(Variant body)
{
if (body.Obj is Projectile)
{
GD.Print("Hit");
QueueFree();
}
}
}

View File

@@ -2,8 +2,8 @@ using Godot;
public partial class Fireball : Projectile
{
public override void _PhysicsProcess(double delta)
{
Translate(new Vector3(0, 0, Speed * (float)delta));
}
public override void _PhysicsProcess(double delta)
{
Translate(new Vector3(0, 0, Speed * (float)delta));
}
}

View File

@@ -4,102 +4,102 @@ using Godot.Collections;
public partial class Player : CharacterBody3D
{
[Export]
private float _speed = 5.0f;
[Export]
private float _jumpVelocity = 4.5f;
[Export]
private float _sensitivityHorizontal = 0.05f;
[Export]
private float _sensitivityVertical = 0.05f;
[Export]
private float _joystickDeadZone = 0.5f;
[Export]
private Array<PackedScene> _projectiles;
[Export]
private float _speed = 5.0f;
[Export]
private float _jumpVelocity = 4.5f;
[Export]
private float _sensitivityHorizontal = 0.05f;
[Export]
private float _sensitivityVertical = 0.05f;
[Export]
private float _joystickDeadZone = 0.5f;
[Export]
private Array<PackedScene> _projectiles;
public bool CanShoot { get; private set; }
public bool CanShoot { get; private set; }
private Node3D _cameraMount;
private Node3D _pivot;
private Node3D _camera;
private PackedScene _weaponType;
private Node3D _cameraMount;
private Node3D _pivot;
private Node3D _camera;
private PackedScene _weaponType;
// Get the _gravity from the project settings to be synced with RigidBody nodes.
public float _gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
// Get the _gravity from the project settings to be synced with RigidBody nodes.
public float _gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Captured;
_cameraMount = GetNode<Node3D>("/root/Main/Player/CameraMount");
_camera = GetNode<Node3D>("/root/Main/Player/CameraMount/Camera");
_pivot = GetNode<Node3D>("/root/Main/Player/Pivot");
_weaponType = _projectiles.First();
CanShoot = true;
}
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Captured;
_cameraMount = GetNode<Node3D>("/root/Main/Player/CameraMount");
_camera = GetNode<Node3D>("/root/Main/Player/CameraMount/Camera");
_pivot = GetNode<Node3D>("/root/Main/Player/Pivot");
_weaponType = _projectiles.First();
CanShoot = true;
}
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("quit"))
GetTree().Quit();
if (Input.IsActionJustPressed("shoot") && CanShoot)
Shoot();
if (Input.IsActionJustPressed("switch"))
SwitchWeaponType();
}
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("quit"))
GetTree().Quit();
if (Input.IsActionJustPressed("shoot") && CanShoot)
Shoot();
if (Input.IsActionJustPressed("switch"))
SwitchWeaponType();
}
public override void _PhysicsProcess(double delta)
{
var gravityDelta = _gravity * (float)delta;
var velocity = CalculateMovement(Velocity, Transform, gravityDelta);
public override void _PhysicsProcess(double delta)
{
var gravityDelta = _gravity * (float)delta;
var velocity = CalculateMovement(Velocity, Transform, gravityDelta);
Velocity = velocity;
MoveAndSlide();
}
Velocity = velocity;
MoveAndSlide();
}
private async void Shoot()
{
GD.Print("Shoot");
var projectile = _weaponType.Instantiate<Projectile>();
projectile.Position = Position;
projectile.Rotation = Rotation;
GetParent().AddChild(projectile);
CanShoot = false;
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
CanShoot = true;
}
private async void Shoot()
{
GD.Print("Shoot");
var projectile = _weaponType.Instantiate<Projectile>();
projectile.Position = Position;
projectile.Rotation = Rotation;
GetParent().AddChild(projectile);
CanShoot = false;
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
CanShoot = true;
}
private async void SwitchWeaponType()
{
var index = _projectiles.IndexOf(_weaponType);
_weaponType = _projectiles[(index + 1) % _projectiles.Count];
CanShoot = false;
await ToSignal(GetTree().CreateTimer(.5), "timeout");
CanShoot = true;
private async void SwitchWeaponType()
{
var index = _projectiles.IndexOf(_weaponType);
_weaponType = _projectiles[(index + 1) % _projectiles.Count];
CanShoot = false;
await ToSignal(GetTree().CreateTimer(.5), "timeout");
CanShoot = true;
}
}
private Vector3 CalculateMovement(Vector3 velocity, Transform3D transform, float gravityDelta)
{
if (!IsOnFloor())
velocity.Y -= gravityDelta;
private Vector3 CalculateMovement(Vector3 velocity, Transform3D transform, float gravityDelta)
{
if (!IsOnFloor())
velocity.Y -= gravityDelta;
if (Input.IsActionJustPressed("jump") && IsOnFloor())
velocity.Y = _jumpVelocity;
if (Input.IsActionJustPressed("jump") && IsOnFloor())
velocity.Y = _jumpVelocity;
Vector2 inputDir = Input.GetVector("right", "left", "back", "forward");
Vector3 direction = (transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
if (direction != Vector3.Zero)
{
velocity.X = direction.X * _speed;
velocity.Z = direction.Z * _speed;
GetNode<Node3D>("Pivot").LookAt(Position + direction.Normalized(), Vector3.Up);
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed);
}
Vector2 inputDir = Input.GetVector("right", "left", "back", "forward");
Vector3 direction = (transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
if (direction != Vector3.Zero)
{
velocity.X = direction.X * _speed;
velocity.Z = direction.Z * _speed;
GetNode<Node3D>("Pivot").LookAt(Position + direction.Normalized(), Vector3.Up);
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed);
}
return velocity;
}
return velocity;
}
}

View File

@@ -2,21 +2,21 @@
public partial class Projectile : Node3D
{
[Export]
public double Cooldown { get; protected set; }
[Export]
public double Cooldown { get; protected set; }
[Export]
private float _projectileSpeed = 0.1f;
[Export]
private float _projectileSpeed = 100f;
public override void _Ready()
{
Speed = _projectileSpeed;
}
public override void _Ready()
{
Speed = _projectileSpeed;
}
public float Speed { get; private set; }
public float Speed { get; private set; }
public void OnTimeToLiveTimeout()
{
QueueFree();
}
public void OnTimeToLiveTimeout()
{
QueueFree();
}
}