1
1
import ComponentRegistry from "../../glhf-ecs/src/ComponentRegistry" ;
2
- import Entity from "../../glhf-ecs/src/Entity" ;
3
2
import World from "../../glhf-ecs/src/World" ;
4
3
import Body from "../../glhf-component/src/Body" ;
5
4
import Position from "../../glhf-component/src/Position" ;
@@ -15,12 +14,13 @@ import {default as KeyboardInput, InputActions} from "../../glhf-input/src/Keybo
15
14
import PlayerKeyboardSystem from "./system/PlayerKeyboardSystem" ;
16
15
import RenderSystem from "./system/RenderSystem" ;
17
16
import PreRenderSystem from "./system/PreRenderSystem" ;
18
- import { PlayerState } from "./component/PlayerState" ;
19
17
import IsIdle from "./component/IsIdle" ;
20
18
import IsWalking from "./component/IsWalking" ;
21
19
import IdleSystem from "./system/IdleSystem" ;
22
20
import WalkingSystem from "./system/WalkingSystem" ;
23
- import StateSystem from "./system/StateSystem" ;
21
+ import CurrentState from "./component/CurrentState" ;
22
+ import IsAttackingWithClub from "./component/IsAttackingWithClub" ;
23
+ import AttackingWithClubSystem from "./system/AttackingWithClubSystem" ;
24
24
25
25
// 0. Create the UI and canvas.
26
26
const $wrapper = createWrapperElement ( 'game-wrapper' , 640 , 480 ) ;
@@ -45,24 +45,24 @@ input.bindKey('w', InputActions.MOVE_UP);
45
45
input . bindKey ( 's' , InputActions . MOVE_DOWN ) ;
46
46
input . bindKey ( 'a' , InputActions . MOVE_LEFT ) ;
47
47
input . bindKey ( 'd' , InputActions . MOVE_RIGHT ) ;
48
- input . bindKey ( 'q ' , InputActions . ACTION_1 ) ;
48
+ input . bindKey ( ' ' , InputActions . ACTION_1 ) ;
49
49
input . listen ( ) ;
50
50
51
- // Register all "Components".
52
- const reg = ComponentRegistry . getInstance ( ) ;
53
- reg . registerComponent ( Body ) ;
54
- reg . registerComponent ( Position ) ;
55
- reg . registerComponent ( Direction ) ;
56
- reg . registerComponent ( Keyboard ) ;
57
- reg . registerComponent ( Renderable ) ;
58
- reg . registerComponent ( SpriteSheet ) ;
59
- reg . registerComponent ( IsIdle ) ;
60
- reg . registerComponent ( IsWalking ) ;
61
- reg . registerComponent ( State ) ;
62
-
63
51
// Create the current "World" (scene).
64
52
const world = new World ( ) ;
65
53
54
+ // Register all "Components".
55
+ world . registerComponent ( Body )
56
+ . registerComponent ( Position )
57
+ . registerComponent ( Direction )
58
+ . registerComponent ( Keyboard )
59
+ . registerComponent ( Renderable )
60
+ . registerComponent ( SpriteSheet )
61
+ . registerComponent ( IsIdle )
62
+ . registerComponent ( IsWalking )
63
+ . registerComponent ( IsAttackingWithClub )
64
+ . registerComponent ( CurrentState ) ;
65
+
66
66
world . createEntity ( "dino" )
67
67
. addComponent ( Body , { width : 10 , height : 20 } ) ;
68
68
@@ -72,12 +72,6 @@ world.createEntity("player")
72
72
. addComponent ( Direction , { x : Directions . NONE , y : Directions . NONE } )
73
73
. addComponent ( Keyboard , { up : "w" , down : "s" , left : "a" , right : "d" } )
74
74
. addComponent ( Renderable )
75
- . addComponent ( IsIdle , {
76
- state : 'idle' ,
77
- animationState : 'idle_up' ,
78
- stateTick : 0 ,
79
- animationTick : 0
80
- } )
81
75
. addComponent ( SpriteSheet , {
82
76
name : 'kil' ,
83
77
offset_x : 128 ,
@@ -87,36 +81,34 @@ world.createEntity("player")
87
81
animations : new Map ( ) ,
88
82
animationCurrentFrame : '' ,
89
83
animationDefaultFrame : '' ,
90
- } ) . addComponent ( State , {
91
- state : PlayerState . idle ,
92
- stateTick : 0 ,
93
- animationState : kilDefaultAnimationFrameName ,
94
- animationTick : 0
95
- } ) ;
96
-
97
-
98
- world . createQuery ( "renderable" , { all : [ Renderable , SpriteSheet , Position ] } ) ;
99
- world . createQuery ( "keyboard" , { all : [ Keyboard ] } ) ;
100
- world . createQuery ( "idle" , { all : [ IsIdle ] } ) ;
101
- world . createQuery ( "walking" , { all : [ IsWalking ] } ) ;
102
-
103
-
104
- // Pre-loop system run.
105
- const preRenderSystem = new PreRenderSystem ( world . getQuery ( "renderable" ) ) ;
106
- preRenderSystem . update ( 0 ) ;
107
- const inputSystem = new PlayerKeyboardSystem ( world . getQuery ( "keyboard" ) , input ) ;
108
- const idleSystem = new IdleSystem ( world . getQuery ( "idle" ) ) ;
109
- const walkingSystem = new WalkingSystem ( world . getQuery ( "walking" ) ) ;
110
- const renderSystem = new RenderSystem ( world . getQuery ( "renderable" ) , $foreground ) ;
111
- const stateSystem = new StateSystem ( world . getQuery ( "renderable" ) ) ; // @todo : Make this work on all entities.
112
-
84
+ } ) . addComponent ( IsIdle , {
85
+ animationStateName : kilDefaultAnimationFrameName ,
86
+ } ) . addComponent ( CurrentState , { stateName : 'IsIdle' } )
87
+
88
+ world . createQuery ( "keyboard-query" , { all : [ Keyboard ] } ) ;
89
+ world . createQuery ( "idle-query" , { all : [ IsIdle ] } ) ;
90
+ world . createQuery ( "walking-query" , { all : [ IsWalking ] } ) ;
91
+ world . createQuery ( "attacking-with-club-query" , { all : [ IsAttackingWithClub ] } ) ;
92
+ world . createQuery ( "renderable-query" , { all : [ Renderable , SpriteSheet , Position ] } ) ;
93
+
94
+ world . registerSystem ( "pre-render-system" , PreRenderSystem )
95
+ . registerSystem ( "input-system" , PlayerKeyboardSystem )
96
+ . registerSystem ( "idle-system" , IdleSystem )
97
+ . registerSystem ( "walking-system" , WalkingSystem )
98
+ . registerSystem ( "attacking-with-club-system" , AttackingWithClubSystem )
99
+ . registerSystem ( "render-system" , RenderSystem ) ;
100
+
101
+ world . createSystem ( "pre-render-system" , "renderable-query" )
102
+ . createSystem ( "input-system" , "keyboard-query" , input )
103
+ . createSystem ( "idle-system" , "idle-query" )
104
+ . createSystem ( "walking-system" , "walking-query" )
105
+ . createSystem ( "attacking-with-club-system" , "attacking-with-club-query" )
106
+ . createSystem ( "render-system" , "renderable-query" , $foreground )
107
+
108
+ world . getSystem ( "pre-render-system" ) . update ( 0 ) ;
113
109
114
110
const loop = ( now : DOMHighResTimeStamp ) => {
115
- inputSystem . update ( now ) ;
116
- idleSystem . update ( now ) ;
117
- walkingSystem . update ( now ) ;
118
- stateSystem . update ( now ) ;
119
- renderSystem . update ( now ) ;
111
+ world . systems . forEach ( system => system . update ( now ) ) ;
120
112
121
113
window . requestAnimationFrame ( loop ) ;
122
114
} ;
@@ -125,8 +117,8 @@ window.requestAnimationFrame(loop);
125
117
126
118
127
119
// @ts -ignore
128
- // window['engine'] = {
129
- // 'q': q
130
- // };
120
+ window [ 'engine' ] = {
121
+ world
122
+ } ;
131
123
132
124
0 commit comments