import { Components } from "./Components" ; import { Raytracer } from "../graphics/Raytracer" ; import { Particles } from "../graphics/particles/Particles" ; import { GamepadsInternal } from "../input/Gamepads" ; import { AnimationSystem } from "../animation/AnimationSystem" ; import { TimeInternal } from "./Time" ; import { PhysicsContext } from "../physics/PhysicsContext" ; import { RigidBody } from "../physics/RigidBody" ; import { BehaviorComponent } from "../behavior/BehaviorComponent" ; import { EntityInternal } from "./Entity" ; import { Screen } from "../ui/Screen" ; import { CollisionSystem } from "../collision/CollisionSystem" ; import { VoidSyncEvent } from "ts-events" ; import { Constructor } from "./Types" ; import { Component } from "./Component" ; import { IKSolver } from "../animation/ik/IKSolver" ; import { AnimationComponent } from "../animation/AnimationComponent" ; import { Collider } from "../collision/Collider" ; import { CharacterCollider } from "../collision/CharacterCollider" ; namespace Private { export const updateHook = new VoidSyncEvent ( ) ; } hidden export namespace UpdateInternal { function iterateComponents < T extends Component > ( ctor , components , handler ) { ( components [ ctor . name ] as T [ ] ) . forEach ( handler ) ; } export function update ( ) { GamepadsInternal . scanGamepads ( ) ; let components = Components . ofTypes ( [ PhysicsContext , BehaviorComponent , AnimationComponent ] ) ; iterateComponents ( PhysicsContext , components , context => { context . update ( ) ; context . entity . getComponents ( RigidBody ) . forEach ( rigidBody => ( rigidBody . update ( context ) ) ) ; } ) ; EntityInternal . collectEntityOperations = true ; AnimationSystem . update ( components . AnimationComponent as AnimationComponent [ ] ) ; iterateComponents ( BehaviorComponent , components , component => component . update ( ) ) ; try { Private . updateHook . post ( ) ; } catch ( e ) { console . error ( "Runtime Error" ) ; console . error ( e . toString ( ) ) ; Private . updateHook . detach ( ) ; } EntityInternal . collectEntityOperations = false ; for ( const entity of EntityInternal . entitiesJustDestroyed ) { EntityInternal . destroyEntity ( entity ) ; } EntityInternal . entitiesJustDestroyed . length = 0 ; for ( const entity of EntityInternal . entitiesJustCreated ) { const behavior = entity . getComponent ( BehaviorComponent ) ; if ( behavior && behavior . active ) { behavior . update ( ) ; } } EntityInternal . entitiesJustCreated . length = 0 ; components = Components . ofTypes ( [ IKSolver , Particles , Screen , Raytracer , Collider , CharacterCollider ] ) ; iterateComponents ( IKSolver , components , c => c . update ( ) ) ; iterateComponents ( Particles , components , c => c . update ( ) ) ; iterateComponents ( Screen , components , c => c . update ( ) ) ; iterateComponents ( Raytracer , components , c => c . update ( ) ) ; CollisionSystem . update ( components . Collider as Collider [ ] , components . CharacterCollider as CharacterCollider [ ] ) ; TimeInternal . incrementCurrentFrame ( ) ; } } export class Update { event static get hook ( ) { return Private . updateHook ; } }