-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.html
133 lines (115 loc) · 4.82 KB
/
game.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
<html>
<head>
<meta charset="utf-8"/>
<!-- Third-party-->
<script src="js/pixi.min.js"></script>
<script src="js/pixi-particles.min.js"></script>
<script src="js/pathfinding-browser.min.js"></script>
<!-- The game -->
<script src="js/game.js"></script>
<script>
// FOR DEVELOPMENT
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
function wipeQueryString(){
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.location.href = clean_uri;
}
}
// on DOM load
document.addEventListener("DOMContentLoaded", function(event) {
document.getElementById('btnRandom').addEventListener('click', wipeQueryString, false);
// Load resources
PIXI.loader
.add('terrainAtlas', './art/rogueliketiles.json')
.add('characterAtlas', './art/characters_1.json')
.add('wallsAtlas', './art/walls.json')
.add('carpetAtlas', './art/dungeoncarpet.json')
.add('itemAtlas', './art/roguelikeitems.json')
.add('circle_bg', './art/circle_bg.png')
.add('particle_standard', './art/particle.png')
.load(setupGame);
// On resources load
function setupGame() {
var keyRepeatRate = 50; // In milliseconds
// Setup game
var time = (new Date).getTime(); // or specify a seed to be used by the entire game
var seed;
// FOR DEVELOPMENT
var seedInQS = getUrlParameter('seed');
if (seedInQS !== '') {
seed = seedInQS;
}
else {
history.pushState({}, '', window.location.href + '?seed=' + time);
seed = time.toString();
}
var dungeonNumberInQS = getUrlParameter('dungeon');
var dungeonNumber = 1;
if(dungeonNumberInQS != '') dungeonNumber = dungeonNumberInQS;
// Our area to render to
var canvas = document.getElementById('canvas');
var renderer = new PixiRenderer(
canvas,
40, // view width
30 // view height
);
// Prep seed if it's not numeric
if (seed.match(/[a-z]/i)) {
var string = seed;
seed = '';
for (var i = 0; i < string.length; i++) {
seed += string.charCodeAt(i);
}
}
// Fetch the game settings
var gameSettings = GameSettingsProvider.getSettings();
// Start the game
var game = new Game(renderer, parseInt(seed), gameSettings, dungeonNumber);
game.generateNextDungeon();
game.start();
// Bind keys
document.onkeydown = keyDown;
document.onkeyup = keyUp;
var isKeyRepeating = false;
var keyRepeatTimer = null;
function keyUp(e) {
if (keyRepeatTimer !== null) {
clearTimeout(keyRepeatTimer);
keyRepeatTimer = null;
}
isKeyRepeating = false;
}
function keyDown(e) {
e.preventDefault();
if (isKeyRepeating) {
if (keyRepeatTimer == null) {
keyRepeatTimer = setTimeout(function() {
isKeyRepeating = false;
clearTimeout(keyRepeatTimer);
keyRepeatTimer = null;
}, keyRepeatRate);
}
}
else {
isKeyRepeating = true;
e = e || window.event;
game.controlPressed(e.keyCode);
}
}
}
});
</script>
</head>
<body style="background-color:black;">
<div id="canvas" style="height:100%;width:100%">
</div>
<button id="btnRandom" style="position:absolute; top:0; right:0;">Random Dungeon</button>
</body>
</html>