-
Notifications
You must be signed in to change notification settings - Fork 3
/
TileComponent.as
executable file
·132 lines (106 loc) · 4.11 KB
/
TileComponent.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package
{
import flash.geom.Point;
import org.flixel.FlxSprite;
import Components.*;
/**
* ...
* @author Nicholas 'A' Feinberg
*/
public class TileComponent extends FlxSprite {
public var gridIndex:int;
protected var gif:Class;
protected var id:String;
public function TileComponent(X:int, Y:int, gridIndex:int, facing:int, gif:Class, id:String, animated:Boolean = false) {
super(X * 16, Y * 16);
if (!animated) {
angle = facing * 90;
loadRotatedGraphic(gif, 4);
}
Manufactoria.grid[gridIndex] = this;
this.gridIndex = gridIndex;
this.facing = facing;
this.gif = gif;
this.id = id;
}
public function regenerationArguments():Array {
return new Array(x / 16, y / 16, gridIndex, facing, id);
}
public static function regenerate(component:TileComponent):TileComponent { //TODO: make grid-dim independent (or dependent?)
if (component is Source || component is Destination) return null;
var rargs:Array = component.regenerationArguments();
return new Manufactoria.COMPONENT_TYPES[Manufactoria.COMPONENTS_BY_ID.indexOf(rargs[4])](rargs[0], rargs[1], rargs[2], rargs[3]);
}
override public function set facing(newFacing:int):void {
if (_facing != newFacing) {
_facing = newFacing;
angle = 90 * _facing;
}
}
public function turnCW():void {
facing = (facing + 1) % 4;
}
public function turnCCW():void {
facing = (facing + 3) % 4;
}
public function mirror():void {
facing = (facing + 2) % 4;
}
public function hmirror():void {
if (!(facing & 1))
mirror();
}
public function direct(str:Robot):void {
str.facing = facing;
}
public function get identifier():String { return id }
public function die():Boolean {
Manufactoria.grid[gridIndex] = null;
exists = false;
return true;
}
override public function toString():String {
return id + Math.floor(x/16) + ":" + Math.floor(y/16) + "f" + facing + ";";
}
public static function buildFromString(raw:String):TileComponent {
var classID:String = raw.charAt(0);
var type:Class = Manufactoria.COMPONENT_TYPES[Manufactoria.COMPONENTS_BY_ID.indexOf(classID)];
var midIndex:int = raw.indexOf(':')
var facingIndex:int = raw.indexOf('f')
var x:int = Number(raw.slice(1, midIndex));
if (x < 5 + Manufactoria.inv_offset || x > 19 - Manufactoria.inv_offset)
return null;
var y:int = Number(raw.slice(midIndex + 1, facingIndex));
if (y < Manufactoria.inv_offset || y > 14 - Manufactoria.inv_offset)
return null;
var gridIndex:int = Manufactoria.gridToArray(Manufactoria.realToGrid(x*16,y*16));
if (gridIndex == Source.grid || gridIndex == Destination.grid)
return null;
var facing:int = Number(raw.charAt(facingIndex + 1));
return new type(x, y, gridIndex, facing);
}
public function unclip():TileComponent {
die();
return regenerate(this);
}
public function move(X:int, Y:int):void {
x = X;
y = Y;
}
public static const NAMES:Array = new Array("Conveyor", "Bridge", "",
"Blue Writer", "Red Writer", "B/R Branch",
"Green Writer", "Yellow Writer", "G/Y Branch");
public static const PLURAL_NAMES:Array = new Array("Conveyors", "Bridges", "",
"Blue Writers", "Red Writers", "B/R Branches",
"Green Writers", "Yellow Writers", "G/Y Branches");
public static const DESCRIPTIONS:Array = new Array( "Pushes the robot in the belt's direction!",
"Pushes the robot along its axis of movement!",
"",
"Writes a blue symbol onto the end of the tape!",
"Writes a red symbol onto the end of the tape!",
"Reads the next symbol and pushes the robot in that direction!", //(if it's red or blue)",
"Writes a green symbol onto the end of the tape!",
"Writes a yellow symbol onto the end of the tape!",
"Reads the next symbol and pushes the robot in that direction!");
}
}