Skip to content

Commit 3def7fa

Browse files
author
Julien
committed
fix(sdk): performance++: only save states when they differ from previous
1 parent 0fe26f5 commit 3def7fa

File tree

3 files changed

+70
-39
lines changed

3 files changed

+70
-39
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ export class PropertiesCommand {
152152
apply (entities, frameInfo) {
153153
let entity = entities.get(this.id)
154154
entity.addState(this.t, { values: this.params, curve: this.curve }, frameInfo.number, frameInfo)
155+
entity.stateAdded = true
155156
}
156-
}
157157
export class WorldCommitCommand {
158158
constructor (args, globalData) {
159159
this.times = args.map(v => +v)

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

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,31 @@ export class Entity {
6262
render (progress, data, globalData) {
6363
let subframes = this.states[data.number]
6464
this.container.visible = false
65+
let start = null
66+
let end
67+
// This t is used to animate the interpolation
68+
let t = 1
6569
if (subframes && subframes.length) {
6670
let index = 0
6771
while (index < subframes.length - 1 && subframes[index].t < progress) {
6872
index++
6973
}
70-
let start = subframes[index - 1]
71-
let end = subframes[index]
72-
// This t is used to animate the interpolation
73-
let t
74+
75+
start = subframes[index - 1]
76+
end = subframes[index]
7477

7578
if (!start) {
76-
// The start frame must be at the end of the previous turn
77-
var prev = this.states[data.previous.number] || []
79+
// The start frame must be at the end of a previous turn
80+
let frameNumbers = Object.keys(this.states)
81+
let index = frameNumbers.indexOf(data.number.toString()) - 1
82+
// Failsafe
83+
if (index === -1) {
84+
index = frameNumbers.length - 1
85+
}
86+
while (index >= 0 && frameNumbers[index] >= data.number) {
87+
index--
88+
}
89+
let prev = this.states[frameNumbers[index]] || []
7890
start = prev[prev.length - 1]
7991

8092
// If it didn't exist on the previous turn, don't even animate it
@@ -88,41 +100,53 @@ export class Entity {
88100
} else {
89101
t = unlerp(start.t, end.t, progress)
90102
}
91-
if (start) {
92-
const changed = {}
93-
const state = Object.assign({}, this.currentState)
94-
95-
for (let property of this.properties) {
96-
const opts = PROPERTIES[property] || PROPERTIES.default
97-
const lerpMethod = opts.lerpMethod
98-
const curve = end.curve[property] || (a => a)
99-
const newValue = lerpMethod(start[property], end[property], curve(t))
100-
if (newValue !== this.currentState[property]) {
101-
changed[property] = true
102-
state[property] = newValue
103-
}
104-
}
105-
this.updateDisplay(state, changed, globalData, data, progress)
106-
Object.assign(this.currentState, state)
107-
this.container.visible = this.container._visible
108-
if (changed.children) {
109-
globalData.mustResetTree = true
110-
if (typeof this.postUpdate === 'function') {
111-
globalData.updatedBuffers.push(this)
112-
}
113-
}
114-
if (changed.zIndex) {
115-
globalData.mustResort = true
116-
}
117-
if (changed.mask) {
118-
globalData.maskUpdates[this.id] = state.mask
103+
} else {
104+
// Look for the most recent state, but don't interpolate?
105+
let frameNumbers = Object.keys(this.states)
106+
let index = frameNumbers.length - 1
107+
while (index >= 0 && frameNumbers[index] > data.number) {
108+
index--
109+
}
110+
let substates = this.states[frameNumbers[index]]
111+
112+
if (substates != null) {
113+
start = substates[substates.length - 1]
114+
end = start
115+
} else {
116+
Object.assign(this.currentState, this.defaultState)
117+
}
118+
}
119+
if (start) {
120+
const changed = {}
121+
const state = Object.assign({}, this.currentState)
122+
for (let property of this.properties) {
123+
const opts = PROPERTIES[property] || PROPERTIES.default
124+
const lerpMethod = opts.lerpMethod
125+
const curve = end.curve[property] || (a => a)
126+
const newValue = lerpMethod(start[property], end[property], curve(t))
127+
if (newValue !== this.currentState[property]) {
128+
changed[property] = true
129+
state[property] = newValue
119130
}
120-
if (Object.keys(changed).length !== 0 && this.parent) {
121-
this.parent.notifyChange()
131+
}
132+
this.updateDisplay(state, changed, globalData, data, progress)
133+
Object.assign(this.currentState, state)
134+
this.container.visible = this.container._visible
135+
if (changed.children) {
136+
globalData.mustResetTree = true
137+
if (typeof this.postUpdate === 'function') {
138+
globalData.updatedBuffers.push(this)
122139
}
123140
}
124-
} else {
125-
Object.assign(this.currentState, this.defaultState)
141+
if (changed.zIndex) {
142+
globalData.mustResort = true
143+
}
144+
if (changed.mask) {
145+
globalData.maskUpdates[this.id] = state.mask
146+
}
147+
if (Object.keys(changed).length !== 0 && this.parent) {
148+
this.parent.notifyChange()
149+
}
126150
}
127151
}
128152

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ export class GraphicEntityModule {
7575
const previousFrameNumber = frameInfo.previous.number
7676
this
7777
.entities.forEach(entity => {
78+
// Only create if entity is affected by this frameInfo
79+
if (entity.stateAdded) {
80+
entity.stateAdded = false
81+
} else {
82+
return
83+
}
84+
7885
// Create empty substate array if none
7986
if (!entity.states[frameNumber]) {
8087
entity.states[frameNumber] = []

0 commit comments

Comments
 (0)