-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathSceneController.ts
More file actions
133 lines (110 loc) · 4.49 KB
/
SceneController.ts
File metadata and controls
133 lines (110 loc) · 4.49 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
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
import {PlacementMode, PlacementSettings} from "SurfacePlacement.lspkg/Scripts/PlacementSettings"
import {GameController} from "GameController.lspkg/GameController"
import {ButtonStateKey} from "GameController.lspkg/Scripts/ButtonState"
import {CharacterController} from "SpecsCharacterController.lspkg/Character Controller/Character Controller"
import {SurfacePlacementController} from "SurfacePlacement.lspkg/Scripts/SurfacePlacementController"
import {AnimationController} from "./AnimationController"
@component
export class SceneController extends BaseScriptComponent {
@input
@allowUndefined
objectVisuals: SceneObject
@input characterController: CharacterController
@input animationController: AnimationController
@input cameraObj: SceneObject
@input("int")
@widget(new ComboBoxWidget([new ComboBoxItem("Near Surface", 0), new ComboBoxItem("Horizontal", 1)]))
placementSettingMode: number = 0
private transform: Transform = null
private camTrans: Transform = null
private surfacePlacement: SurfacePlacementController = SurfacePlacementController.getInstance()
private gameController: GameController = GameController.getInstance()
onAwake() {
this.camTrans = this.cameraObj.getTransform()
this.transform = this.getSceneObject().getTransform()
this.createEvent("OnStartEvent").bind(this.onStart.bind(this))
this.objectVisuals.enabled = false
//HACK: EDITOR TEST:
this.createEvent("TapEvent").bind(() => {
this.JumpButtonDown(true)
//this.KickButtonDown(true);
//this.PunchButtonDown(true);
})
}
private onStart() {
this.startPlacement()
this.createEvent("UpdateEvent").bind(this.onUpdate.bind(this))
this.gameController.scanForControllers()
//register button presses
this.gameController.onButtonStateChanged(ButtonStateKey.a, this.JumpButtonDown.bind(this))
this.gameController.onButtonStateChanged(ButtonStateKey.x, this.PunchButtonDown.bind(this))
this.gameController.onButtonStateChanged(ButtonStateKey.b, this.KickButtonDown.bind(this))
this.gameController.onButtonStateChanged(ButtonStateKey.y, this.sendRumble.bind(this))
}
private sendRumble(pressed: boolean) {
if (pressed) {
this.gameController.sendRumble(20, 10)
}
}
private JumpButtonDown(pressed: boolean) {
if (pressed) {
this.animationController.playJumpAnimation()
}
}
private PunchButtonDown(pressed: boolean) {
if (pressed) {
this.animationController.playPunchAnimation()
}
}
private KickButtonDown(pressed: boolean) {
if (pressed) {
this.animationController.playKickAnimation()
}
}
startPlacement() {
this.objectVisuals.enabled = false
let placementSettings = new PlacementSettings(PlacementMode.HORIZONTAL)
if (this.placementSettingMode == 0) {
placementSettings = new PlacementSettings(
PlacementMode.NEAR_SURFACE,
true, // use surface adjustment widget
vec3.zero(), // offset in cm of widget from surface center
this.onSliderUpdated.bind(this) // callback from widget height changes
)
}
this.surfacePlacement.startSurfacePlacement(placementSettings, (pos, rot) => {
this.onSurfaceDetected(pos, rot)
})
}
resetPlacement() {
this.surfacePlacement.stopSurfacePlacement()
this.startPlacement()
}
private onSliderUpdated(pos: vec3) {
this.transform.setWorldPosition(pos)
}
private onSurfaceDetected(pos: vec3, rot: quat) {
this.objectVisuals.enabled = true
this.transform.setWorldPosition(pos)
this.transform.setWorldRotation(rot)
this.characterController.setPosition(pos)
this.characterController.setInputType(global.deviceInfoSystem.isEditor() ? 1 : 0)
}
private onUpdate() {
const buttonState = this.gameController.getButtonState()
if (!buttonState) {
return
}
//set button states in update instead of on value value changed since vertical and horizontal would come in at different times
let moveSpeed = new vec2(Math.abs(buttonState.lx), Math.abs(buttonState.ly)).distance(vec2.zero()) //0 - 1
const joystickMoveDirection = new vec3(buttonState.lx, 0, buttonState.ly).normalize()
// Convert joystick input into world space relative to camera’s facing direction
let moveDir = this.camTrans.getWorldTransform().multiplyDirection(joystickMoveDirection).normalize()
if (moveSpeed < 0.15) {
moveSpeed = 0
moveDir = vec3.zero()
}
this.characterController.move(moveDir)
this.characterController.setTargetSpeedModifier(moveSpeed)
}
}