198 lines
4.7 KiB
C#
198 lines
4.7 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
/// TODO alter code to player vs computer to account for differing logic
|
|
public partial class Manager : Node2D
|
|
{
|
|
public bool _dead, _ready, _moving;
|
|
public int _ballsMoving = 0, _health = 10, _healthMax, _speed = 5;
|
|
public string _imagePath;
|
|
public Vector2 _deskPosition;
|
|
public List<Vector2> _movements = new();
|
|
public CollisionShape2D _startArea;
|
|
public ManagerPanel _managerPanel = null;
|
|
public Sprite2D _image;
|
|
public Desk _desk = null;
|
|
public Manager _opponent;
|
|
public List<Worker> _workers = new();
|
|
public Node _workerNode;
|
|
public Worker _hoveredWorker, _selectedWorker, _heldWorker;
|
|
// public List<Tchotchke> _tchotckes = new();
|
|
|
|
public override void _Ready()
|
|
{
|
|
_workerNode = GetNode("Workers");
|
|
_healthMax = _health;
|
|
|
|
SetSprite("res://art/ness.png");
|
|
|
|
_managerPanel = GetNode<ManagerPanel>("Panel");
|
|
_desk = GetNode<Desk>("Desk");
|
|
|
|
_deskPosition = new Vector2(1, 1);
|
|
// _movements.Insert(0, _deskPosition);
|
|
_image = GetNode<Sprite2D>("Image");
|
|
_image.GlobalPosition = _desk.GetPositionFromAddress(1, 1);
|
|
|
|
_managerPanel.SetManager(this);
|
|
for (int i = 0; i < Globals.Instance._random.Next(3, 6); i++)
|
|
{
|
|
AddWorker(null);
|
|
}
|
|
}
|
|
|
|
public override void _PhysicsProcess(double DELTA_)
|
|
{
|
|
ChainMovement();
|
|
ChainSelection();
|
|
}
|
|
|
|
public void AddWorker(Worker NEWWORKER)
|
|
{
|
|
Worker newWorker = NEWWORKER ?? Globals.Instance._workerScene.Instantiate<Worker>();
|
|
newWorker._deskPosition = new Vector2(1, 1);
|
|
newWorker.Position = _desk.GetPositionFromAddress(1, 1);
|
|
newWorker._manager = this;
|
|
_workers.Add(newWorker);
|
|
_workerNode.AddChild(newWorker);
|
|
newWorker.SetHovered += SetHoveredWorker;
|
|
}
|
|
|
|
public void ChainMovement()
|
|
{
|
|
Vector2 direction = Vector2.Zero;
|
|
if (Input.IsActionJustPressed("move_up"))
|
|
{
|
|
direction = Vector2.Up;
|
|
}
|
|
else if (Input.IsActionJustPressed("move_down"))
|
|
{
|
|
direction = Vector2.Down;
|
|
}
|
|
else if (Input.IsActionJustPressed("move_left"))
|
|
{
|
|
direction = Vector2.Left;
|
|
}
|
|
else if (Input.IsActionJustPressed("move_right"))
|
|
{
|
|
direction = Vector2.Right;
|
|
}
|
|
|
|
if (direction != Vector2.Zero)
|
|
{
|
|
Vector2 newPostion = _desk.GetPositionFromAddress((int)(_deskPosition.Y + direction.Y), (int)(_deskPosition.X + direction.X));
|
|
if (newPostion != new Vector2(-1, -1))
|
|
{
|
|
if (_movements.Count > 0)
|
|
{
|
|
for (int i = 0; i < _workers.Count && i < _movements.Count; i++)
|
|
{
|
|
_workers[i].GlobalPosition = _desk.GetPositionFromAddress((int)_movements[i].Y, (int)_movements[i].X);
|
|
}
|
|
}
|
|
_deskPosition += direction;
|
|
_movements.Insert(0, _deskPosition);
|
|
_image.GlobalPosition = newPostion;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ChainSelection()
|
|
{
|
|
if (_heldWorker != null)
|
|
{
|
|
_heldWorker.GlobalPosition = GetGlobalMousePosition();
|
|
if (_hoveredWorker != null && _heldWorker != _hoveredWorker)
|
|
{
|
|
SwapPositions(_heldWorker, _hoveredWorker);
|
|
_hoveredWorker._hovered = false;
|
|
_hoveredWorker = null;
|
|
}
|
|
if (Input.IsActionJustReleased("left_click"))
|
|
{
|
|
_heldWorker.GlobalPosition = _heldWorker._chainPosition;
|
|
_heldWorker._held = false;
|
|
_heldWorker = null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (_selectedWorker != null)
|
|
{
|
|
if (Input.IsActionPressed("left_click") && (GetGlobalMousePosition() - _selectedWorker._chainPosition).Length() > 5)
|
|
{
|
|
|
|
_heldWorker = _selectedWorker;
|
|
_heldWorker._held = true;
|
|
_heldWorker._selected = false;
|
|
_selectedWorker = null;
|
|
}
|
|
}
|
|
if (_hoveredWorker != null)
|
|
{
|
|
if (Input.IsActionJustPressed("left_click"))
|
|
{
|
|
_selectedWorker = _hoveredWorker;
|
|
_selectedWorker._selected = true;
|
|
_selectedWorker._hovered = false;
|
|
_hoveredWorker = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ChangeHealth(int CHANGE)
|
|
{
|
|
_health += CHANGE;
|
|
_health = Math.Min(_health, _healthMax);
|
|
|
|
if (_health < 0)
|
|
{
|
|
_dead = true;
|
|
_health = 0;
|
|
}
|
|
|
|
GetNode<ManagerPanel>("Panel").SetValue(_health);
|
|
}
|
|
|
|
public void SetSprite(string PATH)
|
|
{
|
|
_imagePath = PATH;
|
|
}
|
|
|
|
// public void Start()
|
|
// {
|
|
// _workers[0].GlobalPosition = _desk.GetPositionFromAddress(1, 1);
|
|
// }
|
|
|
|
public void SwapPositions(Worker A, Worker B)
|
|
{
|
|
Vector2 chainPositionA = A._chainPosition, chainPositionB = B._chainPosition;
|
|
List<Vector2> movesA = new(A._moves), movesB = new(B._moves);
|
|
B.Position = chainPositionA;
|
|
A._chainPosition = chainPositionB;
|
|
B._chainPosition = chainPositionA;
|
|
A._moves = movesB;
|
|
B._moves = movesA;
|
|
int indexA = _workers.IndexOf(A), indexB = _workers.IndexOf(B);
|
|
Worker C = A;
|
|
_workers[indexA] = B;
|
|
_workers[indexB] = C;
|
|
}
|
|
|
|
private void SetHoveredWorker(Worker HOVEREDWORKER)
|
|
{
|
|
if (HOVEREDWORKER._hovered)
|
|
{
|
|
_hoveredWorker = HOVEREDWORKER;
|
|
}
|
|
else
|
|
{
|
|
_hoveredWorker = null;
|
|
}
|
|
}
|
|
|
|
}
|