Basic player & enemy movement.
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class GameController : Node
|
||||
{
|
||||
public static GameController Instance;
|
||||
|
||||
[Export] public Node2D Player;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
GameController.Instance = this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://djp8dmixf6sq2
|
||||
@@ -0,0 +1,26 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class EnemyMovement : Node
|
||||
{
|
||||
[Export] private NavigationAgent2D _agent2D;
|
||||
[Export] private CharacterBody2D _body2D;
|
||||
[Export] private float _speed;
|
||||
|
||||
private double _time = 0;
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
_time += delta;
|
||||
if (_time > 0.2)
|
||||
{
|
||||
_time = 0;
|
||||
_agent2D.SetTargetPosition(GameController.Instance.Player.GlobalPosition);
|
||||
}
|
||||
// _parent.Position = _agent2D.GetNextPathPosition();
|
||||
var gpos = _agent2D.GetNextPathPosition();
|
||||
var dir = (gpos - _body2D.GlobalPosition).Normalized();
|
||||
// _body2D.SetVelocity(dir * _speed * (float)delta);
|
||||
_body2D.GlobalPosition += dir * _speed * (float)delta;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cu37tswrk107q
|
||||
@@ -0,0 +1,31 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class PlayerMovement : Node
|
||||
{
|
||||
[Export] private CharacterBody2D _body2D;
|
||||
|
||||
[Export] private float _speed;
|
||||
|
||||
private Vector2 _direction = new ();
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
ReadInput();
|
||||
}
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
float dt = (float)delta;
|
||||
|
||||
_body2D.SetVelocity(_direction * _speed * dt);
|
||||
_body2D.MoveAndSlide();
|
||||
}
|
||||
|
||||
private void ReadInput()
|
||||
{
|
||||
_direction.X = Input.GetAxis("move_left", "move_right");
|
||||
_direction.Y = Input.GetAxis("move_up", "move_down");
|
||||
_direction = _direction.Normalized();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://8drbh4rcl410
|
||||
Reference in New Issue
Block a user