Skip to content

Commit

Permalink
better floater spirit stun
Browse files Browse the repository at this point in the history
Now they flicker back to life like a broken lightbulb, instead of blinking like a pac-man ghost, and I love it. Also I separated the jiggling out and made it very short just before they wake up, subtler, and I like it.
  • Loading branch information
aaronwhyte committed Feb 10, 2019
1 parent 06ae155 commit 6bb2706
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
24 changes: 24 additions & 0 deletions public_html/game6/spirits/basespirit.js
Expand Up @@ -28,6 +28,9 @@ function BaseSpirit(screen) {
this.nextActiveTime = -1;
this.nextPassiveTime = -1;

// Spirit is stunned until this time.
this.stunUntil = -Infinity;

BaseSpirit.prototype.reset.call(this, screen);
}
BaseSpirit.prototype = new Spirit();
Expand Down Expand Up @@ -618,3 +621,24 @@ BaseSpirit.prototype.startTimeouts = function() {
this.scheduleActiveTimeout(this.now());
this.schedulePassiveTimeout(this.now() + BaseSpirit.PASSIVE_TIMEOUT * Math.random());
};

/**
* @param {number} duration the spirit will be stunned until this time from now, plus any pre-existing stun time.
*/
BaseSpirit.prototype.addStunDuration = function(duration) {
this.stunUntil = Math.max(this.now(), this.stunUntil) + duration;
};

/**
* @param {number} duration the spirit will be stunned until this time from now. Zero to unstun.
*/
BaseSpirit.prototype.setStunDuration = function(duration) {
this.stunUntil = this.now() + duration;
};

/**
* @returns {number} zero means the spirit is no longer stunned
*/
BaseSpirit.prototype.getStun = function() {
return Math.max(0, this.stunUntil - this.now());
};
43 changes: 39 additions & 4 deletions public_html/game6/spirits/floaterspirit.js
Expand Up @@ -41,6 +41,9 @@ FloaterSpirit.SLEEP_RADS = 15;
// Wake up with this many rads away from a player view bubble.
FloaterSpirit.WAKE_RADS = 10;

FloaterSpirit.PRE_UNSTUN_BLINK_TIME = 33;
FloaterSpirit.PRE_UNSTUN_JIGGLE_TIME = 10;

FloaterSpirit.ELASTICITY = 0.7;

FloaterSpirit.SCHEMA = {
Expand Down Expand Up @@ -82,8 +85,34 @@ FloaterSpirit.prototype.getActiveTimeout = function() {
return FloaterSpirit.ACTIVE_TIMEOUT;
};

FloaterSpirit.prototype.getModelId = function() {
return ModelId.FLOATER;
/**
* @override
*/
FloaterSpirit.prototype.getColor = function() {
// let s = this.getStun();
// let b = 1;
// if (!s) {
// b = 1;
// } else {
// b = 0;
// }
// let c = 0.5 + 0.5 * b;
// return this.color.setRGBA(c, 1, c, 1);


let blinkStun = FloaterSpirit.PRE_UNSTUN_BLINK_TIME;
let s = this.getStun();
let b = 1;
if (!s) {
return this.color.setRGBA(1, 1, 1, 1);
} else if (s > blinkStun) {
b = 0;
} else {
b = Math.max(0, (blinkStun - s) / blinkStun - Math.random());
// b = Math.pow((blinkStun - s) / blinkStun, 4);
}
let c = 0.5 + b; // 0.5 - 1.5
return this.color.setRGBA(c, c, c, 1);
};

/**
Expand Down Expand Up @@ -186,9 +215,14 @@ FloaterSpirit.prototype.activeOnAPixel = function(dg, px) {
FloaterSpirit.prototype.activeStunnedOnPixel = function(dg, px) {
let friction = this.getFriction();
let gravity = 0.05 * this.getActiveTimeout();
if (!this.grounded && px) {

let wakeFactor = Math.max(0, FloaterSpirit.PRE_UNSTUN_JIGGLE_TIME - this.getStun())
/ FloaterSpirit.PRE_UNSTUN_JIGGLE_TIME;

if ((wakeFactor || !this.grounded) && px) {
this.nearbyPx = px;
px.getPixelToGround(this.accel).scaleToLength(gravity);
this.addBodyAngVel(0.5 * (Math.random() - 0.5) * wakeFactor);
}
this.activeFrictionAndAccel(friction, this.accel);
};
Expand Down Expand Up @@ -321,7 +355,8 @@ FloaterSpirit.prototype.onHitOther = function(collisionVec, mag, otherBody, othe
this.lastThumpSoundTime = now;

if (this.getStun() && otherBody.mass === Infinity) {
if (this.getBodyVel().magnitudeSquared() < Math.random() * Math.random()) {
if (this.getStun() > FloaterSpirit.PRE_UNSTUN_JIGGLE_TIME &&
this.getBodyVel().magnitudeSquared() < Math.random() * Math.random()) {
this.setBodyVel(Vec2d.ZERO);
this.setBodyAngVel(0);
this.grounded = true;
Expand Down

0 comments on commit 6bb2706

Please sign in to comment.