-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatHunter.html
177 lines (159 loc) · 5.12 KB
/
RatHunter.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Rat Hunter</title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="mapBackground.js"></script>
<script type="text/javascript" src="modernizr.js"></script>
<script>
var mapTilesheet = new Image();
mapTilesheet.src = "imgs/tilesheet.png";
var firefoxTilesheet = new Image();
firefoxTilesheet.src = "imgs/firefox.png";
var controlCharacter;
var TileMap;
var FirefoxCharacter;
var firefoxFrameIndex = 0;
var allElements;
var GameSounds;
function canvasSupport() {
return Modernizr.canvas;
}
function mainDraw() {
requestAnimationFrame(mainDraw);
AllElements.forEach(function(element) {
element.draw()
});
}
var Keys = {
up: 38,
left: 37,
right: 39,
down: 40
};
var TileMap = {
draw: function() {
var indexOffset = -1;
var tileSize = 32;
var mapRows = 24;
var mapCols = 32;
var numberRowTiles = 30;
//the following arrays can be found under mapBackground.js
for (var r = 0; r < mapRows; r++) {
for (var c = 0; c < mapCols; c++) {
// draws the background grass (first layer)
var tileID = grassTilemap[r][c] + indexOffset;
var sourceX = Math.floor(tileID % numberRowTiles) * tileSize;
var sourceY = Math.floor(tileID / numberRowTiles) * tileSize;
ctx.drawImage(mapTilesheet, sourceX, sourceY, tileSize, tileSize, c * tileSize, r * tileSize, tileSize, tileSize);
// draws the objects on the background (second layer)
tileID = objectsTilemap[r][c] + indexOffset;
sourceX = Math.floor(tileID % numberRowTiles) * tileSize;
sourceY = Math.floor(tileID / numberRowTiles) * tileSize;
ctx.drawImage(mapTilesheet, sourceX, sourceY, tileSize, tileSize, c * tileSize, r * tileSize, tileSize, tileSize);
}
}
}
};
var FirefoxCharacter = {
x: 0,
y: 0,
speed: 0,
defaultSpeed: 10,
directions: {
right: 0,
left: 32,
up: 64,
down: 96
},
currentDirection: 0,
imageIndex: 0,
lastDrawTime: 0,
draw: function() {
var currentTime = new Date().getTime();
if (currentTime - this.lastDrawTime > 200 && this.speed > 0) {
this.imageIndex++;
this.lastDrawTime = new Date().getTime();
if (this.imageIndex > 4) {
this.imageIndex = 0;
}
}
ctx.drawImage(firefoxTilesheet, this.imageIndex * 32, this.currentDirection, 32, 32, this.x, this.y, 70, 70);
},
moveUp: function() {
this.y-=this.speed;
this.speed = this.defaultSpeed;
this.currentDirection = this.directions.up;
},
moveLeft: function() {
this.x-=this.speed;
this.speed = this.defaultSpeed;
this.currentDirection = this.directions.left;
},
moveRight: function() {
this.x+=this.speed;
this.speed = this.defaultSpeed;
this.currentDirection = this.directions.right;
},
moveDown: function() {
this.y+=this.speed;
this.speed = this.defaultSpeed;
this.currentDirection = this.directions.down;
},
stop: function() {
this.speed = 0;
this.imageIndex = 4;
}
};
var GameSounds = {
backgroundMusic: new Audio("sounds/backgroundMusic.ogg")
};
AllElements = [
TileMap,
FirefoxCharacter
];
function keyDown(aEvent) {
switch(aEvent.keyCode) {
case Keys.up:
controlCharacter.moveUp();
break;
case Keys.left:
controlCharacter.moveLeft();
break;
case Keys.right:
controlCharacter.moveRight();
break;
case Keys.down:
controlCharacter.moveDown();
break;
}
}
function keyUp(aEvent) {
controlCharacter.stop();
}
function init() {
if (!canvasSupport()) {
document.write("Your Browser doesn't support Canvas, please download the latest Mozilla Firefox Browser");
} else {
var canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
controlCharacter = FirefoxCharacter; // current controllable character
GameSounds.backgroundMusic.loop = true;
GameSounds.backgroundMusic.play();
window.onkeydown = keyDown;
window.onkeyup = keyUp;
requestAnimationFrame(mainDraw);
}
};
</script>
</head>
<body onload="init();">
<div id="title" align="center">
Rat Hunter
</div>
<div id="centerCanvas">
<canvas id="canvas" width="1024" height="768"></canvas>
</div>
</body>
</html>