Repository navigation
Fix/space invaders 329 #688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lili11-OSS
wants to merge
6
commits into
NextCommunity:main
Choose a base branch
from
Lili11-OSS:fix/space-invaders-329
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6051a3a
Add scoring, defeat detection and round restart to Space Invaders
Lili11-OSS e67b049
Cover scoring, defeat, reward idempotence and restart
Lili11-OSS 2cad0de
Address static analysis findings in Space Invaders contribution
Lili11-OSS 62dc09b
Address static analysis findings in Space Invaders contribution
Lili11-OSS 592b4b6
Keep new game canvas mounted after lifecycle cleanup
Lili11-OSS eaa171a
Keep new game canvas mounted after lifecycle cleanup
Lili11-OSS File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| const { test } = require('node:test'); | ||
| const assert = require('node:assert/strict'); | ||
| const fs = require('node:fs'); | ||
| const vm = require('node:vm'); | ||
|
|
||
| const game = () => { | ||
| let config = null; | ||
| const rewards = []; | ||
| const scores = []; | ||
| const context = vm.createContext({ | ||
| window: { innerWidth: 1000, innerHeight: 800 }, | ||
| document: { createElement: () => ({ style: {} }), body: { appendChild() { /* Unused renderer hook in this scene test double. */ } } }, | ||
| Phaser: { CANVAS: 1 }, XP_SPACE_INVADERS_WIN: 100, | ||
| GameManager: { | ||
| loadPhaser: fn => fn(), create: (_, c) => { config = c; }, | ||
| awardXP: xp => rewards.push(xp), incrementStat: () => 1, | ||
| grantAchievement() { /* Unused renderer hook in this scene test double. */ }, setHighScore: (_, score) => scores.push(score), destroy() { /* Unused renderer hook in this scene test double. */ }, | ||
| }, | ||
| }); | ||
| vm.runInContext(`${fs.readFileSync('src/assets/js/games/space-invaders.js', 'utf8')}\nSpaceInvaders.launch();`, context); | ||
| const handlers = {}; | ||
| const group = () => ({ children: [], add(x) { this.children.push(x); }, getChildren() { return this.children.filter(x => x.active); }, countActive() { return this.getChildren().length; } }); | ||
| const scene = { | ||
| scale: { width: 1000, height: 800 }, | ||
| add: { text(x,y,text) { return { x,y,text,width:24,active:true, setOrigin() { return this; },setText(t) { this.text=t; },getBounds() { return {top:this.y,bottom:this.y+24}; },destroy() { this.active=false; } }; } }, | ||
| physics: { pause() { scene.paused=true; }, add: { | ||
| existing(x) { x.body={setCollideWorldBounds(){ /* Unused physics hook in this scene test double. */ },setAllowGravity(){ /* Unused physics hook in this scene test double. */ },setSize(){ /* Unused physics hook in this scene test double. */ },setVelocityX(v){this.vx=v;},setVelocityY(){ /* Unused physics hook in this scene test double. */ }}; }, | ||
| group, overlap(a,b,fn) { scene.hit=fn; }, | ||
| } }, | ||
| time: { now:1000,addEvent(c) { scene.move=c.callback; return {remove() { scene.timerRemoved=true; }}; } }, | ||
| input: { once() { /* Unused renderer hook in this scene test double. */ },keyboard: {createCursorKeys:()=>({left:{},right:{},space:{}}),once(k,fn){handlers[k]=fn;} } }, | ||
| scene: { restart() { config.scene.create.call(scene); } }, | ||
| }; | ||
| config.scene.create.call(scene); | ||
| return { scene, handlers, rewards, scores, update:()=>config.scene.update.call(scene) }; | ||
| }; | ||
|
|
||
| test('hits update HUD; duplicate collision cannot score twice',()=>{ | ||
| const {scene}=game(); const bullet={active:true,destroy(){this.active=false;}}; | ||
| const alien=scene.si_aliens.getChildren()[0]; | ||
| scene.hit(bullet,alien); scene.hit(bullet,alien); | ||
| assert.equal(scene.si_score,10);assert.equal(scene.si_scoreText.text,'SCORE 10'); | ||
| }); | ||
|
|
||
| test('invasion ends round, freezes input, awards no XP; restart resets round',()=>{ | ||
| const {scene,update,handlers,rewards,scores}=game(); | ||
| scene.si_aliens.getChildren()[0].y=scene.si_player.y; | ||
| update(); update(); | ||
| assert.equal(scene.si_finished,true);assert.equal(scene.timerRemoved,true); | ||
| assert.equal(scene.paused,true);assert.deepEqual(rewards,[]);assert.deepEqual(scores,[0]); | ||
| scene.si_cursors.space.isDown=true; update();assert.equal(scene.si_bullets.countActive(),0); | ||
| handlers['keydown-R']();assert.equal(scene.si_finished,false);assert.equal(scene.si_score,0); | ||
| assert.equal(scene.si_aliens.countActive(),50); | ||
| }); | ||
|
|
||
| test('victory awards once, stores round score, and supports restart',()=>{ | ||
| const {scene,rewards,scores,handlers}=game(); | ||
| for(const alien of [...scene.si_aliens.getChildren()]) scene.hit({active:true,destroy(){this.active=false;}},alien); | ||
| assert.equal(scene.si_finished,true);assert.equal(scene.si_score,500); | ||
| assert.deepEqual(rewards,[100]);assert.deepEqual(scores,[500]); | ||
| scene.hit({active:true},{active:true});assert.deepEqual(rewards,[100]); | ||
| handlers['keydown-R']();assert.equal(scene.si_scoreText.text,'SCORE 0'); | ||
| }); | ||
|
|
||
| test('launch attaches the new canvas after manager cleanup, including relaunch', () => { | ||
| const canvases = []; | ||
| const context = vm.createContext({ | ||
| window: { innerWidth: 1000, innerHeight: 800 }, | ||
| document: { | ||
| createElement: () => ({ style: {}, remove() { canvases.splice(canvases.indexOf(this), 1); } }), | ||
| getElementById: id => canvases.find(canvas => canvas.id === id), | ||
| body: { appendChild: canvas => canvases.push(canvas) }, | ||
| }, | ||
| Phaser: { CANVAS: 1, Game: class { | ||
| constructor(config) { this.canvas = config.canvas; } | ||
| destroy() { this.canvas.remove(); } | ||
| } }, | ||
| }); | ||
| vm.runInContext(fs.readFileSync('src/assets/js/games/game-manager.js', 'utf8'), context); | ||
| vm.runInContext(fs.readFileSync('src/assets/js/games/space-invaders.js', 'utf8'), context); | ||
| vm.runInContext('SpaceInvaders.launch()', context); | ||
| assert.equal(canvases.length, 1); | ||
| const firstCanvas = canvases[0]; | ||
| vm.runInContext('SpaceInvaders.launch()', context); | ||
| assert.equal(canvases.length, 1); | ||
| assert.notEqual(canvases[0], firstCanvas); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variables that aren't defined, but accessed may throw reference errors at runtime.