221 lines
6.6 KiB
C#
221 lines
6.6 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
|
|
|
|
public partial class Actor : Sprite2D
|
|
{
|
|
public Sprite2D _markNormal, _markPressed, _markHovered, _markDisabled, _markFocused;
|
|
public Board _board;
|
|
public override void _Ready()
|
|
{
|
|
_markNormal = GetNode<Sprite2D>("MarkNormal");
|
|
_markPressed = GetNode<Sprite2D>("MarkPressed");
|
|
_markHovered = GetNode<Sprite2D>("MarkHovered");
|
|
_markDisabled = GetNode<Sprite2D>("MarkDisabled");
|
|
_markFocused = GetNode<Sprite2D>("MarkFocused");
|
|
}
|
|
|
|
// CALLABLE ACTIONS
|
|
|
|
public bool RotateCells(int ROTATIONS = 1)
|
|
{
|
|
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
|
|
List<int> addresses = new(_board._cells.Select(c=>c._address).ToList());
|
|
Dictionary<int, int> cwShifts = new()
|
|
{
|
|
{0,1}, {1,2}, {2,5}, {3,0}, {4,4}, {5,8}, {6,3}, {7,6}, {8,7}
|
|
};
|
|
Dictionary<int, int> ccwShifts = new()
|
|
{
|
|
{0,3}, {1,0}, {2,1}, {3,6}, {4,4}, {5,2}, {6,7}, {7,8}, {8,5}
|
|
};
|
|
Dictionary<int, int> shifts = ROTATIONS > 0 ? cwShifts : ccwShifts;
|
|
|
|
for (int i = 0; i < Math.Abs(ROTATIONS); i++)
|
|
{
|
|
for (int j = 0; j < addresses.Count; j++)
|
|
{
|
|
int address = addresses[j];
|
|
int newAddress = shifts[address];
|
|
addresses[j] = newAddress;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < _board._cells.Count; i++)
|
|
{
|
|
int newCellAddress = addresses[i];
|
|
_board._cells[i].Position = positions[newCellAddress];
|
|
}
|
|
|
|
_board.RenumberCells();
|
|
return true;
|
|
}
|
|
|
|
public bool RotateMarks(int ROTATIONS = 1)
|
|
{
|
|
List<Cell> cells = new(_board._cells);
|
|
List<int> addresses = new(_board._cells.Select(c=>c._address).ToList());
|
|
Dictionary<int, int> cwShifts = new()
|
|
{
|
|
{0,1}, {1,2}, {2,5}, {3,0}, {4,4}, {5,8}, {6,3}, {7,6}, {8,7}
|
|
};
|
|
Dictionary<int, int> ccwShifts = new()
|
|
{
|
|
{0,3}, {1,0}, {2,1}, {3,6}, {4,4}, {5,2}, {6,7}, {7,8}, {8,5}
|
|
};
|
|
Dictionary<int, int> shifts = ROTATIONS > 0 ? cwShifts : ccwShifts;
|
|
|
|
for (int i = 0; i < Math.Abs(ROTATIONS); i++)
|
|
{
|
|
for (int j = 0; j < addresses.Count; j++)
|
|
{
|
|
int address = addresses[j];
|
|
int newAddress = shifts[address];
|
|
addresses[j] = newAddress;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < _board._cells.Count; i++)
|
|
{
|
|
int newCellAddress = addresses[i];
|
|
_board._cells[i].Mark(cells[newCellAddress]._marker);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool ShuffleCells()
|
|
{
|
|
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
|
|
Globals.Shuffle(positions);
|
|
for (int i = 0; i < _board._cells.Count; i++)
|
|
{
|
|
_board._cells[i].Position = positions[i];
|
|
}
|
|
_board.RenumberCells();
|
|
return true;
|
|
}
|
|
|
|
public bool ShuffleMarks()
|
|
{
|
|
List<Cell> cells = new(_board._cells);
|
|
Globals.Shuffle(cells);
|
|
for (int i = 0; i < _board._cells.Count; i++)
|
|
{
|
|
_board._cells[i].Mark(cells[i]._marker);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool ShiftCells(int SHIFT_ROWS_BY = 0,int SHIFT_COLUMNS_BY = 0)
|
|
{
|
|
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
|
|
int[,] addressTable = {{0,1,2},{3,4,5},{6,7,8}};
|
|
List<int> rows = new(){0,1,2};
|
|
List<int> columns = new(){0,1,2};
|
|
|
|
rows = Globals.ShiftList(rows, SHIFT_ROWS_BY);
|
|
columns = Globals.ShiftList(columns, SHIFT_COLUMNS_BY);
|
|
|
|
for (int i = 0; i < rows.Count; i++)
|
|
{
|
|
for (int j = 0; j < columns.Count; j++)
|
|
{
|
|
_board._cells[addressTable[rows[i],columns[j]]].Position = positions[addressTable[i,j]];
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool ShiftMarks(int SHIFT_ROWS_BY = 0,int SHIFT_COLUMNS_BY = 0)
|
|
{
|
|
List<Actor> markers = new(_board._cells.Select(c=>c._marker).ToList());
|
|
int[,] addressTable = {{0,1,2},{3,4,5},{6,7,8}};
|
|
List<int> rows = new(){0,1,2};
|
|
List<int> columns = new(){0,1,2};
|
|
|
|
rows = Globals.ShiftList(rows, SHIFT_ROWS_BY);
|
|
columns = Globals.ShiftList(columns, SHIFT_COLUMNS_BY);
|
|
|
|
for (int i = 0; i < rows.Count; i++)
|
|
{
|
|
for (int j = 0; j < columns.Count; j++)
|
|
{
|
|
_board._cells[addressTable[rows[i],columns[j]]].Mark(markers[addressTable[i,j]]);
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
public bool SlideCells(int SHIFT_SPOTS = 1)
|
|
{
|
|
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
|
|
int start = SHIFT_SPOTS > 0 ? 0 : 9;
|
|
for (int i = 0; i < _board._cells.Count; i++)
|
|
{
|
|
int newSpot = (start + i + SHIFT_SPOTS) % 9;
|
|
_board._cells[i].Position = positions[newSpot];
|
|
}
|
|
_board.RenumberCells();
|
|
return true;
|
|
}
|
|
|
|
public bool SlideMarks(int SHIFT_SPOTS = 1)
|
|
{
|
|
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
|
|
int start = SHIFT_SPOTS > 0 ? 0 : 9;
|
|
for (int i = 0; i < _board._cells.Count; i++)
|
|
{
|
|
int newSpot = (start + i + SHIFT_SPOTS) % 9;
|
|
_board._cells[i].Position = positions[newSpot];
|
|
}
|
|
_board.RenumberCells();
|
|
return true;
|
|
}
|
|
|
|
public bool SwapCells(int CELL_A_ADDRESS = -1, int CELL_B_ADDRESS = -1)
|
|
{
|
|
if (CELL_A_ADDRESS < -1 || CELL_A_ADDRESS > 8 || CELL_B_ADDRESS < -1 || CELL_B_ADDRESS > 8 || CELL_A_ADDRESS == CELL_B_ADDRESS)
|
|
{
|
|
return false;
|
|
}
|
|
List<Vector2> positions = new(_board._cells.Select(c=>c.Position).ToList());
|
|
if (CELL_A_ADDRESS == -1)
|
|
{
|
|
CELL_A_ADDRESS = Globals._rng.Next(0,8);
|
|
while (CELL_A_ADDRESS == CELL_B_ADDRESS)
|
|
{
|
|
CELL_A_ADDRESS = Globals._rng.Next(0,8);
|
|
}
|
|
}
|
|
if (CELL_B_ADDRESS == -1)
|
|
{
|
|
CELL_B_ADDRESS = Globals._rng.Next(0,8);
|
|
while (CELL_B_ADDRESS == CELL_A_ADDRESS)
|
|
{
|
|
CELL_B_ADDRESS = Globals._rng.Next(0,8);
|
|
}
|
|
}
|
|
_board._cells[CELL_A_ADDRESS].Position = positions[CELL_B_ADDRESS];
|
|
_board._cells[CELL_B_ADDRESS].Position = positions[CELL_A_ADDRESS];
|
|
_board.RenumberCells();
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
//MoveMark
|
|
|
|
|
|
|
|
}
|