• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

excaliburjs / Excalibur / 14804036802

02 May 2025 09:58PM UTC coverage: 5.927% (-83.4%) from 89.28%
14804036802

Pull #3404

github

web-flow
Merge 5c103d7f8 into 0f2ccaeb2
Pull Request #3404: feat: added Graph module to Math

234 of 8383 branches covered (2.79%)

229 of 246 new or added lines in 1 file covered. (93.09%)

13145 existing lines in 208 files now uncovered.

934 of 15759 relevant lines covered (5.93%)

4.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/engine/EntityComponentSystem/Component.ts
1
import { Entity } from './Entity';
2

3
/**
4
 * Component Constructor Types
5
 */
6
export declare type ComponentCtor<TComponent extends Component = Component> = new (...args: any[]) => TComponent;
7

8
/**
9
 *
10
 */
11
export function isComponentCtor(value: any): value is ComponentCtor<Component> {
UNCOV
12
  return !!value && !!value.prototype && !!value.prototype.constructor;
×
13
}
14

15
/**
16
 * Type guard to check if a component implements clone
17
 * @param x
18
 */
19
function hasClone(x: any): x is { clone(): any } {
UNCOV
20
  return !!x?.clone;
×
21
}
22

23
/**
24
 * Components are containers for state in Excalibur, the are meant to convey capabilities that an Entity possesses
25
 *
26
 * Implementations of Component must have a zero-arg constructor to support dependencies
27
 *
28
 * ```typescript
29
 * class MyComponent extends ex.Component {
30
 *   // zero arg support required if you want to use component dependencies
31
 *   constructor(public optionalPos?: ex.Vector) {}
32
 * }
33
 * ```
34
 */
35
export abstract class Component {
36
  // TODO maybe generate a unique id?
37

38
  /**
39
   * Optionally list any component types this component depends on
40
   * If the owner entity does not have these components, new components will be added to the entity
41
   *
42
   * Only components with zero-arg constructors are supported as automatic component dependencies
43
   */
44
  readonly dependencies?: ComponentCtor[];
45

46
  /**
47
   * Current owning {@apilink Entity}, if any, of this component. Null if not added to any {@apilink Entity}
48
   */
UNCOV
49
  owner?: Entity = undefined;
×
50

51
  /**
52
   * Clones any properties on this component, if that property value has a `clone()` method it will be called
53
   */
54
  clone(): Component {
UNCOV
55
    const newComponent = new (this.constructor as any)();
×
UNCOV
56
    for (const prop in this) {
×
UNCOV
57
      if (this.hasOwnProperty(prop)) {
×
UNCOV
58
        const val = this[prop];
×
UNCOV
59
        if (hasClone(val) && prop !== 'owner' && prop !== 'clone') {
×
UNCOV
60
          newComponent[prop] = val.clone();
×
61
        } else {
UNCOV
62
          newComponent[prop] = val;
×
63
        }
64
      }
65
    }
UNCOV
66
    return newComponent;
×
67
  }
68

69
  /**
70
   * Optional callback called when a component is added to an entity
71
   */
72
  onAdd?(owner: Entity): void;
73

74
  /**
75
   * Optional callback called when a component is removed from an entity
76
   */
77
  onRemove?(previousOwner: Entity): void;
78
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc