Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 1x 1x 1x 1x 36x 3x 1x 2x 41x 1x 40x 52x 2x 50x 3x 1x 2x 57x 57x 57x 54x 1x 54x 54x 54x 54x 1x 1x 7x 1x 6x 1x 6x 6x 30x 29x 1x 3x 40x 40x 2x 2x 6x 5x 1x 4x 8x 10x 10x 9x 9x 4x 4x 4x 4x 9x 5x 5x 5x 4x 5x 4x 8x 4x 8x | import { IEntity } from "./entities/entity.js"; import { ISurfaceController, SurfaceController } from "../controller/surface/surfaceController.js"; import { IAssetsController } from "../controller/assets/assetsController.js"; import { IInputController } from "../controller/input/inputController.js"; import { IFrameTime } from "../models/frameTime.js"; import { NotInitializedException } from "../environment/exceptions.js"; import { IEntityBase, EntityBase } from "./entities/base/entityBase.js"; import { IInputTypePointer } from "../controller/input/types/inputTypePointer.js"; export interface IScene extends IEntityBase { isLoop: boolean; initialize(surface: ISurfaceController, input: IInputController): void; update(frameTime: IFrameTime): void; } export class Scene extends EntityBase implements IScene { private _isLoop: boolean; private _assets?: IAssetsController; private _surface?: ISurfaceController; private _input?: IInputController; public get isLoop(): boolean { return this._isLoop; } public set isLoop(value: boolean) { if (this._isLoop === value) return; this._isLoop = value; } protected get assets(): IAssetsController { if (!this._assets) { throw new NotInitializedException("Scene"); } return this._assets; } protected get surface(): ISurfaceController { if (!this._surface) { throw new NotInitializedException("Scene"); } return this._surface; } protected get input(): IInputController { if (!this._input) { throw new NotInitializedException("Scene"); } return this._input; } public constructor(assets: IAssetsController) { super(); this._isLoop = true; this._assets = assets; } public initialize(surface: ISurfaceController, input: IInputController): void { if (this._surface) { this._surface.removeEventListener(SurfaceController.surfaceChangeEvent, this._onRefresh); } this._input = input; this._surface = surface; this._surface.addEventListener(SurfaceController.surfaceChangeEvent, this._onRefresh); this.onInitialized(); } public override destroy(): void { super.destroy(); this.surface.removeEventListener(SurfaceController.surfaceChangeEvent, this._onRefresh); } public update(frameTime: IFrameTime): void { if (!this.isActive || !this.isVisible) { return; } if (!this.isLoop) { this.isActive = false; } this._surface?.clear(); this.updateEntities(this.children, frameTime); } public override refresh(): void { if (this.isLoop) return; this.isActive = true; } public override invalidate(): void { this._input?.refresh(); } protected onInitialized(): void { //initialize scene } protected override initializeChild(child: IEntity): void { super.initializeChild(child); child.initialize(this.assets, this.surface); } protected override changeCursor(style: string): void { const pointer = this.input.get<IInputTypePointer>("Pointer"); pointer?.setStyle(style); } protected isActiveChanged(value: boolean): void { this.children.forEach(child => child.isActive = value); } protected isVisibleChanged(value: boolean): void { if (value) return; this._surface?.clear(); } protected isEnabledChanged(value: boolean): void { this.children.forEach(child => child.isEnabled = value); } private updateEntities(entities: ReadonlyArray<IEntity>, frameTime: IFrameTime): void { const colliders = new Array<IEntity>(); const context2d = this.surface.context2d; const zGroups = this.getGroups(entities, colliders); zGroups.forEach(zGroup => zGroup.forEach(entity => { entity.update(frameTime); entity.collision(...this.getCollisions(entity, colliders)); entity.draw(context2d); this.updateEntities(entity.children, frameTime); })); } private getGroups(entities: ReadonlyArray<IEntity>, colliders: Array<IEntity>): IEntity[][] { return entities.reduce<Array<IEntity[]>>((group: IEntity[][], entity: IEntity): IEntity[][] => { group[entity.zIndex] = group[entity.zIndex] ?? []; group[entity.zIndex]!.push(entity); if (entity.hitBox) { colliders.push(entity); } return group; }, []); } private getCollisions(entity: IEntity, colliders: ReadonlyArray<IEntity>): IEntity[] { return colliders.reduce((array: IEntity[], sibling: IEntity): IEntity[] => { if (sibling !== entity && entity.hitBox.intersectsWith(sibling.hitBox)) { array.push(sibling); } return array; }, []); } } |