Merge pull request #10 from koneko/dev

pick tower while choosing another
This commit is contained in:
Koneko 2025-01-17 22:37:05 +01:00 committed by GitHub
commit 55214a67ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 65 additions and 37 deletions

View File

@ -39,7 +39,7 @@ export default class GameAssets {
public static async LoadAssets() { public static async LoadAssets() {
if (this.text) { if (this.text) {
throw 'Do not call GameAssets.LoadAssets() more than once.'; console.warn('Do not call GameAssets.LoadAssets() more than once.');
return; return;
} }
console.log('Loading Texture Assets'); console.log('Loading Texture Assets');
@ -124,9 +124,10 @@ export default class GameAssets {
for (let idx = 0; idx < this.Towers.length; idx++) { for (let idx = 0; idx < this.Towers.length; idx++) {
const tower = this.Towers[idx]; const tower = this.Towers[idx];
for (let i = 0; i < tower.projectileTexturesArrayLength; i++) { for (let i = 0; i < tower.projectileTexturesArrayLength; i++) {
const texture = await this.Load(`/assets/projectiles/${tower.sprite}/${i}.png`); const projTexture = await this.Load(`/assets/projectiles/${tower.sprite}/${i}.png`);
tower.projectileTextures[i] = texture; tower.projectileTextures[i] = projTexture;
} }
tower.texture = await this.Load(`/assets/towers/${tower.sprite}.png`);
} }
} }

View File

@ -38,12 +38,17 @@ export class Cell extends GameObject {
zIndex: 99, zIndex: 99,
interactive: true, interactive: true,
}); });
// ? TODO: make range preview 1 global graphics obj, child. fix
this.rangePreview = new PIXI.Graphics({ this.rangePreview = new PIXI.Graphics({
zIndex: 6, zIndex: 10,
});
this.g = new PIXI.Graphics({
zIndex: 5,
}); });
this.clickDetector.rect(0, 0, this.bb.width, this.bb.height); this.clickDetector.rect(0, 0, this.bb.width, this.bb.height);
this.clickDetector.fill({ color: 0xff0000, alpha: 0 }); this.clickDetector.fill({ color: 0xff0000, alpha: 0 });
this.container.addChild(this.clickDetector); this.container.addChild(this.clickDetector);
this.container.addChild(this.g);
this.container.addChild(this.rangePreview); this.container.addChild(this.rangePreview);
this.clickDetector.on('pointerup', (e) => { this.clickDetector.on('pointerup', (e) => {
Engine.Grid.onGridCellClicked(row, column); Engine.Grid.onGridCellClicked(row, column);
@ -79,9 +84,6 @@ export class Cell extends GameObject {
); );
} }
public gDraw() { public gDraw() {
this.g = new PIXI.Graphics({
zIndex: 5,
});
this.g.rect(0, 0, this.bb.width, this.bb.height); this.g.rect(0, 0, this.bb.width, this.bb.height);
if (this.type == TerrainType.Restricted) { if (this.type == TerrainType.Restricted) {
this.g.fill({ color: 0x222222, alpha: 0.5 }); this.g.fill({ color: 0x222222, alpha: 0.5 });
@ -92,7 +94,6 @@ export class Cell extends GameObject {
} else if (this.type == TerrainType.Buildable) { } else if (this.type == TerrainType.Buildable) {
this.g.stroke({ color: 0x00ff00, alpha: 0.9 }); this.g.stroke({ color: 0x00ff00, alpha: 0.9 });
} }
this.container.addChild(this.g);
} }
public gClear() { public gClear() {
this.g.clear(); this.g.clear();
@ -130,15 +131,25 @@ export class Grid extends GameObject {
} }
} }
} }
public toggleGrid() { public toggleGrid(force?: 'hide' | 'show') {
this.cells.forEach((cell) => { this.cells.forEach((cell) => {
if (force) {
if (force == 'hide') {
cell.gClear();
} else {
cell.gDraw();
}
return;
}
if (this.gridShown) { if (this.gridShown) {
cell.gClear(); cell.gClear();
} else { } else {
cell.gDraw(); cell.gDraw();
} }
}); });
this.gridShown = !this.gridShown; if (force == 'hide') this.gridShown = false;
else if (force == 'show') this.gridShown = true;
else this.gridShown = !this.gridShown;
} }
public addCreep(creep: Creep) { public addCreep(creep: Creep) {
this.creeps.push(creep); this.creeps.push(creep);

View File

@ -12,7 +12,7 @@ export enum TowerBehaviours {
export default class TowerManager { export default class TowerManager {
public isPlacingTower: boolean = false; public isPlacingTower: boolean = false;
public canPlaceTowers: boolean = true; public canPlaceTowers: boolean = true;
private selectedTower: TowerDefinition | null = null; public selectedTower: TowerDefinition | null = null;
private previewSprite: PIXI.Sprite = new PIXI.Sprite({ private previewSprite: PIXI.Sprite = new PIXI.Sprite({
parent: Engine.GameMaster.currentScene.stage, parent: Engine.GameMaster.currentScene.stage,
zIndex: 10, zIndex: 10,
@ -40,6 +40,11 @@ export default class TowerManager {
} }
}); });
} }
public ResetChooseTower() {
this.selectedTower = null;
this.isPlacingTower = false;
Engine.Grid.toggleGrid('hide');
}
public ToggleChoosingTowerLocation(towerName: string) { public ToggleChoosingTowerLocation(towerName: string) {
if (!this.canPlaceTowers) return; if (!this.canPlaceTowers) return;
Engine.Grid.toggleGrid(); Engine.Grid.toggleGrid();
@ -47,6 +52,7 @@ export default class TowerManager {
GameAssets.Towers.forEach((item) => { GameAssets.Towers.forEach((item) => {
if (item.name == towerName) { if (item.name == towerName) {
this.selectedTower = item; this.selectedTower = item;
console.log(this.selectedTower);
} }
}); });
} else { } else {

View File

@ -5,9 +5,9 @@ import TowerTab from './TowerTab';
import GemTab from './GemTab'; import GemTab from './GemTab';
export default class Sidebar extends GuiObject { export default class Sidebar extends GuiObject {
public towerTab: TowerTab;
private bounds: PIXI.Rectangle; private bounds: PIXI.Rectangle;
private sidebarSprite: PIXI.NineSliceSprite; private sidebarSprite: PIXI.NineSliceSprite;
private towerTab: TowerTab;
private gemTab: GemTab; private gemTab: GemTab;
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {

View File

@ -44,7 +44,7 @@ class TowerButton extends GuiObject {
this.container.addChild(this.frameSprite); this.container.addChild(this.frameSprite);
parent.addChild(this.container); parent.addChild(this.container);
Engine.GameScene.events.on(TowerEvents.TowerPlacedEvent, (name) => { Engine.GameScene.events.on(TowerEvents.TowerPlacedEvent, (name) => {
this.frameSprite.tint = 0xffffff; // reset the tint after a tower has been placed this.resetTint();
}); });
this.container.onpointerenter = (e) => { this.container.onpointerenter = (e) => {
// add on mouse over info (banner next to sidebar) // add on mouse over info (banner next to sidebar)
@ -53,24 +53,28 @@ class TowerButton extends GuiObject {
this.container.onpointerleave = (e) => {}; this.container.onpointerleave = (e) => {};
} }
public onClick(e: PIXI.FederatedPointerEvent): void { public onClick(e: PIXI.FederatedPointerEvent): void {
if (Engine.TowerManager.isPlacingTower && Engine.TowerManager.selectedTower.name != this.towerName) {
Engine.GameScene.sidebar.towerTab.resetTint();
Engine.TowerManager.ResetChooseTower();
}
if (this.frameSprite.tint == 0x00ff00) this.frameSprite.tint = 0xffffff; if (this.frameSprite.tint == 0x00ff00) this.frameSprite.tint = 0xffffff;
else this.frameSprite.tint = 0x00ff00; else this.frameSprite.tint = 0x00ff00;
Engine.TowerManager.ToggleChoosingTowerLocation(this.towerName); Engine.TowerManager.ToggleChoosingTowerLocation(this.towerName);
} }
public resetTint() {
this.frameSprite.tint = 0xffffff;
}
} }
export default class TowerTab extends GuiObject { export default class TowerTab extends GuiObject {
private bounds: PIXI.Rectangle; private bounds: PIXI.Rectangle;
private towerTabSprite: PIXI.NineSliceSprite; private towerTabSprite: PIXI.NineSliceSprite;
private towerList: Array<any> = []; private towerButtons: TowerButton[] = [];
constructor(bounds: PIXI.Rectangle) { constructor(bounds: PIXI.Rectangle) {
super(false); super(false);
this.bounds = bounds; this.bounds = bounds;
GameAssets.Towers.forEach((twr) => {
let obj = { name: twr.name, description: twr.description, cost: twr.stats.cost };
this.towerList.push(obj);
});
this.container.x = this.bounds.x; this.container.x = this.bounds.x;
this.container.y = this.bounds.y; this.container.y = this.bounds.y;
this.towerTabSprite = new PIXI.NineSliceSprite({ this.towerTabSprite = new PIXI.NineSliceSprite({
@ -84,26 +88,32 @@ export default class TowerTab extends GuiObject {
this.towerTabSprite.width = this.bounds.width; this.towerTabSprite.width = this.bounds.width;
this.towerTabSprite.height = this.bounds.height; this.towerTabSprite.height = this.bounds.height;
this.container.addChild(this.towerTabSprite); this.container.addChild(this.towerTabSprite);
this.towerButtons.push(
new TowerButton( new TowerButton(
0, 0,
0, 0,
70, 70,
70, 70,
this.container, this.container,
GameAssets.RedBackground, GameAssets.RedBackground,
'Basic Tower', 'Basic Tower',
GameAssets.HammerIconTexture GameAssets.HammerIconTexture
)
); );
new TowerButton( this.towerButtons.push(
0, new TowerButton(
1, 0,
70, 1,
70, 70,
this.container, 70,
GameAssets.GreenBackground, this.container,
'Circle Tower', GameAssets.GreenBackground,
GameAssets.HomeIconTexture 'Circle Tower',
GameAssets.HomeIconTexture
)
); );
} }
public resetTint() {
this.towerButtons.forEach((item) => item.resetTint());
}
} }