Skip to content

Commit 7bad927

Browse files
author
Valentin Vetter
committed
refactor(entities): extract SpriteBasedEntity
To prepare future subclass `TilingSprite`
1 parent 52c043d commit 7bad927

File tree

4 files changed

+131
-118
lines changed

4 files changed

+131
-118
lines changed

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+
}
Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
1-
import {TextureBasedEntity} from './TextureBasedEntity.js'
1+
import {SpriteBasedEntity} from './SpriteBasedEntity.js'
22
import {ErrorLog} from '../core/ErrorLog.js'
33
import {MissingImageError} from './errors/MissingImageError.js'
44

55
/* global PIXI */
66

7-
export class Sprite extends TextureBasedEntity {
8-
constructor () {
9-
super()
10-
Object.assign(this.defaultState, {
11-
image: null,
12-
baseWidth: null,
13-
baseHeight: null
14-
})
15-
this.missingTextures = {}
16-
}
17-
7+
export class Sprite extends SpriteBasedEntity {
188
initDisplay () {
199
super.initDisplay()
2010
if (this.defaultState.image === null) {
@@ -23,28 +13,4 @@ export class Sprite extends TextureBasedEntity {
2313
this.graphics = PIXI.Sprite.fromFrame(this.defaultState.image)
2414
}
2515
}
26-
27-
updateDisplay (state, changed, globalData) {
28-
super.updateDisplay(state, changed, globalData)
29-
if (changed.image) {
30-
try {
31-
if (state.image !== null) {
32-
this.graphics.texture = PIXI.Texture.fromFrame(state.image)
33-
} else {
34-
this.graphics.texture = PIXI.Texture.EMPTY
35-
}
36-
} catch (error) {
37-
if (!this.missingTextures[state.image]) {
38-
this.missingTextures[state.image] = true
39-
ErrorLog.push(new MissingImageError(state.image, error))
40-
}
41-
}
42-
}
43-
if (changed.baseWidth) {
44-
this.graphics.width = state.baseWidth
45-
}
46-
if (changed.baseHeight) {
47-
this.graphics.height = state.baseHeight
48-
}
49-
}
5016
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {TextureBasedEntity} from './TextureBasedEntity.js'
2+
import {ErrorLog} from '../core/ErrorLog.js'
3+
import {MissingImageError} from './errors/MissingImageError.js'
4+
5+
/* global PIXI */
6+
7+
export class SpriteBasedEntity extends TextureBasedEntity {
8+
constructor () {
9+
super()
10+
Object.assign(this.defaultState, {
11+
image: null,
12+
baseWidth: null,
13+
baseHeight: null
14+
})
15+
this.missingTextures = {}
16+
}
17+
18+
updateDisplay (state, changed, globalData) {
19+
super.updateDisplay(state, changed, globalData)
20+
if (changed.image) {
21+
try {
22+
if (state.image !== null) {
23+
this.graphics.texture = PIXI.Texture.fromFrame(state.image)
24+
} else {
25+
this.graphics.texture = PIXI.Texture.EMPTY
26+
}
27+
} catch (error) {
28+
if (!this.missingTextures[state.image]) {
29+
this.missingTextures[state.image] = true
30+
ErrorLog.push(new MissingImageError(state.image, error))
31+
}
32+
}
33+
}
34+
if (changed.baseWidth) {
35+
this.graphics.width = state.baseWidth
36+
}
37+
if (changed.baseHeight) {
38+
this.graphics.height = state.baseHeight
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)