Skip to content

Commit c4865bc

Browse files
author
Valentin Vetter
committed
feat(Sprite): add scaleMode field
1 parent e3c390a commit c4865bc

File tree

7 files changed

+39
-6
lines changed

7 files changed

+39
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.codingame.gameengine.module.entities;
2+
3+
public enum ScaleMode {
4+
LINEAR, NEAREST
5+
}

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
@@ -72,6 +72,7 @@ class Serializer {
7272
keys.put("tileY", "ty");
7373
keys.put("tileScaleX", "tsx");
7474
keys.put("tileScaleY", "tsy");
75+
keys.put("scaleMode", "sm");
7576

7677
commands = new HashMap<>();
7778
commands.put("CREATE", "C");

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public abstract class SpriteBasedEntity<T extends SpriteBasedEntity<?>> extends
88

99
private String image;
1010
private Integer baseWidth, baseHeight;
11+
private ScaleMode scaleMode = ScaleMode.LINEAR;
1112

1213
/**
1314
* Sets the image for this <code>Sprite</code>.
@@ -84,4 +85,28 @@ public Integer getBaseHeight() {
8485
return baseHeight;
8586
}
8687

88+
/**
89+
* Sets the scale mode of this <code>TextureBasedEntity</code>.
90+
*
91+
* @param scaleMode
92+
* the scale mode of this <code>TextureBasedEntity</code>.
93+
* @return this <code>TextureBasedEntity</code>.
94+
*/
95+
public T setScaleMode(ScaleMode scaleMode) {
96+
this.scaleMode = scaleMode;
97+
set("scaleMode", scaleMode);
98+
return self();
99+
}
100+
101+
/**
102+
* Returns the scale mode of this <code>TextureBasedEntity</code>.
103+
* <p>
104+
* Default is LINEAR.
105+
*
106+
* @return the scale mode of this <code>TextureBasedEntity</code>.
107+
*/
108+
public ScaleMode getScaleMode() {
109+
return scaleMode;
110+
}
111+
87112
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
/**
44
* Any PIXI Entity based on a texture shares the properties found in this <code>TextureBasedEntity</code>.
55
*
6-
* @param <T> a subclass inheriting Entity, used in order to return <b>this</b> as a T instead of a <code>TextureBasedEntity</code>.
6+
* @param <T>
7+
* a subclass inheriting Entity, used in order to return <b>this</b> as a T instead of a <code>TextureBasedEntity</code>.
78
*/
89
public abstract class TextureBasedEntity<T extends BlendableEntity<?>> extends BlendableEntity<T> {
910

10-
1111
private double anchorX = 0, anchorY = 0;
1212
private int tint = 0xFFFFFF;
1313

1414
TextureBasedEntity() {
1515
super();
1616
}
1717

18-
19-
2018
/**
2119
* Sets both the X and Y anchors of this <code>TextureBasedEntity</code> as a percentage of its width and height.
2220
* <p>

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
@@ -47,7 +47,8 @@ const PROPERTY_KEY_MAP = {
4747
tx: 'tileX',
4848
ty: 'tileY',
4949
tsx: 'tileScaleX',
50-
tsy: 'tileScaleY'
50+
tsy: 'tileScaleY',
51+
sm: 'scaleMode'
5152
}
5253

5354
export class CreateCommand {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export class SpriteBasedEntity extends TextureBasedEntity {
1010
Object.assign(this.defaultState, {
1111
image: null,
1212
baseWidth: null,
13-
baseHeight: null
13+
baseHeight: null,
14+
scaleMode: 'LINEAR'
1415
})
1516
this.missingTextures = {}
1617
}
@@ -24,6 +25,7 @@ export class SpriteBasedEntity extends TextureBasedEntity {
2425
} else {
2526
this.graphics.texture = PIXI.Texture.EMPTY
2627
}
28+
this.graphics.texture.baseTexture.scaleMode = PIXI.SCALE_MODES[state.scaleMode]
2729
} catch (error) {
2830
if (!this.missingTextures[state.image]) {
2931
this.missingTextures[state.image] = true

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const PROPERTIES = {
7676
baseHeight: constOpts,
7777
image: stringOpts,
7878
images: stringOpts,
79+
scaleMode: stringOpts,
7980
restarted: {
8081
type: String,
8182
convert (value, globalData, frameInfo, t) {

0 commit comments

Comments
 (0)