1212 *
1313 * ### WASD input example
1414 * ```
15- import { create_rectangle, query_position, update_position, update_loop, build_game, input_key_down } from "arcade_2d";
16-
17- // Create GameObjects outside update_loop(...)
18- const player = update_position(create_rectangle(100, 100), [300, 300]);
19- const movement_dist = 10;
20-
21- function add_vectors(to, from) {
22- to[0] = to[0] + from[0];
23- to[1] = to[1] + from[1];
24- }
25-
26- update_loop(game_state => {
27- const new_position = query_position(player);
28-
29- if (input_key_down("w")) {
15+ * import { create_rectangle, query_position, update_position, update_loop, build_game, input_key_down } from "arcade_2d";
16+ *
17+ * // Create GameObjects outside update_loop(...)
18+ * const player = update_position(create_rectangle(100, 100), [300, 300]);
19+ * const movement_dist = 10;
20+ *
21+ * function add_vectors(to, from) {
22+ * to[0] = to[0] + from[0];
23+ * to[1] = to[1] + from[1];
24+ * }
25+ *
26+ * update_loop(game_state => {
27+ * const new_position = query_position(player);
28+ *
29+ * if (input_key_down("w")) {
3030 add_vectors(new_position, [0, -1 * movement_dist]);
3131 }
3232 if (input_key_down("a")) {
@@ -38,7 +38,7 @@ update_loop(game_state => {
3838 if (input_key_down("d")) {
3939 add_vectors(new_position, [movement_dist, 0]);
4040 }
41-
41+
4242 // Update GameObjects within update_loop(...)
4343 update_position(player, new_position);
4444});
@@ -48,20 +48,20 @@ build_game();
4848 * ### Draggable objects example
4949 * ```
5050import { create_sprite, update_position, update_scale, pointer_over_gameobject, input_left_mouse_down, update_to_top, query_pointer_position, update_loop, build_game } from "arcade_2d";
51-
51+
5252// Using assets
5353const gameobjects = [
5454 update_position(create_sprite("objects/cmr/splendall.png"), [200, 400]),
5555 update_position(update_scale(create_sprite("avatars/beat/beat.happy.png"), [0.3, 0.3]), [300, 200]),
5656 update_position(update_scale(create_sprite("avatars/chieftain/chieftain.happy.png"), [0.2, 0.2]), [400, 300])];
57-
57+
5858// Simple dragging function
5959function drag_gameobject(gameobject) {
6060 if (input_left_mouse_down() && pointer_over_gameobject(gameobject)) {
6161 update_to_top(update_position(gameobject, query_pointer_position()));
6262 }
6363}
64-
64+
6565update_loop(game_state => {
6666 for (let i = 0; i < 3; i = i + 1) {
6767 drag_gameobject(gameobjects[i]);
@@ -73,7 +73,7 @@ build_game();
7373 * ### Playing audio example
7474 * ```
7575import { input_key_down, create_audio, play_audio, update_loop, build_game } from "arcade_2d";
76-
76+
7777const audio = create_audio("https://labs.phaser.io/assets/audio/SoundEffects/key.wav", 1);
7878update_loop(game_state => {
7979 // Press space to play audio
@@ -87,15 +87,15 @@ build_game();
8787 *
8888 * ```
8989import { create_rectangle, update_position, update_color, get_loop_count, set_scale, update_loop, build_game } from "arcade_2d";
90-
90+
9191const gameobjects = [];
9292for (let i = 0; i < 100; i = i + 1) {
9393 gameobjects[i] = [];
9494 for (let j = 0; j < 100; j = j + 1) {
9595 gameobjects[i][j] = update_position(create_rectangle(1, 1), [i, j]);
9696 }
9797}
98-
98+
9999update_loop(game_state => {
100100 const k = get_loop_count();
101101 for (let i = 0; i < 100; i = i + 1) {
@@ -112,31 +112,31 @@ build_game();
112112 * ```
113113import { create_rectangle, create_sprite, create_text, query_position, update_color, update_position, update_scale, update_text, update_to_top, set_fps, get_loop_count, enable_debug, debug_log, input_key_down, gameobjects_overlap, update_loop, build_game, create_audio, loop_audio, stop_audio, play_audio } from "arcade_2d";
114114// enable_debug(); // Uncomment this to see debug info
115-
115+
116116// Constants
117117let snake_length = 4;
118118const food_growth = 4;
119119set_fps(10);
120-
120+
121121const snake = [];
122122const size = 600;
123123const unit = 30;
124124const grid = size / unit;
125125const start_length = snake_length;
126-
126+
127127// Create Sprite Gameobjects
128128update_scale(create_sprite("https://labs.phaser.io/assets/games/germs/background.png"), [4, 4]); // Background
129129const food = create_sprite("https://labs.phaser.io/assets/sprites/tomato.png");
130130let eaten = true;
131-
131+
132132for (let i = 0; i < 1000; i = i + 1) {
133133 snake[i] = update_color(update_position(create_rectangle(unit, unit), [-unit / 2, -unit / 2]),
134134 [127 + 128 * math_sin(i / 20), 127 + 128 * math_sin(i / 50), 127 + 128 * math_sin(i / 30), 255]); // Store offscreen
135135}
136136const snake_head = update_color(update_position(create_rectangle(unit * 0.9, unit * 0.9), [-unit / 2, -unit / 2]), [0, 0, 0 ,0]); // Head
137-
137+
138138let move_dir = [unit, 0];
139-
139+
140140// Other functions
141141const add_vec = (v1, v2) => [v1[0] + v2[0], v1[1] + v2[1]];
142142const bound_vec = v => [(v[0] + size) % size, (v[1] + size) % size];
@@ -156,49 +156,49 @@ function input() {
156156 }
157157}
158158let alive = true;
159-
159+
160160// Create Text Gameobjects
161161const score = update_position(create_text("Score: "), [size - 60, 20]);
162162const game_text = update_color(update_scale(update_position(create_text(""), [size / 2, size / 2]), [2, 2]), [0, 0, 0, 255]);
163-
163+
164164// Audio
165165const eat = create_audio("https://labs.phaser.io/assets/audio/SoundEffects/key.wav", 1);
166166const lose = create_audio("https://labs.phaser.io/assets/audio/stacker/gamelost.m4a", 1);
167167const move = create_audio("https://labs.phaser.io/assets/audio/SoundEffects/alien_death1.wav", 1);
168168const bg_audio = play_audio(loop_audio(create_audio("https://labs.phaser.io/assets/audio/tech/bass.mp3", 0.5)));
169-
169+
170170// Create Update loop
171171update_loop(game_state => {
172172 update_text(score, "Score: " + stringify(snake_length - start_length));
173173 if (!alive) {
174174 update_text(game_text, "Game Over!");
175175 return undefined;
176176 }
177-
177+
178178 // Move snake
179179 for (let i = snake_length - 1; i > 0; i = i - 1) {
180180 update_position(snake[i], query_position(snake[i - 1]));
181181 }
182182 update_position(snake[0], query_position(snake_head)); // Update head
183183 update_position(snake_head, bound_vec(add_vec(query_position(snake_head), move_dir))); // Update head
184184 debug_log(query_position(snake[0])); // Head
185-
185+
186186 input();
187-
187+
188188 // Add food
189189 if (eaten) {
190190 update_position(food, [math_floor(math_random() * grid) * unit + unit / 2, math_floor(math_random() * grid) * unit + unit / 2]);
191191 eaten = false;
192192 }
193-
193+
194194 // Eat food
195195 if (get_loop_count() > 1 && gameobjects_overlap(snake_head, food)) {
196196 eaten = true;
197197 snake_length = snake_length + food_growth;
198198 play_audio(eat);
199199 }
200200 debug_log(snake_length); // Score
201-
201+
202202 // Check collision
203203 if (get_loop_count() > start_length) {
204204 for (let i = 0; i < snake_length; i = i + 1) {
0 commit comments