11import * as THREE from "three" ;
22
3- export class Entities {
3+ class Entities {
44 constructor ( game ) {
5- this . players = [ ] ;
6-
75 this . game = game ;
86 this . mobMaterial = new THREE . MeshStandardMaterial ( {
97 color : new THREE . Color ( "red" ) ,
@@ -16,12 +14,19 @@ export class Entities {
1614 this . mobMaxCount
1715 ) ;
1816 this . mobMesh . instanceMatrix . setUsage ( THREE . DynamicDrawUsage ) ;
19-
2017 this . game . scene . add ( this . mobMesh ) ;
21- this . playerGeometry = this . game . al . get ( "player" ) ;
22- this . playerGeometry . children [ 0 ] . material . map = this . game . al . get ( "playerTex" ) ;
23-
24-
18+ this . playerMaterial = new THREE . MeshStandardMaterial ( {
19+ color : new THREE . Color ( "blue" ) ,
20+ } ) ;
21+ this . playerGeometry = new THREE . BoxGeometry ( 1 , 1 , 1 ) ;
22+ this . playerMaxCount = 200 ;
23+ this . playerMesh = new THREE . InstancedMesh (
24+ this . playerGeometry ,
25+ this . playerMaterial ,
26+ this . playerMaxCount
27+ ) ;
28+ this . playerMesh . instanceMatrix . setUsage ( THREE . DynamicDrawUsage ) ;
29+ this . game . scene . add ( this . playerMesh ) ;
2530 this . dummy = new THREE . Object3D ( ) ;
2631 }
2732
@@ -40,26 +45,27 @@ export class Entities {
4045 this . mobMesh . setMatrixAt ( num_mobs ++ , this . dummy . matrix ) ;
4146 }
4247 this . mobMesh . instanceMatrix . needsUpdate = true ;
43-
44-
45- for ( let i = 0 ; i < entities . players . length ; i ++ ) {
46- const playerData = entities . players [ i ] ;
47- if ( playerData [ 0 ] === this . game . nick )
48- continue ;
49-
50- let player = this . players [ i ] ;
51- if ( playerData [ 0 ] !== this . game . nick ) {
52- if ( ! player ) {
53- player = this . game . al . get ( "player" ) ;
54- player . children [ 0 ] . material . map = this . game . al . get ( "playerTex" ) ;
55- this . game . scene . add ( player ) ;
56- }
48+ var num_players = 0 ;
49+ for ( let i in entities . players ) {
50+ if ( entities . players [ i ] [ 0 ] !== this . game . nick ) {
51+ num_players ++ ;
5752 }
58- player . position . set (
59- playerData [ 1 ] + offset [ 0 ] ,
60- playerData [ 2 ] + offset [ 1 ] ,
61- playerData [ 3 ] + offset [ 2 ]
62- ) ;
6353 }
54+ this . playerMesh . count = num_players ;
55+ num_players = 0 ;
56+ for ( let i in entities . players ) {
57+ if ( entities . players [ i ] [ 0 ] !== this . game . nick ) {
58+ this . dummy . position . set (
59+ entities . players [ i ] [ 1 ] + offset [ 0 ] ,
60+ entities . players [ i ] [ 2 ] + offset [ 1 ] ,
61+ entities . players [ i ] [ 3 ] + offset [ 2 ]
62+ ) ;
63+ this . dummy . updateMatrix ( ) ;
64+ this . playerMesh . setMatrixAt ( num_players ++ , this . dummy . matrix ) ;
65+ }
66+ }
67+ this . playerMesh . instanceMatrix . needsUpdate = true ;
6468 }
65- } ;
69+ }
70+
71+ export { Entities } ;
0 commit comments