Skip to content

Commit

Permalink
fix: flickering motion of an event as it moves
Browse files Browse the repository at this point in the history
  • Loading branch information
RSamaium committed Oct 18, 2023
1 parent 4885ab2 commit b2b8832
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
10 changes: 5 additions & 5 deletions packages/client/src/Components/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ export class RpgComponent<T = any> extends Container {
}
else {
const { position, direction } = this.data as RpgCommonPlayer
this._x = Math.floor(position?.x ?? 0)
this._y = Math.floor(position?.y ?? 0)
this.z = Math.floor(position?.z ?? 0)
this._x = position?.x ?? 0
this._y = position?.y ?? 0
this.z = position?.z ?? 0
this.direction = direction
}
this._rotation = this.data['rotation'] ?? 0
Expand Down Expand Up @@ -262,15 +262,15 @@ export class RpgComponent<T = any> extends Container {
update(obj: any, objChanged: any, time: number, deltaRatio: number): { moving: boolean } {
if (this.dragMode?.dragging) return { moving: true }

const { speed, teleported, map, fixed, rotation } = obj
const { speed, teleported, map, fixed } = obj
this.data = obj
this.setPosition()
const renderSpeed = speed * deltaRatio
if (this._rotation != this.angle) {
this.angle += Math.min(renderSpeed, this._rotation - this.angle)
}

let moving = false
let moving = obj.moving ?? false

if (!fixed) {
if (teleported != this.teleported || map != this.map) {
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/Sprite/Character.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export default class Character extends Sprite {
this.playAnimation(AnimationEnum.Walk)
}
else {
this.playAnimation(AnimationEnum.Stand)
this.playAnimation(AnimationEnum.Stand)
}
}

Expand Down
39 changes: 38 additions & 1 deletion packages/common/src/AbstractObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Vector2d, Vector2dZero } from './Vector2d'
import { Box } from './VirtualGrid'
import { Behavior, ClientMode, Direction, MoveClientMode, MoveTo, PlayerType, Position, PositionXY, Tick } from '@rpgjs/types'
import { from, map, mergeMap, Observable, Subject, tap, takeUntil, filter } from 'rxjs'
import { RpgCommonPlayer } from './Player'

const ACTIONS = { IDLE: 0, RUN: 1, ACTION: 2 }

Expand All @@ -24,6 +25,7 @@ export class AbstractObject {
width: number = 0
speed: number
direction: number = 3
moving: boolean = false

/*
Properties for move mode
Expand Down Expand Up @@ -751,15 +753,42 @@ export class AbstractObject {
*/
stopMoveTo() {
if (this.destroyMove$.closed) return
this.moving = false
this.destroyMove$.next(true)
this.destroyMove$.unsubscribe()
}

private _lookToward(player: Vector2d, otherPlayer: Vector2d): Direction {
const { x, y } = player;
const { x: ox, y: oy } = otherPlayer;

// Calculate the differences between the x and y coordinates.
const dx = ox - x;
const dy = oy - y;

// Determine the primary direction based on the relative magnitude
// of the x and y differences.
if (Math.abs(dx) > Math.abs(dy)) {
if (dx > 0) {
return Direction.Right;
} else {
return Direction.Left;
}
} else {
if (dy > 0) {
return Direction.Down;
} else {
return Direction.Up;
}
}
}

_moveTo(tick$: Observable<Tick>, positionTarget: AbstractObject | RpgShape | PositionXY, options: MoveTo = {}): Observable<Vector2d> {
let i = 0
let count = 0
const lastPositions: Vector2d[] = []
this.stopMoveTo()
this.moving = true
this.destroyMove$ = new Subject()
const { infinite, onStuck, onComplete } = options
const getPosition = (): Vector2d => {
Expand Down Expand Up @@ -792,9 +821,16 @@ export class AbstractObject {
i = 0
}
if (
lastPositions[2] && lastPositions[0].isEqual(lastPositions[2])
lastPositions[2] &&
(
lastPositions[0].isEqual(lastPositions[2]) ||
lastPositions[1].isEqual(lastPositions[2]) ||
lastPositions[0].isEqual(lastPositions[1])
)
) {
this.direction = this._lookToward(this.position, getPosition())
onStuck?.(count)
this.moving = false
}
else if (this.position.isEqual(getPosition())) {
onComplete?.()
Expand All @@ -804,6 +840,7 @@ export class AbstractObject {
}
else {
count = 0
this.moving = true
}
})
)
Expand Down
5 changes: 1 addition & 4 deletions packages/sample2/main/scene-map.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { RpgSceneMapHooks, RpgSceneMap, RpgGui } from '@rpgjs/client'

const sceneMap: RpgSceneMapHooks = {
onAfterLoading(scene: RpgSceneMap) {
RpgGui.display('tooltip')
scene.viewport?.setZoom(1.5)
}

}

export default sceneMap;
5 changes: 5 additions & 0 deletions packages/server/src/Player/Player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ const playerSchemas = {
$permanent: false
},

moving: {
$permanent: false
},

param: Object,
hp: Number,
sp: Number,
Expand Down Expand Up @@ -262,6 +266,7 @@ export class RpgPlayer extends RpgCommonPlayer {
this._gui = {}
this._elementsEfficiency = []
this._statesEfficiency = []
this.moving = false

this.addParameter(MAXHP, MAXHP_CURVE)
this.addParameter(MAXSP, MAXSP_CURVE)
Expand Down
2 changes: 2 additions & 0 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,13 @@ export class RpgServerEngine {
if (!playerInstance) continue
const player = playerInstance.otherPossessedPlayer ?? playerInstance
if (player.pendingMove.length > 0) {
player.moving = true
const lastFrame = player.pendingMove[player.pendingMove.length - 1]
if (this.inputOptions.workers) obj.push(player.toObject())
else {
p.push(this.gameEngine.processInput(player.playerId, this.globalConfig.inputs).then((val) => {
player.pendingMove = []
player.moving = false
player._lastFramePositions = {
frame: lastFrame.frame,
position: { ...player.position }
Expand Down

0 comments on commit b2b8832

Please sign in to comment.