Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Serializer {
keys.put("zIndex", "z");
keys.put("blendMode", "b");
keys.put("images", "I");
keys.put("imageRange", "IR");
keys.put("restarted", "rs");
keys.put("playing", "p");
keys.put("loop", "l");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codingame.gameengine.module.entities;

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -169,7 +170,15 @@ public SpriteAnimation setImages(String... images) {
throw new IllegalArgumentException("Animation must contain at least 1 image.");
}
this.images = images;
set("images", Stream.of(images).collect(Collectors.joining(",")), null);
String[] compressed = compressImages();
if (compressed == images) {
set("images", Stream.of(images).collect(Collectors.joining(",")), null);
set("imageRange", "");
}
else {
set("images", "");
set("imageRange", compressed[0], null);
}
return this;
}

Expand All @@ -181,4 +190,29 @@ public SpriteAnimation setImages(String... images) {
public String[] getImages() {
return images;
}
}

private String[] compressImages() {
if (Stream.of(images).anyMatch(i -> i.contains("|"))) return images;
String uncompressed = Stream.of(images).collect(Collectors.joining(","));
for (int prefixLength = images[0].length() - 1; prefixLength > 0; prefixLength--) {
String prefix = images[0].substring(0, prefixLength);
String numberText = images[0].substring(prefixLength);
int start = 0;
try {
start = Integer.parseInt(numberText);
} catch (NumberFormatException ex) {
break; // can't parse, no compression possible
}
boolean validPrefix = true;
for (int i = 1; i < images.length; i++) {
String suffix = String.format("%0" + numberText.length() + "d", start + i); // add leading zeros
if (!images[i].equals(prefix + suffix)) validPrefix = false;
}
if (!validPrefix) continue;
String compressed = prefix + "|" + numberText + "|" + images[images.length - 1].substring(prefixLength);
if (compressed.length() < uncompressed.length())
return new String[]{compressed};
}
return images;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const PROPERTY_KEY_MAP = {
z: 'zIndex',
b: 'blendMode',
I: 'images',
IR: 'imageRange',
rs: 'restarted',
p: 'playing',
l: 'loop',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class SpriteAnimation extends TextureBasedEntity {
super()
Object.assign(this.defaultState, {
images: '',
imageRange: '',
loop: false,
duration: 1000,
playing: true,
Expand All @@ -34,6 +35,20 @@ export class SpriteAnimation extends TextureBasedEntity {
updateDisplay (state, changed, globalData, frame, progress) {
super.updateDisplay(state, changed, globalData)

if (state.imageRange) {
let parts = state.imageRange.split('|')
let prefix = parts[0]
let startText = parts[1]
let lastImage = prefix + parts[2]
let images = [prefix + startText]
for (let i = 1; images[images.length - 1] !== lastImage; i++) {
let suffix = `${+startText + i}`
while (suffix.length < startText.length) suffix = '0' + suffix
images.push(prefix + suffix)
}
state.images = images.join(',')
}

if (state.images) {
const images = state.images.split(',')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const PROPERTIES = {
baseHeight: constOpts,
image: stringOpts,
images: stringOpts,
imageRange: stringOpts,
scaleMode: stringOpts,
restarted: {
type: String,
Expand Down