-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathcomponent_transform.go
More file actions
486 lines (408 loc) · 14 KB
/
component_transform.go
File metadata and controls
486 lines (408 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spx
import (
"math"
"github.com/goplus/spbase/mathf"
coreproject "github.com/goplus/spx/v2/internal/core/project"
spxlog "github.com/goplus/spx/v2/internal/log"
engine "github.com/goplus/spx/v2/pkg/spx/pkg/engine"
)
const (
// minSpeed is the minimum allowed speed value to prevent division by zero.
minSpeed = 0.001
// minBounceComponent prevents sprites from getting stuck at boundaries.
minBounceComponent = 0.2
// fullCircleDegrees represents a complete rotation in degrees.
fullCircleDegrees = 360.0
// halfCircleDegrees represents half a rotation in degrees.
halfCircleDegrees = 180.0
)
func toRotationStyle(style string) RotationStyle {
switch style {
case "left-right":
return LeftRight
case "none":
return None
case "normal":
return Normal
default:
spxlog.Warn("Unrecognized rotationStyle value '%s', using default 'Normal'.", style)
return Normal
}
}
// toRadian converts degrees to radians.
func toRadian(dir float64) float64 {
return math.Pi * dir / 180
}
// normalizeDirection normalizes a direction angle to the range (-180, 180].
func normalizeDirection(dir float64) float64 {
if dir <= -180 {
dir += 360
} else if dir > 180 {
dir -= 360
}
return dir
}
// transformComponent encapsulates all position, rotation, and scale functionality
// for sprites in the game world.
type transformComponent struct {
componentBase
// Position coordinates in world space
x, y float64
// Rotation properties
direction float64
rotationStyle RotationStyle
// Transform origin
pivot mathf.Vec2
// State
isDirty bool
}
// initialize initializes the transform component from configuration.
func (t *transformComponent) initialize(sprite *SpriteImpl, spriteCfg *coreproject.SpriteConfig) {
t.componentBase.initialize(sprite, spriteCfg)
t.x = spriteCfg.X
t.y = spriteCfg.Y
t.direction = spriteCfg.Heading
t.rotationStyle = toRotationStyle(spriteCfg.RotationStyle)
t.pivot = spriteCfg.Pivot
t.isDirty = false
}
// cloneFrom creates a new transform component by cloning from source.
func (t *transformComponent) cloneFrom(src component, newSprite *SpriteImpl) component {
srcTransform := src.(*transformComponent)
return &transformComponent{
componentBase: componentBase{sprite: newSprite},
x: srcTransform.x,
y: srcTransform.y,
direction: srcTransform.direction,
rotationStyle: srcTransform.rotationStyle,
pivot: srcTransform.pivot,
isDirty: false,
}
}
// onDestroy performs cleanup when the component is destroyed.
func (t *transformComponent) onDestroy() {
// No resources to cleanup
}
// markDirty marks the transform as dirty, triggering an update.
func (t *transformComponent) markDirty() {
t.isDirty = true
t.sprite.spriteState.IsDirty = true
}
// getPivot returns the current pivot point.
func (t *transformComponent) getPivot() mathf.Vec2 {
return t.pivot
}
// MoveForward moves the sprite forward by the specified step size
// in the direction it's currently facing.
func (t *transformComponent) MoveForward(step float64) {
sin, cos := math.Sincos(toRadian(t.direction))
t.moveTo(t.x+step*sin, t.y+step*cos)
}
// Glide moves the sprite smoothly to the specified position over the given duration.
func (t *transformComponent) Glide(x, y float64, secs float64) {
if isDebugInstrEnabled() {
spxlog.Debug("Glide: sprite=%s, x=%v, y=%v, secs=%v", t.sprite.name, x, y, secs)
}
x0, y0 := t.XY()
from := mathf.NewVec2(x0, y0)
to := mathf.NewVec2(x, y)
aniCopy := coreproject.AniConfig{
Duration: secs,
From: &from,
To: &to,
AniType: coreproject.AniTypeGlide,
IsLoop: true,
}
animName := t.sprite.getStateAnimName(StateGlide)
t.sprite.animation().doTween(animName, &aniCopy)
}
// GlideTo moves the sprite smoothly to the specified object's position
// over the given duration.
func (t *transformComponent) GlideTo(obj any, secs float64) {
x, y := t.sprite.g.objectPos(obj)
t.Glide(x, y, secs)
}
// Step moves the sprite forward by the specified step size using a stepping animation.
func (t *transformComponent) Step(step, speed float64, animation SpriteAnimationName) {
dirSin, dirCos := math.Sincos(toRadian(t.direction))
diff := mathf.NewVec2(step*dirSin, step*dirCos)
to := mathf.NewVec2(t.x, t.y).Add(diff)
t.StepToPos(to.X, to.Y, speed, animation)
}
// StepToPos moves the sprite to the specified position using a stepping animation.
func (t *transformComponent) StepToPos(x, y, speed float64, animation SpriteAnimationName) {
if animation == "" {
animation = t.sprite.getStateAnimName(StateStep)
}
// If no animation exists, move to target immediately.
if !t.sprite.hasAnim(animation) {
t.SetXYpos(x, y)
return
}
from := mathf.NewVec2(t.x, t.y)
to := mathf.NewVec2(x, y)
distance := from.DistanceTo(to)
ani, ok := t.sprite.getAnimation(animation)
if !ok {
return
}
duration := math.Abs(distance) * ani.StepDuration / math.Max(speed, minSpeed)
t.doAnimatedTween(animation, ani, &from, &to, coreproject.AniTypeMove, duration, speed)
}
// StepTo moves the sprite to the specified object's position using a stepping animation.
func (t *transformComponent) StepTo(obj any, speed float64, animation SpriteAnimationName) {
x, y := t.sprite.g.objectPos(obj)
t.StepToPos(x, y, speed, animation)
}
// SetSize sets the sprite's scale to the specified size.
func (t *transformComponent) SetSize(size float64) {
if isDebugInstrEnabled() {
spxlog.Debug("SetSize: sprite=%s, size=%v", t.sprite.name, size)
}
if size < 0 {
spxlog.Warn("SetSize: sprite=%s, size=%v is negative, clamping to 0", t.sprite.name, size)
size = 0
}
t.sprite.runtimeState.Scale = size
t.sprite.runtimeState.IsCostumeDirty = true
t.markDirty()
t.sprite.updatePhysicsShapesScale()
}
// ChangeSize changes the sprite's scale by the specified delta.
func (t *transformComponent) ChangeSize(delta float64) {
if isDebugInstrEnabled() {
spxlog.Debug("ChangeSize: sprite=%s, delta=%v", t.sprite.name, delta)
}
t.SetSize(t.sprite.runtimeState.Scale + delta)
}
// XY returns the current position coordinates.
func (t *transformComponent) XY() (x, y float64) {
return t.x, t.y
}
// Xpos returns the current x-coordinate.
func (t *transformComponent) Xpos() float64 {
return t.x
}
// Ypos returns the current y-coordinate.
func (t *transformComponent) Ypos() float64 {
return t.y
}
// SetXYpos sets both position coordinates to the specified values.
func (t *transformComponent) SetXYpos(x, y float64) {
t.moveTo(x, y)
}
// SetXpos sets the x-coordinate to the specified value.
func (t *transformComponent) SetXpos(x float64) {
t.moveTo(x, t.y)
}
// SetYpos sets the y-coordinate to the specified value.
func (t *transformComponent) SetYpos(y float64) {
t.moveTo(t.x, y)
}
// ChangeXYpos changes both position coordinates by the specified deltas.
func (t *transformComponent) ChangeXYpos(dx, dy float64) {
t.moveTo(t.x+dx, t.y+dy)
}
// ChangeXpos changes the x-coordinate by the specified delta.
func (t *transformComponent) ChangeXpos(dx float64) {
t.moveTo(t.x+dx, t.y)
}
// ChangeYpos changes the y-coordinate by the specified delta.
func (t *transformComponent) ChangeYpos(dy float64) {
t.moveTo(t.x, t.y+dy)
}
// DistanceTo calculates the Euclidean distance from this sprite to the specified object.
func (t *transformComponent) DistanceTo(obj any) float64 {
x, y := t.x, t.y
x2, y2 := t.sprite.g.objectPos(obj)
dx := x - x2
dy := y - y2
return math.Sqrt(dx*dx + dy*dy)
}
// setXY sets position directly without triggering side effects.
// This is used for internal operations that need to bypass normal movement logic.
func (t *transformComponent) setXY(x, y float64) {
t.x, t.y = x, y
}
// moveTo moves the sprite to the specified position, handling pen movement
// and transform updates.
func (t *transformComponent) moveTo(x, y float64) {
x, y = t.fixWorldRange(x, y)
t.sprite.movePen(x, y)
t.x, t.y = x, y
t.markDirty()
}
// fixWorldRange clamps sprite position within world boundaries.
func (t *transformComponent) fixWorldRange(x, y float64) (float64, float64) {
rect := t.sprite.bounds()
if rect == nil {
return x, y
}
worldW, worldH := t.sprite.g.worldSize()
maxW := float64(worldW)/2.0 + float64(rect.Size.X)
maxH := float64(worldH)/2.0 + float64(rect.Size.Y)
x = mathf.Clamp(x, -maxW, maxW)
y = mathf.Clamp(y, -maxH, maxH)
return x, y
}
// Heading returns the current direction the sprite is facing.
func (t *transformComponent) Heading() Direction {
return t.direction
}
// SetHeading sets the sprite's direction to the specified value.
func (t *transformComponent) SetHeading(dir Direction) bool {
return t.applyDirection(dir)
}
// ChangeHeading changes the sprite's direction by the specified delta.
func (t *transformComponent) ChangeHeading(delta Direction) bool {
return t.applyDirection(t.direction + delta)
}
// SetRotationStyle sets the rotation style for the sprite.
func (t *transformComponent) SetRotationStyle(style RotationStyle) {
if isDebugInstrEnabled() {
spxlog.Debug("SetRotationStyle: sprite=%s, style=%v", t.sprite.name, style)
}
t.rotationStyle = style
}
// Turn rotates the sprite by the specified angle using an animation.
func (t *transformComponent) Turn(delta Direction, speed float64, animation SpriteAnimationName) {
from := t.direction
to := t.direction + delta
t.doTurnAnimation(from, to, speed, animation, func() {
if t.ChangeHeading(delta) && isDebugInstrEnabled() {
spxlog.Debug("Turn: sprite=%s, delta=%v", t.sprite.name, delta)
}
})
}
// TurnTo turns the sprite to face the specified object or direction using an animation.
func (t *transformComponent) TurnTo(obj any, speed float64, animation SpriteAnimationName) {
targetAngle := t.calculateTargetAngle(obj)
fromAngle, toAngle := t.normalizeAngleRange(t.direction, targetAngle)
t.doTurnAnimation(fromAngle, toAngle, speed, animation, func() {
if t.applyDirection(targetAngle) && isDebugInstrEnabled() {
spxlog.Debug("TurnTo: sprite=%s, obj=%v", t.sprite.name, obj)
}
})
}
// BounceOffEdge bounces the sprite off the edge of the stage by reflecting
// its direction vector based on the edge that was touched.
func (t *transformComponent) BounceOffEdge() {
if isDebugInstrEnabled() {
spxlog.Debug("BounceOffEdge: %s", t.sprite.name)
}
nearestEdge := t.sprite.checkNearestTouchedBoundary()
if nearestEdge == 0 {
return
}
radians := toRadian(90 - t.direction)
dx := math.Cos(radians)
dy := -math.Sin(radians)
dx, dy = t.calculateBounceDirection(nearestEdge, dx, dy)
newDirection := engine.RadToDeg(math.Atan2(dy, dx)) + 90
t.direction = normalizeDirection(newDirection)
}
// applyDirection sets the sprite's direction to the normalized value.
// Returns true if the direction was actually changed.
func (t *transformComponent) applyDirection(dir float64) bool {
dir = normalizeDirection(dir)
if t.direction == dir {
return false
}
t.direction = dir
t.markDirty()
return true
}
// calculateTargetAngle calculates the angle to turn toward the specified object.
func (t *transformComponent) calculateTargetAngle(obj any) float64 {
switch v := obj.(type) {
case Direction:
return v
default:
x, y := t.sprite.g.objectPos(obj)
dx := x - t.x
dy := y - t.y
return 90 - engine.RadToDeg(math.Atan2(dy, dx))
}
}
// normalizeAngleRange normalizes two angles to minimize the rotation distance.
// This ensures the sprite takes the shortest path when rotating.
func (t *transformComponent) normalizeAngleRange(from, to float64) (float64, float64) {
fromNorm := math.Mod(from+fullCircleDegrees, fullCircleDegrees)
toNorm := math.Mod(to+fullCircleDegrees, fullCircleDegrees)
if toNorm-fromNorm > halfCircleDegrees {
fromNorm += fullCircleDegrees
} else if fromNorm-toNorm > halfCircleDegrees {
toNorm += fullCircleDegrees
}
return fromNorm, toNorm
}
// calculateBounceDirection calculates the new direction vector after bouncing
// off the specified edge.
func (t *transformComponent) calculateBounceDirection(edge int, dx, dy float64) (float64, float64) {
switch edge {
case touchingScreenLeft:
dx = math.Max(minBounceComponent, math.Abs(dx))
case touchingScreenTop:
dy = math.Max(minBounceComponent, math.Abs(dy))
case touchingScreenRight:
dx = -math.Max(minBounceComponent, math.Abs(dx))
case touchingScreenBottom:
dy = -math.Max(minBounceComponent, math.Abs(dy))
}
return dx, dy
}
// doTurnAnimation handles turn animation with fallback to direct heading change.
// This helper eliminates code duplication between Turn and TurnTo methods.
func (t *transformComponent) doTurnAnimation(
from, to float64,
speed float64,
animation SpriteAnimationName,
fallback func(),
) {
if animation == "" {
animation = t.sprite.getStateAnimName(StateTurn)
}
ani, ok := t.sprite.getAnimation(animation)
if !ok {
fallback()
return
}
absDelta := math.Abs(from - to)
duration := ani.TurnToDuration / fullCircleDegrees * absDelta / math.Max(speed, minSpeed)
t.doAnimatedTween(animation, ani, from, to, coreproject.AniTypeTurn, duration, speed)
}
// doAnimatedTween creates and executes a tween animation with the specified parameters.
// This helper eliminates code duplication in animation methods.
func (t *transformComponent) doAnimatedTween(
name SpriteAnimationName,
base *coreproject.AniConfig,
from, to any,
aniType coreproject.AniType,
duration float64,
speed float64,
) {
speed = math.Max(speed, minSpeed)
aniCopy := *base
aniCopy.From = from
aniCopy.To = to
aniCopy.AniType = aniType
aniCopy.Duration = duration
aniCopy.IsLoop = true
aniCopy.Speed = speed
t.sprite.doTween(name, &aniCopy)
}