-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathknitCube.ts
152 lines (131 loc) · 4.48 KB
/
knitCube.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/// <reference path="jslib-modular/tzdraw2d.d.ts" />
/// <reference path="jslib-modular/physics2d.d.ts" />
/// <reference path="rigidSprite.ts"/>
/// <reference path="interfaces.ts"/>
/// <reference path="tileset.ts"/>
/// <reference path="player.ts"/>
class KnitCube extends RigidSprite implements Buildable
{
public static debugColorConstruct:number[] = [.1, .5, .1, 1.0];
public static debugColorCube:number[] = [1.0, 1.0, 1.0, 1.0];
game:GameObject = null;
GROW_SPEED = 2;
maxDimension:number;
minDimension:number;
currentDimension:number;
construct:RigidSprite;
isBuildable:boolean = true;
constructor (options:knitCubeOptions, game:GameObject)
{
super(options);
this.game = game;
this.maxDimension = options.maxDimension;
this.minDimension = options.minDimension;
this.currentDimension = 0;
// for the cube that will be knitted
var vertices:number[][] = [[0,0], [10,0], [10, 10], [0, 10]];
var shape:Physics2DShape = game.physicsDevice.createPolygonShape({
vertices: vertices,
group: ShapeGroups.TOOLS,
mask: ObjectMasks.SOLID
});
var body:Physics2DRigidBody = game.physicsDevice.createRigidBody({
type: "kinematic",
shapes: [shape],
mass: 10
});
var sprite:Draw2DSprite = Draw2DSprite.create({
width: this.maxDimension,
height: 1,
origin : [this.maxDimension / 2, this.maxDimension / 2],
color: KnitCube.debugColorConstruct
});
this.construct = new RigidSprite({
sprite:sprite,
initialPos:options.initialPos,
body:body
});
}
static constructFromTiled(obj:any, tileset:Tileset, game:GameObject):KnitCube {
var vertices:number[][] = [[0,0], [obj.width,0], [obj.width, obj.height], [0, obj.height]];
var shapes : Physics2DShape[] = [
game.physicsDevice.createPolygonShape({
vertices : vertices,
group: ShapeGroups.TOOLS,
mask: ObjectMasks.SOLID
})
];
var body = game.physicsDevice.createRigidBody({
type : 'static',
shapes : shapes,
position : [obj.x, obj.y]
});
var sprite:Draw2DSprite = Draw2DSprite.create({
width: obj.width,
height: obj.height,
x : obj.x,
y : obj.y,
origin : [0, 0],
color: KnitCube.debugColorCube
});
game.physicsWorld.addRigidBody(body);
var params:knitCubeOptions = {
sprite : sprite,
initialPos : [sprite.x, sprite.y],
body : body,
maxDimension : obj.properties.maxDimension,
minDimension : obj.properties.minDimension
};
var kc:KnitCube = new KnitCube(params, game);
game.collisionHelp.pushInteractable(kc);
return kc;
}
ratioYarnUsed():number
{
return this.currentDimension/this.maxDimension;
}
public buildUp():void
{
if (this.currentDimension + this.GROW_SPEED < this.maxDimension) {
this.currentDimension += this.GROW_SPEED;
this.remakeConstruct();
}
}
public buildDown():void
{
if (this.currentDimension - this.GROW_SPEED > this.minDimension) {
this.currentDimension -= this.GROW_SPEED;
this.remakeConstruct();
}
}
getBuildableShape():Physics2DShape
{
return this.body.shapes[0];
}
private remakeConstruct():void
{
if (this.currentDimension > 0)
{
this.construct.sprite.setHeight(this.currentDimension);
this.construct.sprite.setWidth(this.currentDimension);
}
}
playerCollideCallback(player:Player):void {
console.log("knit cube intersecting with player");
}
getShapes():Physics2DShape[] {
return [this.body.shapes[0]];
}
draw(draw2D:Draw2D, offset:number[]) {
// should only do this once when toggling. Not on every draw :(
if (this.game.debugMode){
this.sprite.setColor(KnitCube.debugColorCube);
this.construct.sprite.setColor(KnitCube.debugColorConstruct);
} else {
this.sprite.setColor([0,0,0,0]);
this.construct.sprite.setColor([0,0,0,0]);
}
this.construct.draw(draw2D, offset);
super.draw(draw2D, offset);
}
}