@@ -12,31 +12,19 @@ public static void main(String[] args) {
1212 world .component (Position .class );
1313 world .component (Velocity .class );
1414 world .component (Health .class );
15+
16+ long tagEnemy = world .entity ("Enemy" );;
1517
1618 // Create entities
1719 for (int i = 0 ; i < 3 ; i ++) {
20+ long uniqueTag = world .entity ("UniqueTag_" + i );
1821 world .obtainEntity (world .entity ("Entity_" + i ))
1922 .set (new Position (i * 10.0f , i * 5.0f ))
2023 .set (new Velocity (1.0f + i , 0.5f + i ))
21- .set (new Health (100 - i * 10 ));
24+ .set (new Health (100 - i * 10 )). add ( tagEnemy ). add ( uniqueTag ) ;
2225 }
2326
24- try (Query query = world .query ()
25- .with (Position .class )
26- .with (Velocity .class )
27- .with (Health .class )
28- .build ()) {
29-
30- System .out .println ("Traditional access (with object allocation):" );
31- query .iter (iter -> {
32- Field <Position > positions = iter .field (Position .class , 0 );
33- Field <Health > healths = iter .field (Health .class , 2 );
34- for (int i = 0 ; i < iter .count (); i ++) {
35- Position pos = positions .get (i );
36- Health health = healths .get (i );
37- System .out .printf (" pos=(%.1f, %.1f) health=%d%n" , pos .x (), pos .y (), health .value ());
38- }
39- });
27+ try (Query query = world .query ().with (Position .class ).with (Velocity .class ).with (Health .class ).build ()) {
4028
4129 System .out .println ("\n Direct access (no allocation):" );
4230 query .iter (iter -> {
@@ -57,6 +45,17 @@ public static void main(String[] args) {
5745 }
5846 });
5947 }
48+
49+
50+ try (Query query = world .query ().with (tagEnemy ).with (Health .class ).build ()) {
51+ System .out .println ("\n Direct access with tag:" );
52+ query .iter (iter -> {
53+ for (int i = 0 ; i < iter .count (); i ++) {
54+ int health = iter .fieldInt (Health .class , 1 , "value" , i );
55+ System .out .println ("health=" + health );
56+ }
57+ });
58+ }
6059 }
6160 }
6261}
0 commit comments