-
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathscreen-element.ts
More file actions
51 lines (44 loc) · 1.63 KB
/
Copy pathscreen-element.ts
File metadata and controls
51 lines (44 loc) · 1.63 KB
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
import { Vector, vec } from './math/vector';
import type { Engine } from './engine';
import type { ActorOptions } from './actor';
import { Actor } from './actor';
import { TransformComponent } from './entity-component-system/components/transform-component';
import { CollisionType } from './collision/collision-type';
import { CoordPlane } from './math/coord-plane';
/**
* Type guard to detect a screen element
*/
export function isScreenElement(actor: Actor) {
return actor instanceof ScreenElement;
}
/**
* Helper {@apilink Actor} primitive for drawing UI's, optimized for UI drawing. Does
* not participate in collisions. Drawn on top of all other actors.
*/
export class ScreenElement extends Actor {
protected _engine: Engine;
constructor();
constructor(config?: ActorOptions);
constructor(config?: ActorOptions) {
super({ ...config });
this.get(TransformComponent).coordPlane = CoordPlane.Screen;
this.anchor = config?.anchor ?? vec(0, 0);
this.body.collisionType = config?.collisionType ?? CollisionType.PreventCollision;
this.pointer.useGraphicsBounds = true;
this.pointer.useColliderShape = false;
if (!config?.collider && config?.width > 0 && config?.height > 0) {
this.collider.useBoxCollider(config.width, config.height, this.anchor);
}
}
public _initialize(engine: Engine) {
this._engine = engine;
super._initialize(engine);
}
public contains(x: number, y: number, useWorld: boolean = true) {
if (useWorld) {
return super.contains(x, y);
}
const coords = this._engine.worldToScreenCoordinates(new Vector(x, y));
return super.contains(coords.x, coords.y);
}
}