131 lines
2.8 KiB
C#
131 lines
2.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// Represents tile
|
|
/// </summary>
|
|
///
|
|
|
|
// ee dc bb aa
|
|
// aa = color
|
|
// bb = state/kind
|
|
// c = special
|
|
// d = activating/animation
|
|
// ee = trash value
|
|
|
|
public enum TileColor : byte {
|
|
Blue = 0,
|
|
Red = 1,
|
|
Yellow = 2,
|
|
Green = 3
|
|
}
|
|
|
|
public enum TileKind : byte {
|
|
Air = 0,
|
|
Block = 4,
|
|
Activator = 8,
|
|
Special = 12,
|
|
Activiting = 16,
|
|
Trash = 20
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper data structure for determining anything about a tile.
|
|
/// </summary>
|
|
[System.Serializable] // I guess so we can serialize patterns
|
|
public class TileInfo {
|
|
static uint _instanceCounter;
|
|
private static byte _instance {
|
|
get {
|
|
return (byte)(_instanceCounter++ & 0x7F);
|
|
}
|
|
}
|
|
|
|
public short dat;
|
|
|
|
public TileInfo(short b) => dat = b;
|
|
|
|
// Removing so that the counter one works better
|
|
/*public TileInfo(TileKind tk, TileColor tc, byte tileValue ) {
|
|
dat = (short)((byte)tk | (byte)tc | (tileValue << 8));
|
|
}*/
|
|
|
|
public static int ToPairInt((TileInfo left, TileInfo right) pair) {
|
|
return ((int)pair.left.dat << 16) | (int)pair.right.dat;
|
|
}
|
|
public static (TileInfo left, TileInfo right) FromPairInt(int i) {
|
|
return ((TileInfo)(short)(i >> 16), (TileInfo)(short)(i & 0x00FF));
|
|
}
|
|
public TileInfo(TileKind tk, TileColor tc, byte counter) {
|
|
dat = (short)((byte)tk | (byte)tc | (counter << 5) | (_instance << 8));
|
|
}
|
|
|
|
public TileInfo(TileKind tk, TileColor tc) {
|
|
dat = (short)((byte)tk | (byte)tc | (_instance << 8));
|
|
}
|
|
|
|
|
|
public void Deconstruct(out short b) => b = dat;
|
|
|
|
private static TileColor getRandomColor {
|
|
get {
|
|
return (TileColor)Random.Range(0, 4);
|
|
}
|
|
}
|
|
|
|
public static TileInfo CreateAirTile() {
|
|
return new TileInfo(TileKind.Air, TileColor.Blue);
|
|
}
|
|
|
|
public static TileInfo CreateRandomBlockTile() {
|
|
return new TileInfo(TileKind.Block, getRandomColor);
|
|
}
|
|
|
|
public static TileInfo CreateRandomActivatorTile() {
|
|
return new TileInfo(TileKind.Activator, getRandomColor);
|
|
}
|
|
|
|
public static TileInfo CreateSpecialTile() {
|
|
return new TileInfo(TileKind.Special, TileColor.Blue);
|
|
}
|
|
|
|
public static TileInfo CreateRandomTrashTile() {
|
|
return new TileInfo(TileKind.Trash, getRandomColor, 5);
|
|
}
|
|
|
|
public static void SetAirTile(TileInfo ti) {
|
|
ti.kind = TileKind.Air;
|
|
ti.color = TileColor.Blue;
|
|
}
|
|
|
|
public TileColor color {
|
|
get => (TileColor)(dat & 0x0003);
|
|
set => dat = (short)((dat & 0xFFFC) | (byte)value);
|
|
}
|
|
|
|
|
|
|
|
public TileKind kind {
|
|
get => (TileKind)(dat & 0x001C);
|
|
set => dat = (short)((dat & 0xFFE3) + (byte)value);
|
|
}
|
|
|
|
public byte counter {
|
|
get => (byte)((dat & 0x00E0) >> 5);
|
|
set => dat = (short)((dat & 0xFF1F) | (value << 5));
|
|
}
|
|
|
|
public byte tileValue {
|
|
get => (byte)(dat >> 8);
|
|
}
|
|
|
|
public static implicit operator short(TileInfo ti) {
|
|
return ti.dat;
|
|
}
|
|
|
|
public static explicit operator TileInfo(short b) {
|
|
return new TileInfo(b);
|
|
}
|
|
}
|