11import { getExnFromStrictNull } from "commonlib-ts/src/NullableUtils" ;
22import { state as flyComponentState } from "./FlyComponentStateType"
3- import { getPositionComponentExn , getVelocityComponentExn } from "../gameObject/GameObject" ;
3+ import * as GameObject from "../gameObject/GameObject" ;
44import { getGameObjectStateExn , setPositionComponentState } from "../utils/WorldUtils" ;
55import { state as worldState } from "../world/WorldStateType" ;
6- import { getVelocity } from "./VelocityComponent" ;
7- import { getPosition , setPosition } from "./PositionComponent" ;
6+ import * as VelocityComponent from "./VelocityComponent" ;
7+ import * as PositionComponent from "./PositionComponent" ;
88
99export let create = ( ) : flyComponentState => {
1010 let flyComponentState : flyComponentState = {
@@ -27,20 +27,21 @@ export let setMaxVelocity = (flyComponentState: flyComponentState, maxVelocity)
2727}
2828
2929export let fly = ( worldState : worldState , flyComponentState : flyComponentState ) : worldState => {
30- let maxVelocity = flyComponentState . maxVelocity
31-
30+ //获得该组件的maxVelocity、gameObject
31+ let maxVelocity = getMaxVelocity ( flyComponentState )
3232 let gameObject = getExnFromStrictNull ( flyComponentState . gameObject )
33- let gameObjectState = getGameObjectStateExn ( worldState , gameObject )
3433
35- //通过gameObject获得positionComponent,获得它的position
36- let [ x , y , z ] = getPosition ( getPositionComponentExn ( gameObjectState ) )
34+ //通过该组件的gameObject,获得挂载到该gameObject的positionComponent组件
35+ //获得它的position
36+ let [ x , y , z ] = PositionComponent . getPosition ( GameObject . getPositionComponentExn ( getGameObjectStateExn ( worldState , gameObject ) ) )
3737
38- //通过gameObject获得velocityComponent,获得它的velocity
39- let velocity = getVelocity ( getVelocityComponentExn ( gameObjectState ) )
38+ //通过该组件的gameObject,获得挂载到该gameObject的velocityComponent组件
39+ //获得它的velocity
40+ let velocity = VelocityComponent . getVelocity ( GameObject . getVelocityComponentExn ( getGameObjectStateExn ( worldState , gameObject ) ) )
4041
42+ //根据maxVelocity、velocity,更新positionComponent组件的position
4143 velocity = velocity < maxVelocity ? ( velocity * 2.0 ) : maxVelocity
42-
43- let positionComponentState = setPosition ( getPositionComponentExn ( gameObjectState ) , [ x + velocity , y + velocity , z + velocity ] )
44+ let positionComponentState = PositionComponent . setPosition ( GameObject . getPositionComponentExn ( getGameObjectStateExn ( worldState , gameObject ) ) , [ x + velocity , y + velocity , z + velocity ] )
4445
4546 return setPositionComponentState ( worldState , gameObject , positionComponentState )
4647}
0 commit comments