supervisor-simulator/scripts/Student.cs
2024-12-06 17:14:41 +08:00

118 lines
3.3 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());
return;
}
// 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}");
var ap = GetNode<AnimationPlayer>("AnimationPlayer");
ap.Play("idle_front");
var name_test = GetNode<StudentName>("StudentName");
GD.Print("生成的名字是: " + name_test.GenerateName());
}
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));
randomChangeBody();
}
private void randomChangeBody()
{
GetNode<Sprite2D>("parts/body").Texture = (Texture2D)ResourceLoader.Load(Res.GetRandom(Res.Type.BODY));
GetNode<Sprite2D>("parts/hairstyle").Texture = (Texture2D)ResourceLoader.Load(Res.GetRandom(Res.Type.HAIR));
GetNode<Sprite2D>("parts/outfit").Texture = (Texture2D)ResourceLoader.Load(Res.GetRandom(Res.Type.OUTFIT));
GetNode<Sprite2D>("parts/eye").Texture = (Texture2D)ResourceLoader.Load(Res.GetRandom(Res.Type.EYE));
GetNode<Sprite2D>("parts/accessory").Texture = (Texture2D)ResourceLoader.Load(Res.GetRandom(Res.Type.ACCESSORY));
GetNode<Sprite2D>("parts/smartphone").Texture = (Texture2D)ResourceLoader.Load(Res.GetRandom(Res.Type.PHONE));
}
}