supervisor-simulator/scripts/Student.cs

95 lines
2.4 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
public partial class Student : CharacterBody2D
{
public float Speed { get; set; } = 10.0f;
public const float JumpVelocity = -400.0f;
public int NextType = -1;
private Queue<Vector2I> PathToGo = new();
public override void _PhysicsProcess(double delta)
{
if (PathToGo.Count == 0)
{
return;// No path to follow.
}
Vector2 velocity = new();
var nextPoint = PathToGo.Peek();
if ((int)GlobalPosition.X == nextPoint.X && (int)GlobalPosition.Y == nextPoint.Y) {
GD.Print(PathToGo.Dequeue());
}
nextPoint = PathToGo.Peek();
// if ((int)GlobalPosition.X == nextPoint.X) {
// // Move Y
// // velocity.Y = Math.Max(Speed, Math.Abs(nextPoint.Y - GlobalPosition.Y));
// velocity.Y = Speed;
// if (GlobalPosition.Y > nextPoint.Y) {
// velocity.Y = -velocity.Y;
// }
// } else if ((int)GlobalPosition.Y == nextPoint.Y) {
// // move X
// // velocity.X = Math.Max(Speed, Math.Abs(nextPoint.X - GlobalPosition.X));
// velocity.X = Speed;
// if (GlobalPosition.X > nextPoint.X) {
// velocity.X = -velocity.X;
// }
// }
if ((int)GlobalPosition.X == nextPoint.X) {
// Move Y
// velocity.Y = Math.Max(Speed, Math.Abs(nextPoint.Y - GlobalPosition.Y));
velocity.Y = Speed;
if (GlobalPosition.Y > nextPoint.Y) {
velocity.Y = -velocity.Y;
}
} else if ((int)GlobalPosition.Y == nextPoint.Y) {
// move X
// velocity.X = Math.Max(Speed, Math.Abs(nextPoint.X - GlobalPosition.X));
velocity.X = Speed;
if (GlobalPosition.X > nextPoint.X) {
velocity.X = -velocity.X;
}
}
GlobalPosition = GlobalPosition with {
X = GlobalPosition.X + velocity.X,
Y = GlobalPosition.Y + velocity.Y
};
// Velocity = velocity;
MoveAndSlide();
}
public override void _Ready()
{
base._Ready();
var bt = GetNode<BTPlayer>("BTPlayer");
var bb = bt.Blackboard;
// bb.BindVarToProperty("Stayed", this, "Speed");
// GD.Print(bb.GetVar("Stayed"));
// GD.Print($"Speed: {Speed}");
}
public void MoveFollowPath(List<Vector2I> path)
{
foreach (var p in path)
{
PathToGo.Enqueue(new Vector2I(p.X * 80 + 40, p.Y * 80 + 120));
}
}
public void GoTo(int nType) {
GD.Print($"Called with nType = {nType}");
var lab = GetParentOrNull<Lab>();
if (lab == null) {
return;
}
var block = lab.GetTypedBlock((Lab.MapNodeType)nType, GD.Randi());
MoveFollowPath(lab.GetShortestPath(lab.Point2Coord(GlobalPosition), block));
}
}