-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.js
192 lines (172 loc) · 4.74 KB
/
components.js
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
function initCraftyComponents(){
console.log("Load crafty components");
Crafty.c("PlayerControls", {
init: function() {
this.requires("Keyboard");
return this.speed = 3;
},
playerControls: function(speed) {
if (speed) {
this.speed = speed;
}
this.bind("EnterFrame", function() {
if (this.disableControls) {
return;
}
var dx = 0;
var dy = 0;
if (this.isDown("D")) {
//this.x += this.speed;
dx = this.speed;
}
if (this.isDown("A")) {
//this.x -= this.speed;
dx = -1 * this.speed;
}
if (this.isDown("S")) {
//this.x += this.speed;
dy = this.speed;
}
if (this.isDown("W")) {
//this.x -= this.speed;
dy = -1 * this.speed;
}
if (dx !== 0 || dy !== 0) {
return this.body.ApplyImpulse(new b2Vec2(dx/PTM_RATIO, dy/PTM_RATIO), this.body.GetWorldCenter());
}
});
return this;
}
});//end_of_componet PLAYER CONTROLS
Crafty.c("Player", {
_spawnX: ACTIVE_WIDTH*0.5,
_spawnY: ACTIVE_HEIGHT*0.5,
init: function(){
this.addComponent("2D, Canvas, Color, Box2D, Keyboard, PlayerControls")
.playerControls(5)
//.origin("center")
.color("#fff")
.attr({
x: ACTIVE_WIDTH*0.5,
y: ACTIVE_HEIGHT*0.5,
h: 64,
w: 64
})
.box2d({
bodyType: 'dynamic',
density : 1.0,
friction : 2,
restitution : 0.2,
shape: 'circle',
//Filtering data
categoryBits: 0x0001,
maskBits: 0xfffd,
//groupIndex: 0,
})
.bind("EnterFrame",function() {
var maxSpeed = 10;
var velocity = this.body.GetLinearVelocity();
var speed = velocity.Length();
if (speed > maxSpeed) {
this.body.SetLinearDamping(5.0);
} else if (speed < maxSpeed) {
this.body.SetLinearDamping(0.5);
}
})
.onContact("Portal",
function(data){
destroyAllBodies();
//Get the name of the new room from the Portal object
var portal = data[0].obj;
console.log("Move to room:"+portal.nextRoom);
Crafty.scene(portal.nextRoom);
})
.bind('KeyDown', function(e) {
if (e.keyCode == Crafty.keys['1']) {
g_hud.itemHighlight.attr( { x: g_hud.startX + 64*0 } );
g_playerProps.selectedItem = 0;
evolveItem(g_playerProps.selectedItem);
}
else if (e.keyCode == Crafty.keys['2']) {
g_hud.itemHighlight.attr( { x: g_hud.startX + 64*1 } );
g_playerProps.selectedItem = 1;
evolveItem(g_playerProps.selectedItem);
}
else if (e.keyCode == Crafty.keys['3']) {
g_hud.itemHighlight.attr( { x: g_hud.startX + 64*2 } );
g_playerProps.selectedItem = 2;
evolveItem(g_playerProps.selectedItem);
}
else if (e.keyCode == Crafty.keys['4']) {
g_hud.itemHighlight.attr( { x: g_hud.startX + 64*3 } );
g_playerProps.selectedItem = 3;
evolveItem(g_playerProps.selectedItem);
}
else if (e.keyCode == Crafty.keys['5']) {
g_hud.itemHighlight.attr( { x: g_hud.startX + 64*4 } );
g_playerProps.selectedItem = 4;
evolveItem(g_playerProps.selectedItem);
}
else if(e.keyCode == Crafty.keys['SPACE']){
//Trigger "UseItem" event
//Crafty.trigger("UseItem",'wool');
Crafty.trigger("UseItem",g_playerProps.backpack[g_playerProps.selectedItem].itemType);
}
});
}});//end_of_component Player
Crafty.c("Chamber", {
init: function(){
this.addComponent("2D, Canvas, Color, Box2D")
.color("#ff0000")
.attr({
x: ACTIVE_WIDTH*0.25,
y: ACTIVE_HEIGHT*0.25,
h: 64,
w: 64
})
.box2d({
bodyType: 'static',
density : 1.0,
friction : 2,
restitution : 0.2,
shape: 'box',
//Filtering data
//categoryBits: 0x0001,
//maskBits: 0xfffd,
//groupIndex: 0,
})
}
});//end_of_component Chamber
Crafty.c("Portal",{
nextRoom: "undeterminedRoom",
targetRoom: function(room) {
this.nextRoom = room;
},
});//end_of_component Portal
Crafty.c("ShoutBox",{
printShoutBox: function(text)
{
this.text = text;
var textSize = 16,
initX = 250,
initY = 36;
var component;
for(var i=0;i<this.text.length;i++){
component = Crafty.e("2D, Canvas, SpriteText")
.attr({x: initX, y: initY+5+i*textSize, w: this.text[i].length*textSize, h: textSize})
.registerFont("BlueBubble", textSize, FONT_BLUE_BUBBLE)
.text(this.text[i])
.delay(function() {
//this.text("...");
}, 2000)
}
return component;
}
});
}
destroyAllBodies = function()
{
for(var b = world.GetBodyList(); b; b=b.GetNext()) {
world.DestroyBody(b);
}
}