|
| 1 | +import { Scene } from "phaser"; |
| 2 | +import { Hero } from "../game-objects"; |
| 3 | +import { GameHelper } from "../helpers"; |
| 4 | +import { Sprite, Tilemap, Tileset } from "../phaser-aliases"; |
| 5 | +import { |
| 6 | + BackgroundSound, |
| 7 | + ImageTag, |
| 8 | + SfxTag, |
| 9 | + TilemapLayerTag, |
| 10 | + TilemapObjectsTag, |
| 11 | + TilemapObjectsType, |
| 12 | + TilemapTag, |
| 13 | + TilesetTag, |
| 14 | +} from "../tags"; |
| 15 | + |
| 16 | +const tilesetNames = ["base-tiles", "decorative-tiles", "water-animation", "tree_bright3", "tree_bright4"]; |
| 17 | +type TilesetName = (typeof tilesetNames)[number]; |
| 18 | + |
| 19 | +export class ForestLevel { |
| 20 | + private get _physics(): Phaser.Physics.Arcade.ArcadePhysics { |
| 21 | + return this._scene.physics; |
| 22 | + } |
| 23 | + private get _mainCam(): Phaser.Cameras.Scene2D.Camera { |
| 24 | + return this._scene.cameras.main; |
| 25 | + } |
| 26 | + private _colliderGroup: Phaser.Physics.Arcade.StaticGroup; |
| 27 | + private _sceneWidth = 3200; |
| 28 | + private _map: Tilemap; |
| 29 | + private _layersConfig: Record<TilemapLayerTag, TilesetName[]> = { |
| 30 | + [TilemapLayerTag.BACKGROUND]: ["base-tiles"], |
| 31 | + [TilemapLayerTag.FOREGROUND]: ["base-tiles", "water-animation"], |
| 32 | + [TilemapLayerTag.DECORATIONS]: ["base-tiles", "decorative-tiles", "tree_bright3", "tree_bright4"], |
| 33 | + }; |
| 34 | + private _tilesetsConfig: Record<TilesetName, TilesetTag> = { |
| 35 | + "base-tiles": TilesetTag.FOREST_BASE, |
| 36 | + "decorative-tiles": TilesetTag.FOREST_DECORATIVE, |
| 37 | + "water-animation": TilesetTag.FOREST_WATER_ANIMATION, |
| 38 | + tree_bright3: TilesetTag.FOREST_TREE_BRIGHT_3_ANIMATION, |
| 39 | + tree_bright4: TilesetTag.FOREST_TREE_BRIGHT_4_ANIMATION, |
| 40 | + }; |
| 41 | + |
| 42 | + constructor(public hero: Hero, private _scene: Scene) { |
| 43 | + this._scene.sound.play(BackgroundSound.RIVER_FLOWING_INSECTS, { |
| 44 | + loop: true, |
| 45 | + volume: GameHelper.audioIsEnabled ? 1 : 0, |
| 46 | + }); |
| 47 | + |
| 48 | + this._mainCam.setBounds(0, 0, this._sceneWidth, this._scene.scale.height); |
| 49 | + |
| 50 | + this.addBackgrounds(); |
| 51 | + |
| 52 | + this._map = this._scene.make.tilemap({ key: TilemapTag.FOREST }); |
| 53 | + |
| 54 | + this.generateMap(); |
| 55 | + |
| 56 | + this._colliderGroup = this._physics.add.staticGroup(); |
| 57 | + |
| 58 | + this.addColliders(); |
| 59 | + this.addHero(); |
| 60 | + this.setCollisions(); |
| 61 | + } |
| 62 | + |
| 63 | + private addBackgrounds(): void { |
| 64 | + const backgrounds = [ |
| 65 | + ImageTag.FOREST_BACKGROUND_DAY_1, |
| 66 | + ImageTag.FOREST_BACKGROUND_DAY_2, |
| 67 | + ImageTag.FOREST_BACKGROUND_DAY_3, |
| 68 | + ImageTag.FOREST_BACKGROUND_DAY_4, |
| 69 | + ]; |
| 70 | + |
| 71 | + backgrounds.forEach((background) => { |
| 72 | + this._scene.add |
| 73 | + .tileSprite(0, 0, this._sceneWidth, this._scene.scale.height, background) |
| 74 | + .setOrigin(0) |
| 75 | + .setScale(1.1, 1.1); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + private generateMap(): void { |
| 80 | + // assign each tileset to its name. |
| 81 | + const tilesetsMap: Record<TilesetName, Tileset> = tilesetNames.reduce<Record<TilesetName, Tileset>>( |
| 82 | + (acc, tilesetName) => { |
| 83 | + const tileset = this._map.addTilesetImage(tilesetName, this._tilesetsConfig[tilesetName], 32, 32, 1, 2); |
| 84 | + |
| 85 | + if (!tileset) { |
| 86 | + throw Error("Tileset not found"); |
| 87 | + } |
| 88 | + acc[tilesetName] = tileset; |
| 89 | + return acc; |
| 90 | + }, |
| 91 | + {} |
| 92 | + ); |
| 93 | + |
| 94 | + this._map.createLayer(TilemapLayerTag.BACKGROUND, this.getTilesetForLayer(TilemapLayerTag.BACKGROUND, tilesetsMap)); |
| 95 | + this._map.createLayer(TilemapLayerTag.FOREGROUND, this.getTilesetForLayer(TilemapLayerTag.FOREGROUND, tilesetsMap)); |
| 96 | + this._map.createLayer( |
| 97 | + TilemapLayerTag.DECORATIONS, |
| 98 | + this.getTilesetForLayer(TilemapLayerTag.DECORATIONS, tilesetsMap) |
| 99 | + ); |
| 100 | + |
| 101 | + this._scene.sys.animatedTiles.init(this._map); |
| 102 | + } |
| 103 | + |
| 104 | + private getTilesetForLayer(layer: TilemapLayerTag, tilesetsMap: Record<TilesetName, Tileset>): Tileset[] { |
| 105 | + return this._layersConfig[layer].map((tilesetName) => tilesetsMap[tilesetName]); |
| 106 | + } |
| 107 | + |
| 108 | + private addColliders(): void { |
| 109 | + const colliders = this._map.createFromObjects(TilemapObjectsTag.COLLIDERS, { |
| 110 | + type: TilemapObjectsType.COLLIDER, |
| 111 | + classType: Sprite, |
| 112 | + }); |
| 113 | + |
| 114 | + this._colliderGroup.addMultiple(colliders); |
| 115 | + |
| 116 | + colliders |
| 117 | + .filter((collider): collider is Sprite => collider instanceof Sprite) |
| 118 | + .forEach((collider) => { |
| 119 | + collider.alpha = 0; |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + private addHero(): void { |
| 124 | + const heroPosition = this._map |
| 125 | + .createFromObjects(TilemapObjectsTag.POSITIONS, { |
| 126 | + name: "hero", |
| 127 | + classType: Sprite, |
| 128 | + }) |
| 129 | + .at(0); |
| 130 | + |
| 131 | + if (!(heroPosition instanceof Sprite)) { |
| 132 | + throw Error("Hero position is not a sprite"); |
| 133 | + } |
| 134 | + |
| 135 | + heroPosition.alpha = 0; |
| 136 | + |
| 137 | + this.hero.spawn(heroPosition.x, heroPosition.y); |
| 138 | + |
| 139 | + this._mainCam.startFollow(this.hero); |
| 140 | + } |
| 141 | + |
| 142 | + private setCollisions(): void { |
| 143 | + this._physics.add.collider(this.hero, this._colliderGroup); |
| 144 | + |
| 145 | + this._physics.add.collider(this.hero.projectiles, this._colliderGroup, (projectile) => { |
| 146 | + console.log("collided"); |
| 147 | + this._scene.time.addEvent({ |
| 148 | + delay: 4000, |
| 149 | + callback: () => { |
| 150 | + projectile.destroy(); |
| 151 | + }, |
| 152 | + }); |
| 153 | + this._scene.sound.play(SfxTag.ARROW_WALL_IMPACT); |
| 154 | + }); |
| 155 | + } |
| 156 | +} |
0 commit comments