-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
140 lines (106 loc) · 3.48 KB
/
index.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
<link rel="stylesheet" href="snake.css">
<body>
<div class="message">
<div class="message" id="bigMessage">Snake</div>
<div class="message fadeIn" id="littleMessage">Click to begin</div>
</div>
</body>
<!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script> -->
<script type="module">
import { Settings, Snake, Apple } from './SnakeGame.js';
const State = { Menu: 0, Playing: 1, Lose: 2 };
let snakes = [], player, apples = [], gameState = State.Menu;
let snakeTimer, appleTimer;
const bigMessage = document.getElementById( 'bigMessage' );
const littleMessage = document.getElementById( 'littleMessage' );
function newGame() {
bigMessage.innerText = '';
littleMessage.innerText = '';
littleMessage.classList.remove( 'fadeIn' );
snakes.forEach( snake => snake.remove() );
apples.forEach( apple => apple.remove() );
clearTimeout( snakeTimer );
clearTimeout( appleTimer );
player = new Snake();
snakes = [ player ];
apples = [];
snakeTimer = setTimeout( spawnSnake, 2000 );
for ( let i = 0; i < 3; i ++ ) {
apples.push( new Apple() );
}
appleTimer = setTimeout( spawnApple, 3000 );
gameState = State.Playing;
}
function gameOver() {
bigMessage.innerText = 'Game Over!';
littleMessage.innerText = 'Click to restart'
littleMessage.classList.add( 'fadeIn' );
gameState = State.Lose;
}
function spawnSnake() {
snakes.push( new Snake() );
snakeTimer = setTimeout( spawnSnake, 2000 * snakes.length + Math.random() * 5000 );
}
function spawnApple() {
apples.push( new Apple() );
appleTimer = setTimeout( spawnApple, Math.random() * 3000 );
}
function update( dt ) {
snakes = snakes.filter( snake => {
apples = apples.filter( apple => !snake.tryEatApple( apple ) );
const avoidVectors = snake.getAvoidVectors( snakes );
if ( snake != player ) {
snake.think( apples, avoidVectors );
}
if ( avoidVectors.every( vector => vector.dist > 0 ) ) {
snake.update( dt );
return true;
}
else {
snake.kill();
if ( snake == player ) {
gameOver();
}
return false;
}
} );
}
// const gui = new dat.GUI();
// gui.width = 200;
// for ( const setting in Settings ) {
// gui.add( Settings, setting );
// }
//
// Input and update loop
//
let mouseDown = false;
document.onmousemove = onInput;
document.ontouchmove = onInput;
document.onmousedown = ( e ) => { mouseDown = true; onInput( e ); }
document.ontouchstart = ( e ) => { mouseDown = true; onInput( e ); }
document.onmouseup = ( e ) => mouseDown = false;
document.ontouchend = ( e ) => mouseDown = false;
function onInput( e ) {
const event = e.touches ? e.touches[0] : e;
if ( gameState == State.Playing ) {
player.goalAngle = Math.atan2( event.pageY - player.y, event.pageX - player.x );
}
else if ( mouseDown ) {
newGame();
}
}
let keyDown = true; // default to running
document.onkeydown = () => keyDown = true;
document.onkeyup = () => keyDown = false;
let lastTime = null;
function animate( now ) {
lastTime ??= now; // for first call only
// Only update when key is pressed (for debugging purposes)
if ( keyDown ) {
update( now - lastTime );
}
lastTime = now;
requestAnimationFrame( animate );
}
requestAnimationFrame( animate );
</script>