Big brain time
This commit is contained in:
9
Scripts/Blizzard.cs
Normal file
9
Scripts/Blizzard.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Godot;
|
||||
|
||||
public partial class Blizzard : Projectile
|
||||
{
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
Translate(new Vector3(0, 0, -Speed));
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,9 @@
|
||||
using Godot;
|
||||
|
||||
public partial class Fireball : CsgSphere3D
|
||||
public partial class Fireball : Projectile
|
||||
{
|
||||
private float _speed = 80f;
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
}
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
Translate(new Vector3(0, 0, -Speed));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,82 +1,93 @@
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
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 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;
|
||||
|
||||
private Node3D _cameraMount;
|
||||
private Node3D _pivot;
|
||||
private Node3D _cameraMount;
|
||||
private Node3D _pivot;
|
||||
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");
|
||||
_pivot = GetNode<Node3D>("/root/Main/Player/Pivot");
|
||||
}
|
||||
public override void _Ready()
|
||||
{
|
||||
Input.MouseMode = Input.MouseModeEnum.Captured;
|
||||
_cameraMount = GetNode<Node3D>("/root/Main/Player/CameraMount");
|
||||
_pivot = GetNode<Node3D>("/root/Main/Player/Pivot");
|
||||
_weaponType = _projectiles.First();
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (Input.IsActionJustPressed("quit"))
|
||||
GetTree().Quit();
|
||||
if (Input.IsActionJustPressed("shoot"))
|
||||
Shoot();
|
||||
}
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
{
|
||||
if (Input.IsActionJustPressed("quit"))
|
||||
GetTree().Quit();
|
||||
if (Input.IsActionJustPressed("shoot"))
|
||||
Shoot();
|
||||
if (Input.IsActionJustPressed("switch"))
|
||||
SwitchWeaponType();
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
}
|
||||
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 void Shoot()
|
||||
{
|
||||
GD.Print("Shoot");
|
||||
var projectile = _weaponType.Instantiate<Node3D>();
|
||||
projectile.Position = Position;
|
||||
GetParent().AddChild(projectile);
|
||||
}
|
||||
|
||||
private void Shoot()
|
||||
{
|
||||
GD.Print("Shoot");
|
||||
var projectile = new Fireball();
|
||||
Owner.AddChild(projectile);
|
||||
projectile.Transform = Transform;
|
||||
}
|
||||
private void SwitchWeaponType()
|
||||
{
|
||||
var index = _projectiles.IndexOf(_weaponType);
|
||||
_weaponType = _projectiles[(index + 1) % _projectiles.Count];
|
||||
|
||||
private Vector3 CalculateMovement(Vector3 velocity, Transform3D transform, float gravityDelta)
|
||||
{
|
||||
if (!IsOnFloor())
|
||||
velocity.Y -= gravityDelta;
|
||||
}
|
||||
|
||||
if (Input.IsActionJustPressed("jump") && IsOnFloor())
|
||||
velocity.Y = _jumpVelocity;
|
||||
private Vector3 CalculateMovement(Vector3 velocity, Transform3D transform, float gravityDelta)
|
||||
{
|
||||
if (!IsOnFloor())
|
||||
velocity.Y -= gravityDelta;
|
||||
|
||||
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);
|
||||
}
|
||||
if (Input.IsActionJustPressed("jump") && IsOnFloor())
|
||||
velocity.Y = _jumpVelocity;
|
||||
|
||||
return velocity;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
14
Scripts/Projectile.cs
Normal file
14
Scripts/Projectile.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Godot;
|
||||
|
||||
public partial class Projectile : Node3D
|
||||
{
|
||||
[Export]
|
||||
private float _projectileSpeed = 0.1f;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Speed = _projectileSpeed;
|
||||
}
|
||||
|
||||
public float Speed { get; private set; }
|
||||
}
|
||||
8
Scripts/WeaponType.cs
Normal file
8
Scripts/WeaponType.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace Sandbox.Scripts
|
||||
{
|
||||
internal enum WeaponType
|
||||
{
|
||||
Fire = 0,
|
||||
Blizzard = 1
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user