185 lines
5.1 KiB
C#
185 lines
5.1 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;
|
|
public int _ballsMoving = 0, _health = 10, _healthMax, _speed = 5;
|
|
public string _imagePath;
|
|
public CollisionShape2D _startArea;
|
|
public ManagerPanel _managerPanel = null;
|
|
public Manager _opponent;
|
|
public List<Worker> _workers = new();
|
|
public Worker _hoveredWorker, _selectedWorker, _heldWorker;
|
|
public Node _workerNode;
|
|
public List<Tchotchke> _tchotckes = new();
|
|
|
|
public override void _Ready()
|
|
{
|
|
_workerNode = GetNode("Workers");
|
|
_healthMax = _health;
|
|
|
|
SetSprite("res://art/ness.png");
|
|
|
|
_managerPanel = GetNode<ManagerPanel>("Panel");
|
|
|
|
_managerPanel.SetManager(this);
|
|
|
|
Worker newWorker = Globals.Instance._workerScene.Instantiate<Worker>();
|
|
newWorker.Position = Globals.Instance._screenCenter;
|
|
newWorker._manager = this;
|
|
_workerNode.AddChild(newWorker);
|
|
_workers.Add(newWorker);
|
|
|
|
newWorker = Globals.Instance._workerScene.Instantiate<Worker>();
|
|
newWorker.Position = Globals.Instance._screenCenter;
|
|
newWorker._manager = this;
|
|
_workerNode.AddChild(newWorker);
|
|
_workers.Add(newWorker);
|
|
|
|
newWorker = Globals.Instance._workerScene.Instantiate<Worker>();
|
|
newWorker.Position = Globals.Instance._screenCenter;
|
|
newWorker._manager = this;
|
|
_workerNode.AddChild(newWorker);
|
|
_workers.Add(newWorker);
|
|
|
|
newWorker = Globals.Instance._workerScene.Instantiate<Worker>();
|
|
newWorker.Position = Globals.Instance._screenCenter;
|
|
newWorker._manager = this;
|
|
_workerNode.AddChild(newWorker);
|
|
_workers.Add(newWorker);
|
|
|
|
// for (int i = 0; i < _workers.Count; i++)
|
|
// {
|
|
// _workers[i]._healthBar.Position = _managerPanel.GetNode<Panel>("Team").GetNode<Panel>("T"+(i+1)).GlobalPosition;
|
|
// }
|
|
|
|
// Tchotchke newTchotchke = ResourceLoader.Load<PackedScene>("res://Gameplay/Tchotchkes/awfully_hot_coffee_pot.tscn").Instantiate<Tchotchke>();
|
|
// newTchotchke.Position = new Vector2(Globals.Instance._screenSize.X - 100, Globals.Instance._screenCenter.Y);
|
|
// AddChild(newTchotchke);
|
|
// _tchotckes.Add(newTchotchke);
|
|
}
|
|
|
|
public override void _Process(double DELTA_)
|
|
{
|
|
if (Globals.Instance._battleRunning)
|
|
{
|
|
ChainMovement();
|
|
}
|
|
else
|
|
{
|
|
MoveChain();
|
|
}
|
|
}
|
|
|
|
public void ChainMovement()
|
|
{
|
|
Vector2 mousePosition = GetGlobalMousePosition();
|
|
// _workers[0].LookAt(mousePosition);
|
|
Vector2 mouseOffset = mousePosition - _workers[0].GlobalPosition;
|
|
if (mouseOffset.Length() > _workers[0]._size)
|
|
{
|
|
Vector2 mouseOffsetNormal = mouseOffset.Normalized();
|
|
_workers[0].GlobalPosition += mouseOffsetNormal * _speed;
|
|
_workers[0]._distanceTraveled += _speed;
|
|
_workers[0]._moves.Insert(0, _workers[0].GlobalPosition);
|
|
|
|
for (int i = 1; i < _workers.Count; i++)
|
|
{
|
|
if (_workers[i - 1]._distanceTraveled > (_workers[i - 1]._size + _workers[i]._size) * 1.2f)
|
|
{
|
|
_workers[i]._distanceTraveled += _speed;
|
|
_workers[i].GlobalPosition = _workers[i - 1]._moves[^1];
|
|
_workers[i]._moves.Insert(0, _workers[i - 1]._moves[^1]);
|
|
_workers[i - 1]._moves.RemoveAt(_workers[i - 1]._moves.Count - 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 MoveChain()
|
|
{
|
|
_hoveredWorker = _workers.SingleOrDefault(w => !w._selected && !w._held && w._hovered, null);
|
|
|
|
if (_heldWorker != null)
|
|
{
|
|
_heldWorker.GlobalPosition = GetGlobalMousePosition();
|
|
if (_hoveredWorker != null)
|
|
{
|
|
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 SetSprite(string PATH)
|
|
{
|
|
_imagePath = PATH;
|
|
}
|
|
|
|
public void SwapPositions(Worker A, Worker B)
|
|
{
|
|
Vector2 positionA = A.Position, positionB = B.Position, chainPositionA = A._chainPosition, chainPositionB = B._chainPosition;
|
|
List<Vector2> movesA = new(A._moves), movesB = new(B._moves);
|
|
// A.Position = positionB;
|
|
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;
|
|
}
|
|
|
|
}
|