-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToggleGatesOnGameStateChange.ts
75 lines (60 loc) · 2.19 KB
/
ToggleGatesOnGameStateChange.ts
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
/**
* Works with gamestate to specifically control the gates to the start of a Match
*/
import { GameState } from 'GameUtils';
import { Events } from "Events";
import * as hz from 'horizon/core';
class ToggleGatesOnGameStateChange extends hz.Component<typeof ToggleGatesOnGameStateChange> {
static propsDefinition = {
//in the future, when there is an entity array, we can use that, but for now,
//we hard code all the barriers access and use the parent objects if needed
enterGate1: { type: hz.PropTypes.Entity },
enterGate2: { type: hz.PropTypes.Entity },
exitGate1: { type: hz.PropTypes.Entity },
exitGate2: { type: hz.PropTypes.Entity },
};
preStart() {
this.prepareStartAreaForRace();
this.connectLocalBroadcastEvent(
Events.onGameStateChanged,
(data) => {
if (data.fromState === GameState.StartingMatch && data.toState === GameState.PlayingMatch) {
this.closeStartAreaForMatch();
}
else if (data.toState === GameState.CompletedMatch || data.toState === GameState.ReadyForMatch) {
this.prepareStartAreaForRace();
}
});
}
start() { }
private closeStartAreaForMatch() {
this.setBarrierActive(true, this.props.enterGate1);
this.setBarrierActive(true, this.props.enterGate2);
this.setBarrierActive(false, this.props.exitGate1);
this.setBarrierActive(false, this.props.exitGate2);
}
private prepareStartAreaForRace() {
this.setBarrierActive(false, this.props.enterGate1);
this.setBarrierActive(false, this.props.enterGate2);
this.setBarrierActive(true, this.props.exitGate1);
this.setBarrierActive(true, this.props.exitGate2);
}
private setBarrierActive(isActivated: boolean, barrierEntity: hz.Entity | undefined) {
barrierEntity?.collidable.set(isActivated);
//we animated the barrier if it has an animation, otherwise we just show/hide it
const animEnt = barrierEntity?.as(hz.AnimatedEntity);
if (animEnt) {
if (isActivated) {
animEnt.stop();
}
else {
animEnt.play();
}
}
else {
barrierEntity?.visible.set(isActivated);
}
}
dispose() { }
}
hz.Component.register(ToggleGatesOnGameStateChange);