-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
134 lines (115 loc) · 4.14 KB
/
index.html
File metadata and controls
134 lines (115 loc) · 4.14 KB
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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PokèmonNode</title>
<script src="/js/jquery-1.8.2.js"></script>
<script src="/js/canvace-0.2.4.min.js"></script>
<style>
body {text-align: center;}
canvas {border: 1px solid black;}
</style>
</head>
<body>
<canvas width="900" height="400"></canvas>
<script>
var mainCharacterStates, character, keyboard, characterSM;
var modes = ['still', 'walking', 'running'];
var directions = ['left', 'up', 'right', 'down'];
var moveKeys = {};
var stepTimer = null;
var userTick = function() {
moveKeys = {
left: keyboard.isKeyDown(KeyEvent.DOM_VK_A),
up: keyboard.isKeyDown(KeyEvent.DOM_VK_W),
right: keyboard.isKeyDown(KeyEvent.DOM_VK_D),
down: keyboard.isKeyDown(KeyEvent.DOM_VK_S)
};
var mode = (keyboard.isKeyDown(KeyEvent.DOM_VK_SPACE)) ? 'run' : 'walk';
if (stepTimer == null && moveKeys.left) characterSM[mode]('left');
if (stepTimer == null && moveKeys.up) characterSM[mode]('up');
if (stepTimer == null && moveKeys.right) characterSM[mode]('right');
if (stepTimer == null && moveKeys.down) characterSM[mode]('down');
};
$.getJSON('Pokemon.json', {}, function (response) {
var loader = new Canvace.Loader('media');
loader.loadImages(response, function () {
var canvas = $('canvas')[0];
var stage = new Canvace.Stage(response, canvas);
keyboard = new Canvace.Keyboard(window, true);
var loop = new Canvace.RenderLoop(stage, null, loader, userTick);
loop.run();
mainCharacterStates = (function() {
var states = {};
$.each(modes, function(i, mode) {
states[mode] = {};
$.each(directions, function(j, direction) {
states[mode][direction] = stage.getEntity({
'mode': mode,
'direction': direction
});
});
});
return states;
})();
character = stage.getInstance({});
$(window)
.on('focusout', function() {loop.suspend();})
.on('focusin', function() {loop.run();});
});
});
characterSM = new Canvace.StateMachine((function() {
var states = {},
walkingVelocities = {
left: -2.8571428571428571428571428571429,
up: -2.8571428571428571428571428571429,
right: 2.8571428571428571428571428571429,
down: 2.8571428571428571428571428571429
},
runningVelocities = {
left: -6.6666666666666666666666666666667,
up: -6.6666666666666666666666666666667,
right: 6.6666666666666666666666666666667,
down: 6.6666666666666666666666666666667
},
directionAxis = {
left: 'i',
up: 'j',
right: 'i',
down: 'j'
};
var resetCharacter = function(direction, mode) {
var axis = directionAxis[direction],
mode = (keyboard.isKeyDown(KeyEvent.DOM_VK_SPACE)) ? 'running' : 'still';
character = character.replaceWith(mainCharacterStates[mode][direction]);
character.getUniformVelocity()[axis] = 0;
character.getPosition()[axis] = Math.round(character.getPosition()[axis]);
stepTimer = null;
};
$.each(directions, function(index, stateDirection) {
states[stateDirection] = {
walk: function(direction) {
if (direction == stateDirection) {
character = character.replaceWith(mainCharacterStates.walking[direction]);
character.getUniformVelocity()[directionAxis[direction]] = walkingVelocities[direction];
stepTimer = setTimeout(resetCharacter, 350, direction);
return stateDirection;
} else {
character = character.replaceWith(mainCharacterStates.walking[direction]);
stepTimer = setTimeout(resetCharacter, 70, direction);
return direction;
}
},
run: function(direction) {
character = character.replaceWith(mainCharacterStates.running[direction]);
character.getUniformVelocity()[directionAxis[direction]] = runningVelocities[direction];
stepTimer = setTimeout(resetCharacter, 150, direction);
return direction;
}
};
});
return states;
})(), 'down');
</script>
</body>
</html>