Skip to content

Commit 574a7da

Browse files
author
Builder
committed
Merge branch 'rounded-rect' into 'master'
feat(sdk): New Shape: RoundedRectangle See merge request codingame/game-engine!187
2 parents 6d39288 + b8186fa commit 574a7da

File tree

11 files changed

+248
-39
lines changed

11 files changed

+248
-39
lines changed

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
26+
CIRCLE, LINE, RECTANGLE, SPRITE, TEXT, BITMAPTEXT, GROUP, BUFFERED_GROUP, SPRITEANIMATION, ROUNDED_RECTANGLE
2727
}
2828

2929
Entity() {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,17 @@ public Rectangle createRectangle() {
265265
newEntity(c);
266266
return c;
267267
}
268+
269+
/**
270+
* Creates a new RoundedRectangle entity, its graphical counterpart will be created on the frame currently being computed.
271+
*
272+
* @return the entity. Modify its properties to animate the graphical counterpart.
273+
*/
274+
public RoundedRectangle createRoundedRectangle() {
275+
RoundedRectangle c = new RoundedRectangle();
276+
newEntity(c);
277+
return c;
278+
}
268279

269280
/**
270281
* Creates a new Text entity, its graphical counterpart will be created on the frame currently being computed.
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.codingame.gameengine.module.entities;
2+
3+
/**
4+
* A RoundedRectangle specifies an area in a the <code>world</code> enclosed by the Rectangle's upper-left point (x,y), its width, and its height.
5+
* <p>
6+
* The rectangle on screen will have rounded corners
7+
* </p>
8+
* The coordinates, width and height are in world units.
9+
*/
10+
public class RoundedRectangle extends Shape<RoundedRectangle> {
11+
12+
private int width = 100;
13+
private int height = 100;
14+
private int radius = 20;
15+
16+
RoundedRectangle() {
17+
super();
18+
}
19+
20+
/**
21+
* Sets the width of this <code>RoundedRectangle</code> in world units.
22+
*
23+
* @param width
24+
* the width for this <code>RoundedRectangle</code>.
25+
* @return this <code>RoundedRectangle</code>
26+
*/
27+
public RoundedRectangle setWidth(int width) {
28+
return setWidth(width, null);
29+
}
30+
31+
/**
32+
* Sets the width of this <code>RoundedRectangle</code> in world units.
33+
*
34+
* @param width
35+
* the width for this <code>RoundedRectangle</code>.
36+
* @param curve
37+
* the transition to animate between values of this property.
38+
* @return this <code>RoundedRectangle</code>
39+
*/
40+
public RoundedRectangle setWidth(int width, Curve curve) {
41+
this.width = width;
42+
set("width", width, curve);
43+
return this;
44+
}
45+
46+
/**
47+
* Returns the width of this <code>RoundedRectangle</code> in world units.
48+
* <p>
49+
* Default is 100.
50+
*
51+
* @return the width of this <code>RoundedRectangle</code>.
52+
*/
53+
public int getWidth() {
54+
return width;
55+
}
56+
57+
/**
58+
* Sets the height of this <code>RoundedRectangle</code> in world units.
59+
*
60+
* @param height
61+
* the height for this <code>RoundedRectangle</code>.
62+
* @return this <code>RoundedRectangle</code>
63+
*/
64+
public RoundedRectangle setHeight(int height) {
65+
return setHeight(height, null);
66+
}
67+
68+
/**
69+
* Sets the height of this <code>RoundedRectangle</code> in world units.
70+
*
71+
* @param height
72+
* the height for this <code>RoundedRectangle</code>.
73+
* @param curve
74+
* the transition to animate between values of this property.
75+
* @return this <code>RoundedRectangle</code>
76+
*/
77+
public RoundedRectangle setHeight(int height, Curve curve) {
78+
this.height = height;
79+
set("height", height, curve);
80+
return this;
81+
}
82+
83+
/**
84+
* Returns the height of this <code>RoundedRectangle</code> in world units.
85+
* <p>
86+
* Default is 100.
87+
*
88+
* @return the height of this <code>RoundedRectangle</code>.
89+
*/
90+
public int getHeight() {
91+
return height;
92+
}
93+
94+
/**
95+
* Sets the radius of this <code>RoundedRectangle</code>'s corners in world units.
96+
*
97+
* @param radius
98+
* the radius for the corners of this <code>RoundedRectangle</code>.
99+
* @param curve
100+
* the transition to animate between values of this property.
101+
* @return this <code>RoundedRectangle</code>
102+
*/
103+
public RoundedRectangle setRadius(int radius, Curve curve) {
104+
this.radius = radius;
105+
set("radius", radius, curve);
106+
return this;
107+
}
108+
109+
/**
110+
* Sets the radius of this <code>RoundedRectangle</code>'s corners in world units.
111+
*
112+
* @param radius
113+
* the radius for the corners of this <code>RoundedRectangle</code>.
114+
* @return this <code>RoundedRectangle</code>
115+
*/
116+
public RoundedRectangle setRadius(int radius) {
117+
return setRadius(radius,null);
118+
}
119+
120+
/**
121+
* Returns the radius of this <code>RoundedRectangle</code>'s corners in world units.
122+
* <p>
123+
* Default is 20.
124+
* </p>
125+
*
126+
* @return the radius of the corners of this <code>RoundedRectangle</code>.
127+
*/
128+
public int getRadius() {
129+
return radius;
130+
}
131+
132+
@Override
133+
Entity.Type getType() {
134+
return Entity.Type.ROUNDED_RECTANGLE;
135+
}
136+
137+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Serializer {
8787

8888
types = new HashMap<>();
8989
types.put(Type.RECTANGLE, "R");
90+
types.put(Type.ROUNDED_RECTANGLE, "K");
9091
types.put(Type.CIRCLE, "C");
9192
types.put(Type.GROUP, "G");
9293
types.put(Type.BUFFERED_GROUP, "B");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class Entity {
145145

146146
this.container.zIndex = state.zIndex
147147
this.container.alpha = state.alpha
148-
this.container.position.set(state.x * globalData.coeff, state.y * globalData.coeff)
148+
this.container.position.set(state.x * globalData.toWorldUnits, state.y * globalData.toWorldUnits)
149149
this.container.scale.set(state.scaleX || eps, state.scaleY || eps)
150150
this.container.rotation = state.rotation
151151
this.container._visible = state.visible && !this.hide

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

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,25 @@ import { BitmapText } from './BitmapText.js'
77
import { Group } from './Group.js'
88
import { BufferedGroup } from './BufferedGroup.js'
99
import { SpriteAnimation } from './SpriteAnimation.js'
10+
import { RoundedRectangle } from './RoundedRectangle.js';
1011

1112
export class EntityFactory {
1213
static create (type) {
13-
var entity
14-
switch (type) {
15-
case 'C':
16-
entity = new Circle()
17-
break
18-
case 'R':
19-
entity = new Rectangle()
20-
break
21-
case 'L':
22-
entity = new Line()
23-
break
24-
case 'S':
25-
entity = new Sprite()
26-
break
27-
case 'T':
28-
entity = new Text()
29-
break
30-
case 'X':
31-
entity = new BitmapText()
32-
break
33-
case 'G':
34-
entity = new Group()
35-
break
36-
case 'B':
37-
entity = new BufferedGroup()
38-
break
39-
case 'A':
40-
entity = new SpriteAnimation()
41-
break
42-
default:
43-
throw new Error('Exception: entity type not found: ' + type)
14+
const EntityClass = {
15+
C: Circle,
16+
R: Rectangle,
17+
L: Line,
18+
S: Sprite,
19+
T: Text,
20+
X: BitmapText,
21+
G: Group,
22+
B: BufferedGroup,
23+
A: SpriteAnimation,
24+
K: RoundedRectangle
25+
}[type]
26+
if (!EntityClass) {
27+
throw new Error('Exception: entity type not found: ' + type)
4428
}
45-
return entity
29+
return new EntityClass()
4630
}
4731
}

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class GraphicEntityModule {
1313
this.extrapolationMap = {}
1414

1515
this.globalData = {
16-
coeff: 1,
16+
toWorldUnits: 1,
1717
mustResetTree: true,
1818
mustResort: true,
1919
maskUpdates: {},
@@ -204,7 +204,28 @@ export class GraphicEntityModule {
204204
this.globalData.players = players
205205
const width = globalData.width
206206
const height = globalData.height
207-
this.globalData.coeff = fitAspectRatio(width, height, WIDTH, HEIGHT)
208-
api.coeff = this.globalData.coeff
207+
this.globalData.toWorldUnits = fitAspectRatio(width, height, WIDTH, HEIGHT)
208+
api.toWorldUnits = this.globalData.toWorldUnits
209+
210+
// Retro-compatibility
211+
Object.defineProperty(api, 'coeff', {get: () => {
212+
const msg = 'The "coeff" property of GraphicEntityModule\'s API is deprecated, please use "toWorldUnits" instead'
213+
const stack = (new Error).stack
214+
215+
if (console.groupCollapsed) {
216+
console.groupCollapsed(
217+
"%cDeprecation Warning: %c%s",
218+
"color:#614108;background:#fffbe6",
219+
"font-weight:normal;color:#614108;background:#fffbe6",
220+
msg
221+
)
222+
console.warn(stack)
223+
console.groupEnd()
224+
} else {
225+
console.warn("Deprecation Warning: ", msg)
226+
}
227+
228+
return api.toWorldUnits
229+
}})
209230
}
210231
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export default class Line extends Shape {
2626
this.graphics.clear()
2727
this.graphics.lineStyle(globalData.atLeastOnePixel(state.lineWidth), state.lineColor, state.lineAlpha)
2828
this.graphics.moveTo(0, 0)
29-
this.graphics.lineTo(-this.container.x + state.x2 * globalData.coeff, -this.container.y + state.y2 * globalData.coeff)
29+
this.graphics.lineTo(-this.container.x + state.x2 * globalData.toWorldUnits, -this.container.y + state.y2 * globalData.toWorldUnits)
3030
}
3131
}
3232
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class Rectangle extends Shape {
3333
}
3434

3535
this.graphics.lineStyle(globalData.atLeastOnePixel(state.lineWidth), state.lineColor, state.lineAlpha)
36-
this.graphics.drawRect(0, 0, state.width * globalData.coeff, state.height * globalData.coeff)
36+
this.graphics.drawRect(0, 0, state.width * globalData.toWorldUnits, state.height * globalData.toWorldUnits)
3737
if (state.fillColor !== null) {
3838
this.graphics.endFill()
3939
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Shape } from './Shape.js'
2+
3+
export class RoundedRectangle extends Shape {
4+
static defaultSideLength () {
5+
return 100
6+
}
7+
static defaultRadius () {
8+
return 20
9+
}
10+
constructor () {
11+
super()
12+
Object.assign(this.defaultState, {
13+
width: RoundedRectangle.defaultSideLength(),
14+
height: RoundedRectangle.defaultSideLength(),
15+
radius: RoundedRectangle.defaultRadius()
16+
})
17+
}
18+
19+
initDisplay () {
20+
super.initDisplay()
21+
this.graphics.drawRoundedRect(0, 0, this.defaultState.width, this.defaultState.height, this.defaultState.radius)
22+
this.graphics.endFill()
23+
}
24+
25+
updateDisplay (state, changed, globalData) {
26+
super.updateDisplay(state, changed, globalData)
27+
if (changed.lineWidth ||
28+
changed.lineColor ||
29+
changed.lineAlpha ||
30+
changed.fillColor ||
31+
changed.radius ||
32+
changed.height ||
33+
changed.width) {
34+
this.graphics.clear()
35+
if (state.fillColor !== null) {
36+
this.graphics.beginFill(state.fillColor, state.fillAlpha)
37+
}
38+
39+
this.graphics.lineStyle(globalData.atLeastOnePixel(state.lineWidth), state.lineColor, state.lineAlpha)
40+
this.graphics.drawRoundedRect(0, 0,
41+
state.width * globalData.toWorldUnits,
42+
state.height * globalData.toWorldUnits,
43+
state.radius * globalData.toWorldUnits)
44+
if (state.fillColor !== null) {
45+
this.graphics.endFill()
46+
}
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)