Skip to content

Commit 16fe1b4

Browse files
author
Builder
committed
Merge branch 'polygons' into 'master'
feat(sdk): New Shape: Polygons See merge request codingame/game-engine!189
2 parents 574a7da + 1ab3beb commit 16fe1b4

File tree

12 files changed

+151
-9
lines changed

12 files changed

+151
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.factorypath
12
target/
23
.settings
34
.classpath

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
@@ -23,7 +23,7 @@ public abstract class Entity<T extends Entity<?>> {
2323
Mask mask;
2424

2525
static enum Type {
26-
CIRCLE, LINE, RECTANGLE, SPRITE, TEXT, BITMAPTEXT, GROUP, BUFFERED_GROUP, SPRITEANIMATION, ROUNDED_RECTANGLE
26+
CIRCLE, LINE, RECTANGLE, SPRITE, TEXT, BITMAPTEXT, GROUP, BUFFERED_GROUP, SPRITEANIMATION, ROUNDED_RECTANGLE, POLYGON
2727
}
2828

2929
Entity() {

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
@Singleton
2727
public class GraphicEntityModule implements Module {
2828

29-
//JAVA
30-
//TODO: masks
3129
//TODO: extra properties for Texts (text wrapping, alignement, ...)
3230

3331
static int ENTITY_COUNT = 0;
@@ -276,6 +274,17 @@ public RoundedRectangle createRoundedRectangle() {
276274
newEntity(c);
277275
return c;
278276
}
277+
278+
/**
279+
* Creates a new Polygon entity, its graphical counterpart will be created on the frame currently being computed.
280+
*
281+
* @return the entity. Modify its properties to animate the graphical counterpart.
282+
*/
283+
public Polygon createPolygon() {
284+
Polygon c = new Polygon();
285+
newEntity(c);
286+
return c;
287+
}
279288

280289
/**
281290
* Creates a new Text entity, its graphical counterpart will be created on the frame currently being computed.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.codingame.gameengine.module.entities;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
7+
/**
8+
* <p>
9+
* A Polygon specifies an area in a the <code>world</code> defined by a sequence of points.
10+
* </p>
11+
* The coordinates of each point are in world units.
12+
*/
13+
public class Polygon extends Shape<Polygon> {
14+
15+
private static class Point {
16+
int x, y;
17+
18+
public Point(int x, int y) {
19+
this.x = x;
20+
this.y = y;
21+
}
22+
23+
}
24+
25+
private List<Point> points = new ArrayList<>();
26+
27+
Polygon() {
28+
super();
29+
}
30+
31+
/**
32+
* Adds a point to the path of this <code>Polygon</code>.
33+
*
34+
* @param x
35+
* the x coordinate in world units
36+
* @param y
37+
* the x coordinate in world units
38+
* @return this <code>Polygon</code>
39+
*/
40+
public Polygon addPoint(int x, int y) {
41+
points.add(new Point(x, y));
42+
set("points", asString(points));
43+
return this;
44+
}
45+
46+
/**
47+
* Clears the path of this <code>Polygon</code>.
48+
*
49+
* @return this <code>Polygon</code>
50+
*/
51+
public Polygon clearPoints() {
52+
points.clear();
53+
set("points", asString(points));
54+
return this;
55+
}
56+
57+
/**
58+
* Sets the transition used to animate the positions of each point. Only used if the path has the same number of points.
59+
* @param curve
60+
* the transition to animate between coordinates of each point in the path.
61+
* @return this <code>Polygon</code>
62+
*/
63+
public Polygon setPointsInterpolationCurve(Curve curve) {
64+
set("points", asString(points), curve);
65+
return this;
66+
}
67+
68+
private String asString(List<Point> points) {
69+
return points.stream().map(p -> p.x + "," + p.y).collect(Collectors.joining(","));
70+
}
71+
72+
@Override
73+
Entity.Type getType() {
74+
return Entity.Type.POLYGON;
75+
}
76+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Serializer {
6565
keys.put("duration", "d");
6666
keys.put("baseWidth", "bw");
6767
keys.put("baseHeight", "bh");
68+
keys.put("points", "ps");
6869

6970
commands = new HashMap<>();
7071
commands.put("CREATE", "C");
@@ -96,6 +97,7 @@ class Serializer {
9697
types.put(Type.TEXT, "T");
9798
types.put(Type.BITMAPTEXT, "X");
9899
types.put(Type.SPRITEANIMATION, "A");
100+
types.put(Type.POLYGON, "P");
99101

100102
if (keys.values().stream().distinct().count() != keys.values().size()) {
101103
throw new RuntimeException("Duplicate keys");

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const PROPERTY_KEY_MAP = {
4040
l: 'loop',
4141
d: 'duration',
4242
bw: 'baseWidth',
43-
bh: 'baseHeight'
43+
bh: 'baseHeight',
44+
ps: 'points'
4445
}
4546

4647
export class CreateCommand {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Group } from './Group.js'
88
import { BufferedGroup } from './BufferedGroup.js'
99
import { SpriteAnimation } from './SpriteAnimation.js'
1010
import { RoundedRectangle } from './RoundedRectangle.js';
11+
import { Polygon } from './Polygon.js';
1112

1213
export class EntityFactory {
1314
static create (type) {
@@ -21,7 +22,8 @@ export class EntityFactory {
2122
G: Group,
2223
B: BufferedGroup,
2324
A: SpriteAnimation,
24-
K: RoundedRectangle
25+
K: RoundedRectangle,
26+
P: Polygon
2527
}[type]
2628
if (!EntityClass) {
2729
throw new Error('Exception: entity type not found: ' + type)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Shape } from './Shape.js'
2+
3+
export class Polygon extends Shape {
4+
constructor () {
5+
super()
6+
Object.assign(this.defaultState, {
7+
points: []
8+
})
9+
}
10+
11+
initDisplay () {
12+
super.initDisplay()
13+
this.graphics.endFill()
14+
}
15+
16+
updateDisplay (state, changed, globalData) {
17+
super.updateDisplay(state, changed, globalData)
18+
if (changed.lineWidth ||
19+
changed.lineColor ||
20+
changed.lineAlpha ||
21+
changed.fillColor ||
22+
changed.points) {
23+
this.graphics.clear()
24+
if (state.fillColor !== null) {
25+
this.graphics.beginFill(state.fillColor, state.fillAlpha)
26+
}
27+
28+
this.graphics.lineStyle(globalData.atLeastOnePixel(state.lineWidth), state.lineColor, state.lineAlpha)
29+
this.graphics.drawPolygon(state.points.map(coord => coord * globalData.toWorldUnits))
30+
if (state.fillColor !== null) {
31+
this.graphics.endFill()
32+
}
33+
}
34+
}
35+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export class Rectangle extends Shape {
2525
changed.width ||
2626
changed.height ||
2727
changed.lineAlpha ||
28-
changed.fillColor ||
2928
changed.fillColor) {
3029
this.graphics.clear()
3130
if (state.fillColor !== null) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export class RoundedRectangle extends Shape {
3737
}
3838

3939
this.graphics.lineStyle(globalData.atLeastOnePixel(state.lineWidth), state.lineColor, state.lineAlpha)
40-
this.graphics.drawRoundedRect(0, 0,
41-
state.width * globalData.toWorldUnits,
40+
this.graphics.drawRoundedRect(0, 0,
41+
state.width * globalData.toWorldUnits,
4242
state.height * globalData.toWorldUnits,
4343
state.radius * globalData.toWorldUnits)
4444
if (state.fillColor !== null) {

0 commit comments

Comments
 (0)