Skip to content

Commit 3db85bf

Browse files
Merge branch 'tile-sprite' into 'master'
feat(sdk): add `Tilingsprite` See merge request codingame/game-engine!231
2 parents 9b6217e + ba6156e commit 3db85bf

File tree

12 files changed

+382
-137
lines changed

12 files changed

+382
-137
lines changed

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public abstract class Entity<T extends Entity<?>> {
2424
Mask mask;
2525

2626
static enum Type {
27-
CIRCLE, LINE, RECTANGLE, SPRITE, TEXT, BITMAPTEXT, GROUP, BUFFERED_GROUP, SPRITEANIMATION, ROUNDED_RECTANGLE, POLYGON
27+
CIRCLE, LINE, RECTANGLE, SPRITE, TEXT, BITMAPTEXT, GROUP, BUFFERED_GROUP, SPRITEANIMATION, ROUNDED_RECTANGLE, POLYGON, TILING_SPRITE
2828
}
2929

3030
Entity() {
@@ -449,29 +449,18 @@ public boolean isVisible() {
449449
return visible;
450450
}
451451

452-
/**
453-
* Sets a given <code>Shape</code> as this <code>Entity</code>'s <code>Mask</code>.
454-
*
455-
* @param shape
456-
* the mask.
457-
* @return this <code>Entity</code>.
458-
*/
459-
public T setMask(Shape<?> shape) {
460-
return saveMask(shape);
461-
}
462-
463452
/**
464453
* <p>
465-
* Sets a given <code>Sprite</code> as this <code>Entity</code>'s <code>Mask</code>.
454+
* Sets a given <code>Mask</code> as this <code>Entity</code>'s <code>Mask</code>.
466455
* </p>
467456
* <b>Note:</b> texture masks will only work on browsers which support WebGL
468457
*
469458
* @param sprite
470459
* the mask.
471460
* @return this <code>Entity</code>.
472461
*/
473-
public T setMask(Sprite sprite) {
474-
return saveMask(sprite);
462+
public T setMask(Mask mask) {
463+
return saveMask(mask);
475464
}
476465

477466
/**

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public Rectangle createRectangle() {
261261
newEntity(c);
262262
return c;
263263
}
264-
264+
265265
/**
266266
* Creates a new RoundedRectangle entity, its graphical counterpart will be created on the frame currently being computed.
267267
*
@@ -272,7 +272,7 @@ public RoundedRectangle createRoundedRectangle() {
272272
newEntity(c);
273273
return c;
274274
}
275-
275+
276276
/**
277277
* Creates a new Polygon entity, its graphical counterpart will be created on the frame currently being computed.
278278
*
@@ -360,6 +360,17 @@ public SpriteAnimation createSpriteAnimation() {
360360
return c;
361361
}
362362

363+
/**
364+
* Creates a new TilingSprite entity, its graphical counterpart will be created on the frame currently being computed.
365+
*
366+
* @return the entity. Modify its properties to animate the graphical counterpart.
367+
*/
368+
public TilingSprite createTilingSprite() {
369+
TilingSprite c = new TilingSprite();
370+
newEntity(c);
371+
return c;
372+
}
373+
363374
private void newEntity(Entity<?> e) {
364375
lockWorld = true;
365376
entities.add(e);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class Serializer {
6868
keys.put("points", "ps");
6969
keys.put("skewX", "kx");
7070
keys.put("skewY", "ky");
71+
keys.put("tileX", "tx");
72+
keys.put("tileY", "ty");
73+
keys.put("tileScaleX", "tsx");
74+
keys.put("tileScaleY", "tsy");
7175

7276
commands = new HashMap<>();
7377
commands.put("CREATE", "C");
@@ -99,6 +103,7 @@ class Serializer {
99103
types.put(Type.BITMAPTEXT, "X");
100104
types.put(Type.SPRITEANIMATION, "A");
101105
types.put(Type.POLYGON, "P");
106+
types.put(Type.TILING_SPRITE, "D");
102107

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

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

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,92 +4,11 @@
44
* A Sprite is a graphical entity which displays an image. That image must be loaded into the viewer's texture cache, which you can configure by
55
* adding files to the <code>assets</code> folder of your game's project.
66
*/
7-
public class Sprite extends TextureBasedEntity<Sprite> implements Mask {
8-
9-
private String image;
10-
private Integer baseWidth, baseHeight;
11-
12-
Sprite() {
13-
super();
14-
}
7+
public class Sprite extends SpriteBasedEntity<Sprite> {
158

169
@Override
1710
Entity.Type getType() {
1811
return Entity.Type.SPRITE;
1912
}
2013

21-
/**
22-
* Sets the image for this <code>Sprite</code>.
23-
* <p>
24-
* You must either:
25-
* <ul>
26-
* <li>use the filename of an image relative to the assets folder of the Java project.
27-
* <li>use the a player's nickname token.
28-
* </ul>
29-
*
30-
* @param image
31-
* the name of the image to use for this <code>Sprite</code>.
32-
* @return this <code>Sprite</code>.
33-
*/
34-
public Sprite setImage(String image) {
35-
this.image = image;
36-
set("image", image, null);
37-
return this;
38-
}
39-
40-
/**
41-
* Sets the image base width for this <code>Sprite</code>. If not set, the image base width is the real image width.
42-
*
43-
* @param baseWidth
44-
* image width
45-
* @return this <code>Sprite</code>.
46-
*/
47-
public Sprite setBaseWidth(int baseWidth) {
48-
this.baseWidth = baseWidth;
49-
set("baseWidth", baseWidth, null);
50-
return self();
51-
}
52-
53-
/**
54-
* Sets the image base height for this <code>Sprite</code>. If not set, the image base height is the real image height.
55-
*
56-
* @param baseHeight
57-
* image height
58-
* @return this <code>Sprite</code>.
59-
*/
60-
public Sprite setBaseHeight(int baseHeight) {
61-
this.baseHeight = baseHeight;
62-
set("baseHeight", baseHeight, null);
63-
return self();
64-
}
65-
66-
/**
67-
* Returns the name of the image used for this <code>Sprite</code>.
68-
* <p>
69-
* Can be a player's nickname token.
70-
*
71-
* @return the name of the image used for this <code>Sprite</code>
72-
*/
73-
public String getImage() {
74-
return image;
75-
}
76-
77-
/**
78-
* Returns the image base width for this <code>Sprite</code>. If not set, the image base width is the real image width, but this will return null.
79-
*
80-
* @return the image base width for this <code>Sprite</code>.
81-
*/
82-
public Integer getBaseWidth() {
83-
return baseWidth;
84-
}
85-
86-
/**
87-
* Returns the image base height for this <code>Sprite</code>. If not set, the image base height is the real image height, but this will return
88-
* null.
89-
*
90-
* @return the image base height for this <code>Sprite</code>.
91-
*/
92-
public Integer getBaseHeight() {
93-
return baseHeight;
94-
}
9514
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.codingame.gameengine.module.entities;
2+
3+
/**
4+
* A Sprite is a graphical entity which displays an image. That image must be loaded into the viewer's texture cache, which you can configure by
5+
* adding files to the <code>assets</code> folder of your game's project.
6+
*/
7+
public abstract class SpriteBasedEntity<T extends SpriteBasedEntity<?>> extends TextureBasedEntity<T> implements Mask {
8+
9+
private String image;
10+
private Integer baseWidth, baseHeight;
11+
12+
/**
13+
* Sets the image for this <code>Sprite</code>.
14+
* <p>
15+
* You must either:
16+
* <ul>
17+
* <li>use the filename of an image relative to the assets folder of the Java project.
18+
* <li>use the a player's nickname token.
19+
* </ul>
20+
*
21+
* @param image
22+
* the name of the image to use for this <code>Sprite</code>.
23+
* @return this <code>Sprite</code>.
24+
*/
25+
public T setImage(String image) {
26+
this.image = image;
27+
set("image", image, null);
28+
return self();
29+
}
30+
31+
/**
32+
* Sets the image base width for this <code>Sprite</code>. If not set, the image base width is the real image width.
33+
*
34+
* @param baseWidth
35+
* image width
36+
* @return this <code>Sprite</code>.
37+
*/
38+
public T setBaseWidth(int baseWidth) {
39+
this.baseWidth = baseWidth;
40+
set("baseWidth", baseWidth, null);
41+
return self();
42+
}
43+
44+
/**
45+
* Sets the image base height for this <code>Sprite</code>. If not set, the image base height is the real image height.
46+
*
47+
* @param baseHeight
48+
* image height
49+
* @return this <code>Sprite</code>.
50+
*/
51+
public T setBaseHeight(int baseHeight) {
52+
this.baseHeight = baseHeight;
53+
set("baseHeight", baseHeight, null);
54+
return self();
55+
}
56+
57+
/**
58+
* Returns the name of the image used for this <code>Sprite</code>.
59+
* <p>
60+
* Can be a player's nickname token.
61+
*
62+
* @return the name of the image used for this <code>Sprite</code>
63+
*/
64+
public String getImage() {
65+
return image;
66+
}
67+
68+
/**
69+
* Returns the image base width for this <code>Sprite</code>. If not set, the image base width is the real image width, but this will return null.
70+
*
71+
* @return the image base width for this <code>Sprite</code>.
72+
*/
73+
public Integer getBaseWidth() {
74+
return baseWidth;
75+
}
76+
77+
/**
78+
* Returns the image base height for this <code>Sprite</code>. If not set, the image base height is the real image height, but this will return
79+
* null.
80+
*
81+
* @return the image base height for this <code>Sprite</code>.
82+
*/
83+
public Integer getBaseHeight() {
84+
return baseHeight;
85+
}
86+
87+
}

0 commit comments

Comments
 (0)