Skip to content

Commit ce7a66d

Browse files
committed
fix animations for player
1 parent 7b53552 commit ce7a66d

File tree

4 files changed

+63
-32
lines changed

4 files changed

+63
-32
lines changed

src/entities/phys/physicsEntity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UPDATE_PRIORITY, Sprite, Point, ViewContainer } from "pixi.js";
1+
import { UPDATE_PRIORITY, Sprite, Point, ViewContainer, TilingSprite } from "pixi.js";
22
import { IPhysicalEntity, OnDamageOpts } from "../entity";
33
import { Water } from "../water";
44
import { BodyWireframe } from "../../mixins/bodyWireframe";
@@ -57,7 +57,7 @@ export abstract class PhysicsEntity<
5757
public readonly bodyMoving$: Observable<boolean>;
5858

5959
constructor(
60-
public readonly sprite: ViewContainer,
60+
public readonly sprite: Sprite|TilingSprite,
6161
protected physObject: RapierPhysicsObject,
6262
protected gameWorld: GameWorld,
6363
) {

src/entities/playable/worm.ts

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import Logger from "../../log";
4646
import { WormState, InnerWormState } from "./wormState";
4747
import { filter, first } from "rxjs";
4848
import { TweenEngine } from "../../motion/tween";
49+
import { TiledSpriteAnimated } from "../../utils/tiledspriteanimated";
4950

5051
export enum EndTurnReason {
5152
TimerElapsed = 0,
@@ -84,9 +85,6 @@ export interface WormRecordedState extends PlayableRecordedState {
8485
weapon: IWeaponCode;
8586
}
8687

87-
const ANIM_FRAME_RATE = 2;
88-
const TILES_PER_SHEET = 0;
89-
9088
/**
9189
* Physical representation of a worm on the map. May be controlled.
9290
*/
@@ -109,9 +107,6 @@ export class Worm extends PlayableEntity<WormRecordedState> {
109107
private static idleAnim: Texture;
110108
private static impactDamageMultiplier = 0.75;
111109
private static minImpactForDamage = 12;
112-
private tileCounter = 0;
113-
private timeSinceLastAnim = 0;
114-
115110
protected fireWeaponDuration = 0;
116111
private currentWeapon: IWeaponDefinition = WeaponBazooka;
117112
protected state = new WormState(InnerWormState.Inactive);
@@ -191,28 +186,27 @@ export class Worm extends PlayableEntity<WormRecordedState> {
191186
private readonly toaster?: Toaster,
192187
private readonly recorder?: StateRecorder,
193188
) {
194-
console.log(
195-
Worm.idleAnim
196-
)
197-
const sprite = new TilingSprite({
189+
const sprite = new TiledSpriteAnimated({
198190
texture: Worm.idleAnim,
199191
width: 96,
200192
height: 144,
201193
tileScale: {x: 1, y: 1},
202-
tilePosition: {x: 0, y: 0}
194+
tilePosition: {x: 0, y: 0},
195+
scale: {x: 0.33, y: 0.33},
196+
anchor: {x: 0.5, y: 0.5},
197+
columns: 10,
198+
tileCount: 120,
199+
fps: 60,
203200
});
204-
sprite.scale.set(0.33,0.33);
205-
sprite.anchor.set(0.5, 0.5);
206201
const body = world.createRigidBodyCollider(
207202
ColliderDesc.cuboid(
208-
(sprite.width*0.33) / (PIXELS_PER_METER * 2),
209-
(sprite.height*0.33) / (PIXELS_PER_METER * 2),
203+
(sprite.width*sprite.scale.x) / (PIXELS_PER_METER * 2),
204+
(sprite.height*sprite.scale.y) / (PIXELS_PER_METER * 2),
210205
)
211206
.setActiveEvents(ActiveEvents.COLLISION_EVENTS)
212207
.setCollisionGroups(Worm.collisionBitmask)
213208
.setSolverGroups(Worm.collisionBitmask)
214209
.setFriction(0.1),
215-
//.setMass(5),
216210
RigidBodyDesc.dynamic()
217211
.setTranslation(position.worldX, position.worldY)
218212
.lockRotations(),
@@ -439,7 +433,7 @@ export class Worm extends PlayableEntity<WormRecordedState> {
439433
this.fireAngle = Math.PI * 2 - this.fireAngle;
440434
}
441435
this.facingRight = !this.facingRight;
442-
this.sprite.scale.x = this.facingRight ? 0.33 : -0.33;
436+
this.sprite.scale.x = this.facingRight ? Math.abs(this.sprite.scale.x) : -Math.abs(this.sprite.scale.x);
443437
}
444438

445439
this.state.transition(
@@ -651,22 +645,10 @@ export class Worm extends PlayableEntity<WormRecordedState> {
651645

652646
update(dt: number, dMs: number): void {
653647
super.update(dt, dMs);
654-
this.timeSinceLastAnim += dMs;
655-
if (this.timeSinceLastAnim > 30) {
656-
this.timeSinceLastAnim = 0;
657-
this.tileCounter += 1;
658-
const tile = (this.sprite as TilingSprite);
659-
if (this.tileCounter % 10 === 0) {
660-
this.tileCounter = 0;
661-
tile.tilePosition.x += 0;
662-
tile.tilePosition.y += tile.height;
663-
} else {
664-
tile.tilePosition.x += tile.width;
665-
}
666-
}
667648
if (this.sprite.destroyed) {
668649
return;
669650
}
651+
(this.sprite as TiledSpriteAnimated).update(dMs);
670652
this.wireframe.setDebugText(
671653
`worm_state: ${this.state.stateName}, velocity: ${this.body.linvel().y} ${this.impactVelocity}, aim: ${this.fireAngle}`,
672654
);

src/flags.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export enum DebugLevel {
1010
class Flags extends EventEmitter {
1111
public DebugView: DebugLevel;
1212
public simulatePhysics = true;
13+
public stepAnimationsId = "";
1314

1415
constructor() {
1516
super();
@@ -31,6 +32,8 @@ class Flags extends EventEmitter {
3132
(globalThis as any)["wormgineFlags"] = {
3233
toggleSimulatePhysics: () =>
3334
(this.simulatePhysics = !this.simulatePhysics),
35+
stepAnimation: (step = true) =>
36+
(this.stepAnimationsId = step ? Math.random().toString() : ""),
3437
};
3538
}
3639
}

src/utils/tiledspriteanimated.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Texture, TilingSprite, TilingSpriteOptions } from "pixi.js";
2+
import Flags from "../flags";
3+
4+
interface TiledSpriteAnimatedOptions extends TilingSpriteOptions {
5+
columns: number;
6+
tileCount: number;
7+
fps: number;
8+
}
9+
10+
export class TiledSpriteAnimated extends TilingSprite {
11+
private tileCounter = 0;
12+
private timeSinceLastAnim = 0;
13+
private readonly targetFrameMs;
14+
private readonly tileCount: number;
15+
private readonly columns: number;
16+
private debugStepAnim: string = "";
17+
constructor(opts: TiledSpriteAnimatedOptions) {
18+
super(opts)
19+
this.targetFrameMs = 1000 / opts.fps;
20+
this.tileCount = opts.tileCount;
21+
this.columns = opts.columns;
22+
}
23+
24+
public update(deltaMs: number) {
25+
this.timeSinceLastAnim += deltaMs;
26+
if (Flags.stepAnimationsId) {
27+
if (this.debugStepAnim === Flags.stepAnimationsId) {
28+
return;
29+
}
30+
this.debugStepAnim = Flags.stepAnimationsId;
31+
}
32+
else if (this.timeSinceLastAnim < this.targetFrameMs) {
33+
return;
34+
}
35+
this.timeSinceLastAnim = 0;
36+
this.tileCounter += 1;
37+
if (this.tileCounter === this.tileCount) {
38+
this.tileCounter = 0;
39+
}
40+
// XXX: This is buggy. Using max helped to stop gittery anims.
41+
const tileColumn = Math.max(1, this.tileCounter % this.columns);
42+
const tileRow = Math.floor(this.tileCounter / this.columns);
43+
this.tilePosition.x = tileColumn * this.width;
44+
this.tilePosition.y = tileRow * this.height;
45+
}
46+
}

0 commit comments

Comments
 (0)