mirror of
https://github.com/Terncode/pixel.horse.git
synced 2026-12-03 05:10:04 +01:00
Fix boat bobbing flicker from time overflow
This commit is contained in:
1 parent
6635e0e84e
commit
1648a40aa8
4 files changed
+31
-7
No files matched your search
@@ -2,7 +2,7 @@ import {
|
||||
Entity, Point, TileType, Rect, MapInfo, Camera, Region, MapState, defaultMapState, Pony,
|
||||
EntityFlags, WorldMap, EntityState,
|
||||
} from '../common/interfaces';
|
||||
import { contains, removeItem, boundsIntersect, array, pushUniq, containsPoint, removeItemFast } from '../common/utils';
|
||||
import { contains, removeItem, boundsIntersect, array, pushUniq, containsPoint, removeItemFast, getTimeFrame } from '../common/utils';
|
||||
import { isBoundsVisible, } from '../common/camera';
|
||||
import {
|
||||
getRegionElevation, setRegionElevation,
|
||||
@@ -487,8 +487,7 @@ export function updateEntities(game: PonyTownGame, gameTime: number, delta: numb
|
||||
|
||||
if ((flags & EntityFlags.Bobbing) !== 0) {
|
||||
const bobs = entity.bobs!;
|
||||
const frame = (((gameTime / 1000) * entity.bobsFps!) | 0) % bobs.length;
|
||||
entity.z = toWorldZ(bobs[frame]);
|
||||
entity.z = toWorldZ(bobs[getTimeFrame(gameTime, entity.bobsFps!, bobs.length)]);
|
||||
}
|
||||
else if ((flags & EntityFlags.StaticY) === 0) {
|
||||
entity.z = getMapHeightAt(map, entity.x, entity.y, gameTime);
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
import * as sprites from '../generated/sprites';
|
||||
import { getRegionTile, getRegionElevation, setRegionTile, getRegionTileIndex, setRegionTileDirty, doRelativeToRegion, getRegionGlobal } from './region';
|
||||
import { tileWidth, tileHeight, tileElevation, WATER_FPS, REGION_SIZE, WATER_HEIGHT } from './constants';
|
||||
import { clamp, toInt, at, invalidEnumReturn } from './utils';
|
||||
import { clamp, toInt, at, invalidEnumReturn, getTimeFrame } from './utils';
|
||||
import { isAreaVisible } from './camera';
|
||||
import { WHITE } from './colors';
|
||||
import { releasePalette } from '../graphics/paletteManager';
|
||||
@@ -604,8 +604,7 @@ export function getTileHeight(
|
||||
return 0.5;
|
||||
}
|
||||
else if (typeNumber === TileTypeNumber.Boat) {
|
||||
const frame = ((gameTime / 1000) * WATER_FPS) | 0;
|
||||
return waterHeight[frame % waterHeight.length];
|
||||
return waterHeight[getTimeFrame(gameTime, WATER_FPS, waterHeight.length)];
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -168,6 +168,11 @@ export function toInt(value: any): number {
|
||||
return value | 0;
|
||||
}
|
||||
|
||||
export function getTimeFrame(timeMs: number, fps: number, frames: number): number {
|
||||
const frame = Math.floor((timeMs / 1000) * fps) % frames;
|
||||
return frame < 0 ? frame + frames : frame;
|
||||
}
|
||||
|
||||
export function dispose<T extends { dispose(): void; }>(obj: T | undefined): undefined {
|
||||
if (obj) {
|
||||
obj.dispose();
|
||||
|
||||
@@ -5,7 +5,7 @@ import { spy, assert, SinonFakeTimers, useFakeTimers } from 'sinon';
|
||||
import {
|
||||
arraysEqual, removeItem, removeById, dispose, fromNow, clamp, normalize,
|
||||
toInt, hasFlag, setFlag, at, findById, distance, contains, maxDate, minDate,
|
||||
flatten, includes, isCommand, processCommand, collidersIntersect, formatDuration, att
|
||||
flatten, includes, isCommand, processCommand, collidersIntersect, formatDuration, att, getTimeFrame
|
||||
} from '../../common/utils';
|
||||
import { rect } from '../../common/rect';
|
||||
|
||||
@@ -132,6 +132,27 @@ describe('utils', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTimeFrame()', () => {
|
||||
it('returns frame for given time', () => {
|
||||
expect(getTimeFrame(0, 6, 4)).equal(0);
|
||||
expect(getTimeFrame(500, 6, 4)).equal(3);
|
||||
expect(getTimeFrame(1000, 6, 4)).equal(2);
|
||||
});
|
||||
|
||||
it('returns valid frame for timestamps that overflow 32 bit integers', () => {
|
||||
const time = Date.UTC(2026, 11, 3);
|
||||
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const t = time + i * 1000 / 6;
|
||||
expect(getTimeFrame(t, 6, 4)).equal(Math.floor(t / 1000 * 6) % 4);
|
||||
}
|
||||
});
|
||||
|
||||
it('returns positive frame for negative time', () => {
|
||||
expect(getTimeFrame(-1000, 6, 4)).equal(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toInt()', () => {
|
||||
it('returns given integer value', () => {
|
||||
expect(toInt(1)).equal(1);
|
||||
|
||||
Reference in new issue
Block a user