Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop Dead (#720) #2520

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/abilities/Nutcase.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ export default (G) => {
return ability.isUpgraded() ? true : creature.size <= 2;
},
});

},

// activate() :
Expand Down
51 changes: 51 additions & 0 deletions src/animations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Game from './game';
import { Creature } from './creature';
import { Hex } from './utility/hex';
import { Ability } from './ability';
import { QuadraticCurve } from './utility/curve';

// to fix @ts-expect-error 2554: properly type the arguments for the trigger functions in `game.ts`

Expand All @@ -12,9 +13,11 @@ type AnimationOptions = {
ignoreMovementPoint?: boolean;
ignoreTraps?: boolean;
ignoreFacing?: boolean;
callback?: () => void;
callbackStepIn?: (hex?: Hex) => void;
pushed?: boolean;
turnAroundOnComplete?: boolean;
flipped?: boolean;
};

export class Animations {
Expand Down Expand Up @@ -307,4 +310,52 @@ export class Animations {

return [tween, sprite, dist];
}

death(creature: Creature, opts: AnimationOptions) {
// Animation Properties
const length = 100; // Distance travelled in x
const numSegments = 10; // "Resolution" of the curve
const speed = !opts.overrideSpeed ? 500 : opts.overrideSpeed;

// Curve should pass (0, 0)
const curve = opts.flipped ? new QuadraticCurve(0.1, 5, 0) : new QuadraticCurve(0.1, -5, 0);

// Tween properties
const segmentLength = (opts.flipped ? -1 : 1) * Math.round(length / numSegments);
const segmentTime = Math.round(speed / numSegments);

const startPos = creature.creatureSprite.getPos();

creature.healthHide();

let currSegment = 1;

const anim = () => {
if (currSegment > numSegments) {
opts.callback();
return;
}

// Calculate the point in the curve
const next = {
x: startPos.x + segmentLength * currSegment,
y: startPos.y + curve.calc_y(segmentLength * currSegment),
};

// Tween to point
creature.creatureSprite.setPx(next, segmentTime).then(() => {
// Next tween
anim();
});

currSegment++;
};

// Rotate and Fade the sprite
creature.creatureSprite.setAngle(opts.flipped ? -90 : 90, 500);
creature.creatureSprite.setAlpha(0, 500);

// Launch the sprite
anim();
}
}
31 changes: 30 additions & 1 deletion src/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,21 @@ export class Creature {
}

// Kill animation
this.creatureSprite.setAlpha(0, 500).then(() => this.destroy());
const opts = {
callback: () => {
this.destroy();
},
flipped: false,
};

// Check whether or not to flip the animation
if (killerCreature instanceof Creature) {
if (this.pos.x - killerCreature.pos.x < 0) {
opts.flipped = true;
}
}

game.animations.death(this, opts);
this.cleanHex();

game.updateQueueDisplay();
Expand Down Expand Up @@ -1840,6 +1854,17 @@ class CreatureSprite {
return this._promisifyTween(this._group, { alpha: a }, durationMS);
}

setAngle(a: number, durationMS = 0): Promise<CreatureSprite> {
if (durationMS === 0 || this._group.angle === a) {
this._group.angle = a;
return new Promise((resolve) => {
resolve(this);
});
} else {
return this._promisifyTween(this._group, { angle: a }, durationMS);
}
}

setHex(h: Hex, durationMS = 0): Promise<CreatureSprite> {
return this.setPx(h.displayPos, durationMS);
}
Expand Down Expand Up @@ -1921,6 +1946,10 @@ class CreatureSprite {
}
}

getPos() {
return this._group.position;
}

hint(text: string, hintType: CreatureHintType) {
const tooltipSpeed = 250;
const tooltipDisplaySpeed = 500;
Expand Down
6 changes: 3 additions & 3 deletions src/data/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ type ExtractValidCreatureTypes<T extends UnitData> = {
}[number];

// Create unions from the various arrays
export type UnitName = typeof unitNames[number];
export type Realm = typeof realms[number];
export type Level = typeof unitLevels[number];
export type UnitName = (typeof unitNames)[number];
export type Realm = (typeof realms)[number];
export type Level = (typeof unitLevels)[number];

// Create a union of valid creature `type`s
export type CreatureType = ExtractValidCreatureTypes<UnitData>;
Expand Down
31 changes: 31 additions & 0 deletions src/utility/curve.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Quadratic Curve Class
*
* Used to define a quadratic curve of the form y = ax^2 + bx + c
*/
export class QuadraticCurve {
a: number;
b: number;
c: number;

/**
* @constructor
* @param{number} a - Coefficient of the second order term
* @param{number} b - Coefficient of the first order term
* @param{number} c - Intercept of the curve
*/
constructor(a: number, b: number, c: number) {
this.a = a;
this.b = b;
this.c = c;
}

/**
* Calculates the resulting y value given x
* @param{number} x - the x value to use for calculation
* @returns{number} y - the resulting y value
*/
calc_y(x: number): number {
return this.a * Math.pow(x, 2) + this.b * x + this.c;
}
}
Loading