exported creepstats to seperate file

This commit is contained in:
koneko 2024-10-18 00:57:24 +02:00
parent 660599c439
commit e04d4a76c7
6 changed files with 62 additions and 49 deletions

View File

@ -0,0 +1,14 @@
[
{
"health": 2,
"speed": 0.0005,
"special": null,
"resistance": {
"physical": 0,
"divine": 0,
"fire": 0,
"ice": 0,
"frostfire": 0
}
}
]

View File

@ -30,7 +30,6 @@
[ [
[8, 0], [8, 0],
[8, 1], [8, 1],
[8, 2],
[9, 2], [9, 2],
[10, 2], [10, 2],
[11, 2], [11, 2],
@ -44,7 +43,6 @@
[11, 10], [11, 10],
[10, 10], [10, 10],
[9, 10], [9, 10],
[8, 10],
[8, 11], [8, 11],
[8, 12], [8, 12],
[8, 13], [8, 13],

View File

@ -1,5 +1,5 @@
import * as PIXI from 'pixi.js'; import * as PIXI from 'pixi.js';
import { MissionDefinition } from './Definitions'; import { CreepStats, MissionDefinition } from './Definitions';
export default class Assets { export default class Assets {
public static async LoadAssets() { public static async LoadAssets() {
@ -12,6 +12,13 @@ export default class Assets {
}); });
console.log('Loading Missions'); console.log('Loading Missions');
await this.LoadMissions(); await this.LoadMissions();
await this.LoadCreepStats();
}
public static async LoadCreepStats() {
const res = await fetch('/assets/CreepStats.json');
const stats = await res.json();
this.CreepStats = stats;
} }
private static async LoadMissions() { private static async LoadMissions() {
@ -38,4 +45,5 @@ export default class Assets {
public static MissionBackgrounds: PIXI.Texture[] = []; public static MissionBackgrounds: PIXI.Texture[] = [];
public static Missions: MissionDefinition[]; public static Missions: MissionDefinition[];
public static CreepStats: CreepStats[];
} }

View File

@ -30,6 +30,21 @@ export type WaveDefinition = {
creeps: CreepType[]; creeps: CreepType[];
}; };
export type CreepStats = {
health: number;
speed: number;
special: Function;
resistance: CreepResistances;
};
export type CreepResistances = {
physical: number;
divine: number;
fire: number;
ice: number;
frostfire: number;
};
export type PathDefinition = [[row: number, column: number]]; export type PathDefinition = [[row: number, column: number]];
export enum CreepType { export enum CreepType {
@ -40,6 +55,7 @@ export enum CreepType {
export enum TerrainType { export enum TerrainType {
Restricted = 0, Restricted = 0,
Buildable = 1, Buildable = 1,
Path = 9,
} }
export enum GemType { export enum GemType {

View File

@ -1,42 +1,8 @@
import Assets from '../base/Assets'; import Assets from '../base/Assets';
import { CreepType, PathDefinition } from '../base/Definitions'; import { CreepStats, CreepType, PathDefinition } from '../base/Definitions';
import GameObject from '../base/GameObject'; import GameObject from '../base/GameObject';
import * as PIXI from 'pixi.js'; import * as PIXI from 'pixi.js';
import GameScene from '../scenes/GameScene'; import GameScene from '../scenes/GameScene';
import { Grid } from './Grid';
export function CreepStats(ctype: CreepType): object {
switch (ctype) {
case CreepType.Basic:
return {
health: 2,
speed: 0.45,
special: null,
resistance: {
physical: 0,
divine: 0,
fire: 0,
ice: 0,
frostfire: 0,
},
};
case CreepType.Fast:
throw new Error('Fast creep not defined.');
default:
return {
health: null,
speed: null,
special: null,
resistance: {
physical: null,
divine: null,
fire: null,
ice: null,
frostfire: null,
},
};
}
}
export enum CreepEvents { export enum CreepEvents {
Died = 'died', Died = 'died',
@ -48,23 +14,30 @@ export enum CreepEvents {
export default class Creep extends GameObject { export default class Creep extends GameObject {
public creepType: CreepType; public creepType: CreepType;
private path: PathDefinition; private path: PathDefinition;
private stats: CreepStats;
private pathIndex: number = 0; private pathIndex: number = 0;
private speed: number = 0.002; private speed: number;
private gameScene: GameScene; private gameScene: GameScene;
public health: number = 2; public health: number;
public escaped: boolean = false; public escaped: boolean = false;
public died: boolean = false; public died: boolean = false;
public x: number; // X and Y are local to the grid, not canvas public x: number; // X and Y are local to the grid, not canvas
public y: number; public y: number;
constructor(creepType: CreepType, path: PathDefinition, gameScene: GameScene, bounds?: PIXI.Rectangle) { constructor(creepType: CreepType, path: PathDefinition, gameScene: GameScene, bounds?: PIXI.Rectangle) {
super(bounds); super(bounds);
this.creepType = creepType;
this.path = path;
this.gameScene = gameScene; this.gameScene = gameScene;
this.creepType = creepType;
this.stats = Assets.CreepStats[this.creepType];
this.speed = this.stats.speed;
this.health = this.stats.health;
this.path = path;
this.x = path[0][1] + 0.5; // centered this.x = path[0][1] + 0.5; // centered
this.y = path[0][0] + 0.5; this.y = path[0][0] + 0.5;
this.gameScene.grid.container.addChild(this.container); this.gameScene.grid.container.addChild(this.container);
} }
private gridUnitsToPixels(gridUnits) {
return this.gameScene.grid.gridUnitsToPixels(gridUnits);
}
public update(elapsedMS: number) { public update(elapsedMS: number) {
if (this.pathIndex + 1 == this.path.length) { if (this.pathIndex + 1 == this.path.length) {
if (this.escaped) return; if (this.escaped) return;
@ -108,10 +81,10 @@ export default class Creep extends GameObject {
if (increaseIndex) this.pathIndex++; if (increaseIndex) this.pathIndex++;
this.setBounds( this.setBounds(
new PIXI.Rectangle( new PIXI.Rectangle(
this.gameScene.grid.gridUnitsToPixels(this.x), this.gridUnitsToPixels(this.x),
this.gameScene.grid.gridUnitsToPixels(this.y), this.gridUnitsToPixels(this.y),
this.gameScene.grid.gridUnitsToPixels(0.5), this.gridUnitsToPixels(0.5),
this.gameScene.grid.gridUnitsToPixels(0.6) this.gridUnitsToPixels(0.6)
) )
); );
} }

View File

@ -30,11 +30,17 @@ export class Cell extends GameObject {
case TerrainType.Restricted: case TerrainType.Restricted:
g.fill(0xff0000); g.fill(0xff0000);
break; break;
case TerrainType.Path:
g.fill(0xff00ff);
break;
case TerrainType.Buildable: case TerrainType.Buildable:
g.stroke(0x00ff00); g.stroke(0x00ff00);
break; break;
} }
this.container.addChild(g); this.container.addChild(g);
this.container.x = this.bounds.x;
this.container.y = this.bounds.y;
return; // comment to enable debugging
const text = new PIXI.Text({ const text = new PIXI.Text({
text: `${this.row}|${this.column}`, text: `${this.row}|${this.column}`,
style: new PIXI.TextStyle({ style: new PIXI.TextStyle({
@ -48,8 +54,6 @@ export class Cell extends GameObject {
text.x = this.bounds.width / 2; text.x = this.bounds.width / 2;
text.y = this.bounds.height / 2; text.y = this.bounds.height / 2;
if (this.isPath) text.text += 'P'; if (this.isPath) text.text += 'P';
this.container.x = this.bounds.x;
this.container.y = this.bounds.y;
} }
} }
@ -73,7 +77,7 @@ export class Grid extends GameObject {
type = 1; type = 1;
} }
const isPath = this.gameMap.paths.some((path) => path.some((p) => p[0] === x && p[1] === y)); const isPath = this.gameMap.paths.some((path) => path.some((p) => p[0] === x && p[1] === y));
if (isPath) type = TerrainType.Restricted; if (isPath) type = TerrainType.Path;
let cell = new Cell(type, x, y, isPath, this); let cell = new Cell(type, x, y, isPath, this);
this.cells.push(cell); this.cells.push(cell);
} }