Select the types of activity you want to include in your feed.
[READ-ONLY] Mirror of https://github.com/flo-bit/shadow-shmup. Fun and casual top-down, roguelike shoot 'em up browsergame with shadow elements and colorful neon effects
flo-bit.dev/shadow-shmup/
···1515import ProjectileManager, { ProjectileData } from './projectile-manager.js';
16161717import { sound } from '@pixi/sound';
1818+import Controls from './controls.js';
18191920export default class Game {
2021 container: PIXI.Container;
···37383839 playing: boolean = false;
39404141+ controls: Controls;
4242+4043 constructor() {
4144 this.setup();
4245···44474548 window.container = this.container;
4649 window.game = this;
5050+5151+ this.controls = new Controls();
47524853 sound.add('music-intro', {
4954 url: '/shadow-shmup/music-intro.mp3'
···251256252257 this.projectileManager?.update(deltaTime);
253258254254- if (this.playerManager?.players[0].health <= 0) {
259259+ let player = this.playerManager?.players[0];
260260+ if (player && player.health <= 0) {
255261 this.enemyManager?.killAll();
256262 this.projectileManager?.clearAllProjectiles();
257263
+68
src/controls.ts
···11+export default class Controls {
22+ keys: Record<string, boolean> = {};
33+44+ constructor() {
55+ window.addEventListener('keydown', (e) => {
66+ this.keys[e.key.toLowerCase()] = true;
77+ console.log(this.keys);
88+ });
99+ window.addEventListener('keyup', (e) => {
1010+ this.keys[e.key.toLowerCase()] = false;
1111+ });
1212+1313+ window.addEventListener('blur', () => {
1414+ this.keys = {};
1515+ });
1616+1717+ let startX: number | undefined, startY: number | undefined;
1818+ // mobile controls, if touch is moved at least 10 pixels in any direction, it will be considered
1919+ let min = 20;
2020+ window.addEventListener('touchstart', (e) => {
2121+ let touch = e.touches[0];
2222+ startX = touch.clientX;
2323+ startY = touch.clientY;
2424+ });
2525+2626+ window.addEventListener('touchmove', (e) => {
2727+ let touch = e.touches[0];
2828+ if (startX === undefined || startY === undefined) return;
2929+3030+ let dx = touch.clientX - startX;
3131+ let dy = touch.clientY - startY;
3232+3333+ if (Math.abs(dx) > min) {
3434+ this.keys[dx > 0 ? 'd' : 'a'] = true;
3535+ } else {
3636+ this.keys['d'] = false;
3737+ this.keys['a'] = false;
3838+ }
3939+4040+ if (Math.abs(dy) > min) {
4141+ this.keys[dy > 0 ? 's' : 'w'] = true;
4242+ } else {
4343+ this.keys['s'] = false;
4444+ this.keys['w'] = false;
4545+ }
4646+ });
4747+4848+ window.addEventListener('touchend', (e) => {
4949+ this.keys['w'] = false;
5050+ this.keys['a'] = false;
5151+ this.keys['s'] = false;
5252+ this.keys['d'] = false;
5353+ });
5454+ }
5555+5656+ get left() {
5757+ return this.keys['a'] || this.keys['arrowleft'];
5858+ }
5959+ get right() {
6060+ return this.keys['d'] || this.keys['arrowright'];
6161+ }
6262+ get up() {
6363+ return this.keys['w'] || this.keys['arrowup'];
6464+ }
6565+ get down() {
6666+ return this.keys['s'] || this.keys['arrowdown'];
6767+ }
6868+}
+4-4
src/player.ts
···165165 if (this.game.playing) {
166166 let dx = 0,
167167 dy = 0;
168168- if (keys['w']) dy -= 1;
169169- if (keys['s']) dy += 1;
170170- if (keys['a']) dx -= 1;
171171- if (keys['d']) dx += 1;
168168+ if (this.game.controls.up) dy -= 1;
169169+ if (this.game.controls.down) dy += 1;
170170+ if (this.game.controls.left) dx -= 1;
171171+ if (this.game.controls.right) dx += 1;
172172173173 // Normalize diagonal movement
174174 if (dx !== 0 && dy !== 0) {