-
Notifications
You must be signed in to change notification settings - Fork 3
/
tool.ts
292 lines (261 loc) · 10.6 KB
/
tool.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/// <reverence path="player.ts"/>
/// <reference path="rigidSprite.ts"/>
/// <reference path="interfaces.ts"/>
/// <reference path="toolYarnBall"/>
/// <reference path="tileset.ts"/>
/// <reference path="rectangle.ts"/>
/// <reference path="InpDevWrapper.ts"/>
/*
* Class: Tool
* A tool is an interactable object that allows for the building of another object in the scene.
*
* There are two primary ways of creating a tool. The first is to specify a slew of variables and let
* the tool create the object it will build automatically. This object will appear right on top of
* the tool. The other option is to set its buildable property manually, thereby tying it to another
* object in the scene after creation.
*/
class Tool extends RigidSprite implements Interactable
{
public static debugColorTool:number[] = [1.0, 0.0, 0.0, 1.0];
public static debugColorBuildable:number[] = [0.0, 1.0, 0.0, 1.0];
game:GameObject;
buildables:Buildable[];
toolYarnBall:ToolYarnBall;
texture:Texture;
playerOverlapping:boolean = false;
highlightSprite:Draw2DSprite;
constructor (options:ToolOptions, game:GameObject)
{
super(options);
this.game = game;
this.buildables = [];
if (options.hasOwnProperty("buildable") && options.buildable != null)
{
this.buildables.push(options.buildable);
}
if (options.hasOwnProperty("toolYarnBall") && options.toolYarnBall != null)
{
this.toolYarnBall = options.toolYarnBall;
}
else
{
this.toolYarnBall = null;
}
// make sure the mask is set so that this does not interact with anything
this.body.shapes[0].setMask(0);
this.body.setPosition(options.initialPos);
// make the sprite to show when the player is overlapping with the tool
// build the player sprite
var spriteParams:any = {
x: options.initialPos[0],
y: options.initialPos[1],
width: 128,
height: 128,
color: [1.0, 1.0, 1.0, 1.0]
};
this.highlightSprite = Draw2DSprite.create(spriteParams);
game.graphicsDevice.createTexture({
src: "assets/glow.png",
mipmaps: true,
onload: (texture:Texture) =>
{
if (texture != null)
{
this.highlightSprite.setTexture(texture);
this.highlightSprite.setTextureRectangle([0, 0, 128, 128]);
console.log("set texture for highlight sprite");
}
}
});
}
static constructFromTiled(obj:any, tileset:Tileset, game:GameObject) {
// fix the rotation so that 0 is up and we're using degrees
var rotation:number = (parseFloat(obj.properties.rotation) * (3.141592 / 180)) + 3.141592;
console.log("Building tool from tiled");
var material:Physics2DMaterial = game.physicsDevice.createMaterial({
elasticity : 0,
staticFriction : 0,
dynamicFriction : 0
});
var shape:Physics2DShape = game.physicsDevice.createPolygonShape({
vertices : game.physicsDevice.createBoxVertices(obj.width, obj.height),
material : material,
group : 2,
mask : 0
});
var body:Physics2DRigidBody = game.physicsDevice.createRigidBody({
type : 'kinematic',
shapes : [shape],
position : [obj.x + obj.width/2, obj.y + obj.height/2]
});
var sprite:Draw2DSprite = Draw2DSprite.create({
width: obj.width,
height: obj.height,
color: Tool.debugColorTool
});
game.physicsWorld.addRigidBody(body);
var options:ToolOptions = {
sprite : sprite,
initialPos : [obj.x + obj.width/2, obj.y + obj.height/2],
gid: obj.gid,
body : body,
buildable : null,
width: obj.width,
height: obj.height
};
if (!(obj.properties.prebuilt == "true"))
{
var rectWidth = parseFloat(obj.properties.width) * 64;
var initHeight:number = (parseFloat(obj.properties.initHeight) ? parseFloat(obj.properties.initHeight) * 64 : 0);
var maxHeight:number = parseFloat(obj.properties.maxHeight) * 64;
var minHeight:number = parseFloat(obj.properties.minHeight) * 64;
var initialPos:number[] = [obj.x + obj.width/2, obj.y + obj.height];
// limit the size to the shapes we can handle
//maxHeight = Math.floor(maxHeight / 32) * 32;
//minHeight = Math.floor(minHeight / 32) * 32;
//initHeight = Math.floor(initHeight / 32) * 32;
//rectWidth = Math.ceil(rectWidth / 64) * 64;
// build the rectangle here if not prebuilt
var material:Physics2DMaterial = game.physicsDevice.createMaterial({
elasticity : 0,
staticFriction : 0.3,
dynamicFriction : 0.2
});
var vertices:number[][] = game.physicsDevice.createRectangleVertices(-rectWidth/2, 0, rectWidth/2, initHeight);
var shape:Physics2DShape = game.physicsDevice.createPolygonShape({
vertices: vertices,
material: material,
group: ShapeGroups.OVERLAPPABLES,
mask: ObjectMasks.SOLID
});
var body:Physics2DRigidBody = game.physicsDevice.createRigidBody({
type: "kinematic",
shapes: [shape],
mass: 10
});
var sprite:Draw2DSprite = Draw2DSprite.create({
width: rectWidth,
height: (initHeight ? initHeight : 1), // XXX: hack to make sure we don't get errors from 0 width objects
origin : [rectWidth/2, 0],
color: Tool.debugColorBuildable
});
// add the body to the world
game.physicsWorld.addRigidBody(body);
body.setPosition(initialPos);
body.setRotation(rotation);
console.log("IsClimbable: " + obj.properties.isClimbable);
var rectOptions:RectangleOptions = {
sprite : sprite,
initialPos : initialPos,
body : body,
initSize : initHeight,
maxSize : maxHeight,
minSize : minHeight,
width : rectWidth,
height : initHeight,
rotation: rotation,
isBuildable : (obj.properties.isBuildable == "true"),
isClimbable : (obj.properties.isClimbable == "true"),
isSolid : (obj.properties.isSolid == "true"),
isPullable : (obj.properties.isPullable == "true")
};
if (obj.gid)
{
rectOptions.gid = obj.gid;
}
if (obj.properties.bodyType)
{
rectOptions.bodyType = obj.properties.bodyType;
}
var newRectangle:Rectangle = new Rectangle(rectOptions, game);
tileset.rigidSprites.push(newRectangle);
game.collisionHelp.pushInteractable(newRectangle);
options.buildable = newRectangle;
}
var newTool:Tool = new Tool(options, game);
game.collisionHelp.pushInteractable(newTool);
return newTool;
}
/*
* Method: getShape
* Returns the shape that the player must be overlapping with in order to build this item. ie the knitting needles.
*/
getShapes():Physics2DShape[]
{
return this.body.shapes;
}
playerCollideCallback(player:Player):void
{
// check to see if the player is overlapping the right object
if (this.game.collisionHelp.collisionUtils.intersects(this.body.shapes[0], player.rigidSprite.body.shapes[0])
&& this.buildables.length > 0 && !this.isDead)
{
this.playerOverlapping = true;
// handle key presses
for (var i:number = 0; i < this.buildables.length; i++)
{
var buildable:Buildable = this.buildables[i];
if (this.game.keyboard.keyPressed("W"))
{
if (buildable.ratioYarnUsed() == 1){
this.game.sfx.setCurrentFX(this.game.sfx.noKnitSFX, true);
} else {
this.game.sfx.setCurrentFX(this.game.sfx.knitUpSFX);
}
buildable.buildUp();
}
else if (this.game.keyboard.keyPressed("S"))
{
if (buildable.ratioYarnUsed() == 0){
this.game.sfx.setCurrentFX(this.game.sfx.noKnitSFX, true);
} else {
this.game.sfx.setCurrentFX(this.game.sfx.knitDownSFX);
}
buildable.buildDown();
}
else if (!this.game.sfxSource.paused &&
((this.game.sfx.currentSFX == this.game.sfx.knitDownSFX) ||
(this.game.sfx.currentSFX == this.game.sfx.knitUpSFX)))
{
// no longer actively building, so pause the knitting sound!
this.game.sfxSource.pause();
}
}
}
}
setToolYarnBall(toolYarnBall:ToolYarnBall)
{
this.toolYarnBall = toolYarnBall;
this.toolYarnBall.setBuildable(this.buildables[0]); // XXX: Arbitrarily sets the first buildable
}
draw(draw2D:Draw2D, offset:number[]) {
if (this.game.debugMode)
{
this.sprite.setColor(Chain.debugColorChain);
}
else
{
this.sprite.setColor([0,0,0,0]);
}
super.draw(draw2D, offset);
if (this.playerOverlapping)
{
if (this.body != null)
{
var pos:number[] = this.body.getPosition();
this.highlightSprite.x = pos[0];
this.highlightSprite.y = pos[1];
//this.highlightSprite.rotation = this.body.getRotation();
}
else
{
this.highlightSprite.x = this.initialPos[0] - 64;
this.highlightSprite.y = this.initialPos[1] + 64;
}
this.highlightSprite.x -= offset[0];
this.highlightSprite.y -= offset[1];
draw2D.drawSprite(this.highlightSprite);
this.playerOverlapping = false;
}
}
}