Skip to content

Commit a06eb42

Browse files
committed
fix(sdk): reworking the groups to fix 4164
1 parent 916fc5a commit a06eb42

File tree

7 files changed

+95
-81
lines changed

7 files changed

+95
-81
lines changed

engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/BufferedGroup.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* This reduces artifacts caused by rounding errors at the cost of lower performance.
88
* </p>
99
*/
10-
public class BufferedGroup extends Group {
10+
public class BufferedGroup extends ContainerBasedEntity<BufferedGroup> {
1111
@Override
1212
Entity.Type getType() {
1313
return Entity.Type.BUFFERED_GROUP;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.codingame.gameengine.module.entities;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.Stream;
7+
8+
/**
9+
*
10+
*
11+
* @param <T> a subclass inheriting Entity, used in order to return <b>this</b> as a T instead of a <code>ContainerBasedEntity</code>.
12+
*/
13+
public abstract class ContainerBasedEntity<T extends Entity<?>> extends Entity<T> {
14+
15+
private Set<Entity<?>> entities;
16+
17+
ContainerBasedEntity() {
18+
super();
19+
20+
entities = new HashSet<>();
21+
}
22+
/**
23+
* Separates the given entity from this <code>ContainerBasedEntity</code>.
24+
*
25+
* @param entity
26+
* the <code>Entity</code> to be removed from this ContainerBasedEntity, if it is part of it.
27+
*/
28+
public void remove(Entity<?> entity) {
29+
if (entity.parent == this) {
30+
entity.parent = null;
31+
entities.remove(entity);
32+
set("children", asString(entities), null);
33+
}
34+
}
35+
36+
/**
37+
* Adds the given <code>Entity</code> instances to this <code>ContainerBasedEntity</code>.
38+
* <p>
39+
* The entities will be displayed within a container controlled by this <code>ContainerBasedEntity</code>.
40+
*
41+
* @param entities
42+
* the <code>Entity</code> instances to be added to this ContainerBasedEntity.
43+
* @exception IllegalArgumentException
44+
* if at least one given <code>Entity</code> is already in a <code>ContainerBasedEntity</code>.
45+
*/
46+
public void add(Entity<?>... entities) {
47+
Stream.of(entities).forEach(entity -> {
48+
if (entity.parent != null) {
49+
throw new IllegalArgumentException();
50+
}
51+
this.entities.add(entity);
52+
});
53+
54+
set("children", asString(this.entities), null);
55+
}
56+
57+
private String asString(Set<Entity<?>> entities) {
58+
return entities.stream()
59+
.map(e -> String.valueOf(e.getId()))
60+
.collect(Collectors.joining(","));
61+
}
62+
63+
}

engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/Entity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public abstract class Entity<T extends Entity<?>> {
1919
private double scaleX = 1, scaleY = 1;
2020
private boolean visible = true;
2121
private double rotation, alpha = 1;
22-
Group parent;
22+
ContainerBasedEntity parent;
2323
Mask mask;
2424

2525
static enum Type {

engine/modules/entities/src/main/java/com/codingame/gameengine/module/entities/Group.java

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,12 @@
11
package com.codingame.gameengine.module.entities;
22

3-
import java.util.HashSet;
4-
import java.util.Set;
5-
import java.util.stream.Collectors;
6-
import java.util.stream.Stream;
7-
83
/**
94
* A Group is an Entity which acts as a container for other entities.
105
*/
11-
public class Group extends Entity<Group> {
12-
13-
private Set<Entity<?>> entities;
6+
public class Group extends ContainerBasedEntity<Group> {
147

158
Group() {
169
super();
17-
entities = new HashSet<>();
18-
}
19-
20-
/**
21-
* Separates the given entity from this <code>Group</code>.
22-
*
23-
* @param entity
24-
* the <code>Entity</code> to be removed from this group, if it is part of it.
25-
*/
26-
public void remove(Entity<?> entity) {
27-
if (entity.parent == this) {
28-
entity.parent = null;
29-
entities.remove(entity);
30-
set("children", asString(entities), null);
31-
}
32-
}
33-
34-
/**
35-
* Adds the given <code>Entity</code> instances to this <code>Group</code>.
36-
* <p>
37-
* The entities will be displayed within a container controlled by this <code>Group</code>.
38-
*
39-
* @param entities
40-
* the <code>Entity</code> instances to be added to this group.
41-
* @exception IllegalArgumentException
42-
* if at least one given <code>Entity</code> is already in a <code>Group</code>.
43-
*/
44-
public void add(Entity<?>... entities) {
45-
Stream.of(entities).forEach(entity -> {
46-
if (entity.parent != null) {
47-
throw new IllegalArgumentException();
48-
}
49-
this.entities.add(entity);
50-
});
51-
52-
set("children", asString(this.entities), null);
53-
}
54-
55-
private String asString(Set<Entity<?>> entities) {
56-
return entities.stream()
57-
.map(e -> String.valueOf(e.getId()))
58-
.collect(Collectors.joining(","));
5910
}
6011

6112
@Override

engine/modules/entities/src/main/resources/view/entity-module/BufferedGroup.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Group } from './Group.js'
21
import { getRenderer, flagForDestructionOnReinit } from '../core/rendering.js'
32
import { WIDTH, HEIGHT } from '../core/constants.js'
3+
import { ContainerBasedEntity } from './ContainerBasedEntity.js'
44

55
/* global PIXI */
66

7-
export class BufferedGroup extends Group {
7+
export class BufferedGroup extends ContainerBasedEntity {
88
initDisplay () {
99
super.initDisplay()
1010
this.gameTexture = PIXI.RenderTexture.create(WIDTH, HEIGHT)
@@ -14,10 +14,6 @@ export class BufferedGroup extends Group {
1414
this.needsRender = true
1515
}
1616

17-
updateDisplay (state, changed, globalData) {
18-
super.updateDisplay(state, changed, globalData)
19-
}
20-
2117
postUpdate () {
2218
if (this.needsRender) {
2319
getRenderer().render(this.buffer, this.gameTexture)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Entity } from './Entity.js'
2+
3+
/* global PIXI */
4+
5+
export class ContainerBasedEntity extends Entity {
6+
constructor () {
7+
super()
8+
Object.assign(this.defaultState, {
9+
children: []
10+
})
11+
}
12+
13+
initDisplay () {
14+
super.initDisplay()
15+
this.graphics = new PIXI.Container()
16+
}
17+
18+
updateDisplay (state, changed, globalData) {
19+
super.updateDisplay(state, changed, globalData)
20+
}
21+
22+
get childrenContainer () {
23+
return this.graphics
24+
}
25+
}
Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,4 @@
1-
import { Entity } from './Entity.js'
1+
import { ContainerBasedEntity } from './ContainerBasedEntity.js'
22

3-
/* global PIXI */
4-
5-
export class Group extends Entity {
6-
constructor () {
7-
super()
8-
Object.assign(this.defaultState, {
9-
children: []
10-
})
11-
}
12-
13-
initDisplay () {
14-
super.initDisplay()
15-
this.graphics = new PIXI.Container()
16-
}
17-
18-
updateDisplay (state, changed, globalData) {
19-
super.updateDisplay(state, changed, globalData)
20-
}
21-
22-
get childrenContainer () {
23-
return this.graphics
24-
}
3+
export class Group extends ContainerBasedEntity {
254
}

0 commit comments

Comments
 (0)