-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
178 lines (143 loc) · 4.23 KB
/
game.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
var createGame = require('voxel-engine'),
player = require('voxel-player'),
texturePath = require('painterly-textures')(__dirname),
trigger = require('spatial-trigger'),
aabb = require('aabb-3d'),
walk = require('voxel-walk'),
hud = require('./lib/hud.js'),
generateWorld = require('./lib/generate-world.js'),
code = {};
hud.walkCircuit.onchange = function() {
console.log('updating moveForward');
// FIXME could be this.value
var moveForward, funcString, functionBody = hud.walkCircuit.value;
try {
funcString = "moveForward = function (robot, speed, dt) {" + functionBody + "}";
eval(funcString);
code.moveForward = moveForward;
hud.walkError.style.display = 'none';
hud.walkError.innerText = '';
} catch(e) {
code.handleWalkError(e);
}
};
// TODO de-duplicate handling of walk and jump here
hud.jumpCircuit.onchange = function() {
console.log('updating jump');
var jump, funcString, functionBody = hud.jumpCircuit.value;
try {
funcString = "jump = function (robot, dt) {" + functionBody + "}";
eval(funcString);
code.jump = jump;
hud.jumpError.style.display = 'none';
hud.jumpError.innerText = '';
} catch(e) {
code.handleJumpError(e);
}
};
code.moveForwardError = function() {
hud.showError("Error! Walking circuit damaged!");
};
code.moveForward = code.moveForwardError;
code.moveForwardWrapper = function(target, speed, dt) {
var robot = {
speed: {
forward: target.velocity.z * -100
}
};
try {
code.moveForward(robot, speed, dt);
target.velocity.z = robot.speed.forward / -100;
hud.walkError.style.display = 'none';
hud.walkError.innerText = '';
} catch(e) {
code.handleWalkError(e);
}
};
code.handleWalkError = function (e) {
hud.walkError.innerText = e.message;
hud.walkError.style.display = 'block';
};
code.inert = function () {
console.log('not implemented');
};
code.jumpError = function () {
hud.showError("Error! Jumping circuit broken!");
setTimeout(hud.showJumpTutorial, 2000);
};
code.jump = code.inert;
code.jumpWrapper = function(target, dt) {
// TODO consider providing more than just y
// could also make a function to convert a target to a robot
// and another for the reverse
var robot = {
speed: {
up: target.velocity.y * 100
}
};
try {
code.jump(robot, dt);
target.velocity.y = robot.speed.up / 100;
hud.jumpError.style.display = 'none';
hud.jumpError.innerText = '';
} catch(e) {
code.handleJumpError(e);
}
};
code.handleJumpError = function(e) {
hud.jumpError.innerText = e.message;
hud.jumpError.style.display = 'block';
};
var game = createGame({
texturePath: texturePath,
generate: generateWorld,
controls: {
moveForward: code.moveForwardWrapper,
jump: code.jumpWrapper
}
});
game.appendTo(document.body);
// TODO move player creation and walking to separate script - only needs game
// and can require walk from there.
var createPlayer = player(game);
var avatar = createPlayer('player.png');
avatar.pov(3);
avatar.possess();
avatar.yaw.position.set(0, 2, 10);
game.on('tick', function() {
walk.render(avatar.playerSkin);
var vx = Math.abs(avatar.velocity.x),
vz = Math.abs(avatar.velocity.z);
if (vx > 0.001 || vz > 0.001) { walk.stopWalking(); }
else { walk.startWalking(); }
});
var triggers = {
atJumpObstacle: aabb([-5, 1, 6], [11, 1, 3]),
atStrafeObstacle: aabb([-5, 3, 0], [11, 1, 6])
};
triggers = triggers;
trigger(game.spatial, triggers.atJumpObstacle).on('enter', function() {
hud.walkTutorial.classList.add('complete');
hud.showInstruction('Press [spacebar] to jump');
if (code.jump === code.inert) {
code.jump = code.jumpError;
}
});
trigger(game.spatial, triggers.atStrafeObstacle).on('enter', function () {
hud.jumpTutorial.classList.add('complete');
// TODO next challenge
});
(function drawTriggers () {
for (var triggerName in triggers) {
game.addAABBMarker(triggers[triggerName]);
}
});
var critterCreator = require('voxel-critter')(game);
var img = new Image();
img.onload = function() {
var wills = critterCreator(img);
wills.position.clone(game.controls.target().avatar.position);
wills.position.z -= 4;
wills.position.y += 10;
};
img.src = 'wills.png';