Skip to content

Commit

Permalink
Implement time scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
rajsite committed Dec 22, 2022
1 parent 36d536b commit 8395f69
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Expand Up @@ -44,6 +44,8 @@ jobs:
run: docker build -t test1 .
- name: Build GBA ROM
run: docker run -v $PWD/build:/mydata --rm test1 ./build.sh -f wasm2c /mydata/cart.wasm gba /mydata/cart
- name: Build GBA ROM Time Scale
run: docker run -v $PWD/build:/mydata --rm test1 ./build.sh -f wasm2c /mydata/cart.timescale.wasm gba /mydata/cart.timescale
- name: Upload artifact
uses: actions/upload-pages-artifact@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,3 +1,3 @@
# arvaders

See the [web version](https://rajsite.github.io/arvaders/). Or the GBA ROM running in a [web GBA emulator](https://gba.ninja/?autorun=https://rajsite.github.io/arvaders/cart.gba) (click "Run it anyway"). Or download the [GBA ROM](https://rajsite.github.io/arvaders/cart.gba).
See the [web version](https://rajsite.github.io/arvaders/). Or the GBA ROM running in a [web GBA emulator](https://gba.ninja/?autorun=https://rajsite.github.io/arvaders/cart.timescale.gba) (click "Run it anyway"). Or download the [GBA ROM](https://rajsite.github.io/arvaders/cart.timescale.gba).
10 changes: 10 additions & 0 deletions asconfig.json
Expand Up @@ -23,6 +23,16 @@
"noAssert": true,
"use": "abort="
},
"gba": {
"optimizeLevel": 3,
"shrinkLevel": 1,
"noAssert": true,
"use": [
"abort=",
"TIME_SCALE=4"
],
"outFile": "build/cart.timescale.wasm"
},
"debug": {
"debug": true,
"sourceMap": "http://localhost:4444/cart.wasm.map",
Expand Down
7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -3,11 +3,12 @@
"private": true,
"version": "1.0.0",
"scripts": {
"build": "asc --target release && w4 bundle --html build/index.html build/cart.wasm",
"build": "asc --target release && w4 bundle --html build/index.html build/cart.wasm && asc --target gba",
"build:debug": "asc --target debug",
"build:watch": "nodemon",
"w4:run": "w4 watch build/cart.wasm",
"w4:watch": "w4 watch build/cart.wasm",
"w4:run": "w4 run build/cart.wasm",
"w4:run:timescale": "w4 run build/cart.timescale.wasm",
"w4:watch": "w4 watch",
"dev": "concurrently \"npm:*:watch\""
},
"devDependencies": {
Expand Down
14 changes: 9 additions & 5 deletions src/scenes/level1.ts
Expand Up @@ -9,6 +9,7 @@ import { Condition } from '../systems';
import * as scenes from '.';
import { Scene } from "./types";
import { SCREEN_SIZE } from "../utilities/screen";
import { scaleVelocity } from "../utilities/time";

class Level1Start extends Scene {
run(): Scene {
Expand All @@ -24,8 +25,8 @@ class Level1Start extends Scene {
initializeVisualPosition(player, images.player);
player.position!.x = 0;
player.position!.y = SCREEN_SIZE - player.position!.height;
player.position!.vx = 8;
player.position!.vy = 0;
player.position!.vx = scaleVelocity(4);
player.position!.vy = scaleVelocity(0);
player.position!.movement = movements.gamepadLeftRight;
player.health!.value = 100;

Expand All @@ -41,7 +42,8 @@ class Level1Start extends Scene {
initializeVisualPosition(enemy, images.invader);
enemy.position!.x = ((enemy.position!.width + 10) * i);
enemy.position!.y = 0;
enemy.position!.vx = 2;
enemy.position!.vx = scaleVelocity(1);
enemy.position!.vy = scaleVelocity(0);
enemy.position!.movement = movements.sideToSide;
enemy.health!.value = 100;
enemy.attack!.attackInterval = 50;
Expand All @@ -60,7 +62,8 @@ class Level1Start extends Scene {
bullet.exists = false;
bullet.position!.movement = movements.forwardToAstral;
bullet.damage!.value = 50;
bullet.position!.vy = -2;
bullet.position!.vx = scaleVelocity(0);
bullet.position!.vy = scaleVelocity(-2);
}

for (let i = 0; i < 10; i++) {
Expand All @@ -75,7 +78,8 @@ class Level1Start extends Scene {
bullet.exists = false;
bullet.position!.movement = movements.forwardToAstral;
bullet.damage!.value = 50;
bullet.position!.vy = 2;
bullet.position!.vy = scaleVelocity(0);
bullet.position!.vy = scaleVelocity(2);
}

return scenes.level1Update;
Expand Down
3 changes: 2 additions & 1 deletion src/scenes/lose.ts
Expand Up @@ -3,10 +3,11 @@ import * as scenes from './';
import { Scene } from "./types";
import * as images from '../images';
import { SCREEN_SIZE } from '../utilities/screen';
import { scaleTime } from '../utilities/time';

class Lose extends Scene {
private ticks: i32 = 0;
private readonly maxTicks: i32 = 120;
private readonly maxTicks: i32 = scaleTime(60);

run(): Scene {
this.ticks++;
Expand Down
3 changes: 2 additions & 1 deletion src/scenes/win.ts
Expand Up @@ -3,10 +3,11 @@ import * as scenes from './';
import { Scene } from "./types";
import * as images from '../images';
import { SCREEN_SIZE } from '../utilities/screen';
import { scaleTime } from '../utilities/time';

class Win extends Scene {
private ticks: i32 = 0;
private readonly maxTicks: i32 = 120;
private readonly maxTicks: i32 = scaleTime(60);

run(): Scene {
this.ticks++;
Expand Down
5 changes: 3 additions & 2 deletions src/utilities/tick.ts
@@ -1,11 +1,12 @@
import * as w4 from "../wasm4";
import * as gamepad from './gamepad';
import { scaleTime } from "./time";

let count = 0;
export function tick (x: i32, y: i32): void {
count++;
const scaledCount: i32 = count/20;
const show: bool = scaledCount % 2 === 0;
const seconds: i32 = count/scaleTime(60);
const show: bool = seconds % 2 === 0;

store<u16>(w4.DRAW_COLORS, 0x002);
if (gamepad.is & w4.BUTTON_1) {
Expand Down
5 changes: 5 additions & 0 deletions src/utilities/time.ts
@@ -0,0 +1,5 @@
// @ts-expect-error checking global TIME_SCALE
const timeScale = isDefined(TIME_SCALE) ? TIME_SCALE : 1;

export const scaleTime = (time: i32): i32 => time / timeScale;
export const scaleVelocity = (velocity: i32): i32 => velocity * timeScale;
2 changes: 1 addition & 1 deletion src/world.ts
Expand Up @@ -5,7 +5,7 @@ import { Pool } from './utilities/pool';
import { Query } from './query';
import { Attack } from './components/attack';

const MAX_CAPACITY = 200;
const MAX_CAPACITY = 150;

const entityPool = new Pool<Entity>(MAX_CAPACITY, Entity.create, Entity.reset);
const attackPool = new Pool<Attack>(MAX_CAPACITY, Attack.create, Attack.reset);
Expand Down

0 comments on commit 8395f69

Please sign in to comment.