// Constructor for particles public Animation( Actor.AnimationType type, Texture2D spriteSheet, int frameCount, bool isLooping = false, float frameTime = 0.1f) { this.owner = null; this.animationType = type; this.texture = spriteSheet; this.frameDuration = frameTime; this.isLooping = isLooping; this.frameTime = 0.0f; this.frameCount = frameCount; this.isPlaying = false; this.frameIndex = 0; this.frameWidth = spriteSheet.Width / frameCount; this.frameHeight = spriteSheet.Height; this.currentFrame.Width = this.frameWidth; this.currentFrame.Height = this.frameHeight; this.currentFrame.Y = 0; }
/// <summary> /// Returns a normalized vector pointing to the other actor /// from ourselves. /// </summary> public Vector2 DirectionTo(Actor other) { return Vector2.Normalize(other.position - this.Position); }
public virtual void collideWith(Actor other) { }
/// <summary> /// Returns a float representing the distance between /// this actor and the other actor. /// </summary> public static float DistanceBetween(Actor a, Actor b) { return Vector2.Distance(a.Position, b.Position); }
/// <summary> /// Checking whether an actor is within bounds for culling purposes. /// </summary> public bool isInBounds(Actor a) { if (view.Contains(a.getBufferedRectangleBounds(0))) return true; return false; }
/// <summary> /// Returns the maximum of 0.0f or the distance from an /// actor's position to the outer border of the camera /// view. /// </summary> public float getDistanceOutsideBorders(Actor actor) { Vector2 pos = actor.Position; // top or bottom if (pos.X >= bounds.leftBound && pos.X <= bounds.rightBound) { // outside top border if (pos.Y < bounds.upperBound) return bounds.upperBound - pos.Y; // outside bottom border else if (pos.Y > bounds.lowerBound) return pos.Y - bounds.lowerBound; // in bounds else return 0.0f; } // left or right else if (pos.Y >= bounds.upperBound && pos.Y <= bounds.lowerBound) { // outside right border if (pos.X > bounds.rightBound) return pos.X - bounds.rightBound; // outside left border else if (pos.X < bounds.leftBound) return bounds.leftBound - pos.X; // in bounds else return 0.0f; } // I don't know how it'd get here else return 0.0f; }
/// <summary> /// Returns the actor's distance from the center of the /// view. /// </summary> public float getDistanceFromViewCenter(Actor actor) { return Vector2.Subtract(actor.Position, viewCenter).Length(); }
public static void separate(Actor a, Actor b) { if (a.changeInPosition.Length() > b.changeInPosition.Length()) a.Position -= a.changeInPosition; else b.Position -= b.changeInPosition; }
//private static Vector2 smallForce = new Vector2(50,50); //http://en.wikipedia.org/wiki/Inelastic_collision public static Vector2[] collide(Actor a, Actor b) { Vector2 ua = a.Velocity; Vector2 ub = b.Velocity; float ma = a.Mass; float mb = b.Mass; //(cr * mb * (ub - ua) + ma * ua + mb * ua) / (ma + mb); Vector2 va = (cr * mb * (ub - ua) + ma * ua + mb * ua) / (ma + mb); Vector2 vb = (cr * ma * (ua - ub) + ma * ua + mb * ua) / (ma + mb); a.Velocity = va; b.Velocity = vb; Vector2[] res = { va, vb }; return res; }
public static void applyMovement(Actor a, float delta, Boolean applyFric ) { Vector2 acc = a.Acceleration; Vector2 vel = a.Velocity; Vector2 pos = a.Position; //this is not used currently /* Vector2 accFric = -1 * vel; Vector2 accFricNormalize = accFric; if (!accFricNormalize.Equals(Vector2.Zero)) accFricNormalize.Normalize(); else { accFricNormalize.X = (float) Math.Cos(a.Rotation); accFricNormalize.Y = (float)Math.Sin(a.Rotation); accFricNormalize.Normalize(); accFricNormalize = -1 * accFricNormalize; } */ a.Velocity = acc * delta + a.MiscAcceleration * delta + vel; // cap the max speed for each state if (a.CurrState != Actor.state.Dashing && Math.Sqrt(magnitudeSquared(a.Velocity)) > a.MaxVel && !a.IsBeingKnockedBack) a.Velocity = vel = a.MaxVel * Vector2.Normalize(a.Velocity); else if(a.CurrState == Actor.state.Dashing && Math.Sqrt(magnitudeSquared(a.Velocity)) > a.MaxVelDash && !a.IsBeingKnockedBack) a.Velocity = a.MaxVelDash * Vector2.Normalize(a.Velocity); // cap total max speed if (Math.Sqrt(magnitudeSquared(a.Velocity)) > Actor.MAX_POSSIBLE_SPEED) a.velocity = Actor.MAX_POSSIBLE_SPEED * Vector2.Normalize(a.Velocity); //relative friction if (applyFric) a.Velocity = vel = uk * a.Velocity; //a.Position = pos = vel * delta + pos; Vector2 dV = vel * delta; a.move(dV.X, dV.Y); //Rotations //if ( !finalVelNormalize.Equals(Vector2.Zero)) if (!acc.Equals(Vector2.Zero)) // turn direction to mirror controller { a.Rotation = VectorToAngle(a.Acceleration); } //update the bounds float s = (float)Math.Sin(a.Rotation); float c = (float)Math.Cos(a.Rotation); //rotate about the origin then add to position a.bounds.center.X = /*c * (a.Offset.X) - s * (a.Offset.Y) +*/ a.Position.X; a.bounds.center.Y = /* s * (a.Offset.X) + c * (a.Offset.Y) +*/ a.Position.Y; if (a is Shark) { Shark sh = (Shark)a; sh.mouthPoint = sh.Bounds.center; sh.mouthPoint.X += 100; RotatePoint(sh.Bounds.center.X, sh.Bounds.center.Y, sh.Rotation, ref sh.mouthPoint); sh.mouthCircle.center = sh.mouthPoint; } if (a is Penguin) { Penguin p = (Penguin)a; p.spearPoint = p.Bounds.center; p.spearPoint.X += p.spearLength; RotatePoint(p.Bounds.center.X, p.Bounds.center.Y, p.Rotation, ref p.spearPoint); p.spearCircle.center = p.spearPoint; p.spearDeflectorAura.center = p.Position; } a.acceleration *= accDecay; a.MiscAcceleration *= accDecay; //zero if too small if ( Math.Abs(a.Velocity.X) < eps) a.velocity.X = 0; if ( Math.Abs(a.Velocity.Y) < eps) a.velocity.Y = 0; if ( Math.Abs(a.Acceleration.X) < eps) a.acceleration.X = 0; if ( Math.Abs(a.Acceleration.Y) < eps) a.acceleration.Y = 0; }
private void keepInBounds(Actor a) { Rectangle aBounds = a.getBufferedRectangleBounds(0); if (!gameplayBoundaries.Contains(aBounds)) { // 1.) determine the wall(s) with which we are colliding // 3.) ensure position within bounds // 2.) zero velocity and acceleration along those axes if (aBounds.Left < gameplayBoundaries.Left) { a.move((float)Math.Abs(gameplayBoundaries.Left - aBounds.Left) + EPSILON, 0.0f); if (a.velocity.X < 0.0f) a.velocity.X = 0.0f; //if (a.acceleration.X < 0.0f) // a.acceleration.X = 0.0f; a.acceleration.X = -(a.acceleration.X); } if (aBounds.Right > gameplayBoundaries.Right) { a.move(-((float)Math.Abs(gameplayBoundaries.Right - aBounds.Right) + EPSILON), 0.0f); if (a.velocity.X > 0.0f) a.velocity.X = 0.0f; //if (a.acceleration.X > 0.0f) // a.acceleration.X = 0.0f; a.acceleration.X = -(a.acceleration.X); } if (aBounds.Top < gameplayBoundaries.Top) { a.move(0.0f, (float)Math.Abs(gameplayBoundaries.Top - aBounds.Top) + EPSILON); if (a.velocity.Y < 0.0f) a.velocity.Y = 0.0f; //if (a.acceleration.Y < 0.0f) // a.acceleration.Y = 0.0f; a.acceleration.Y = -(a.acceleration.Y); } if (aBounds.Bottom > gameplayBoundaries.Bottom) { a.move(0.0f, -((float)Math.Abs(gameplayBoundaries.Bottom - aBounds.Bottom) + EPSILON)); if (a.velocity.Y > 0.0f) a.velocity.Y = 0.0f; //if (a.acceleration.Y > 0.0f) // a.acceleration.Y = 0.0f; a.acceleration.Y = -(a.acceleration.Y); } } }
private bool isOffScreen(Actor a) { Rectangle aBounds = a.getBufferedRectangleBounds(0); return !camera.View.Contains(aBounds) && !camera.View.Intersects(aBounds); }
private bool isNearAPlayer(Actor a) { Vector2 aPos = a.Position; float minDist = 5000.0f; float currDist = 0.0f; foreach (Penguin p in players) { currDist = Vector2.Distance(p.Position, aPos); if (currDist < minDist) minDist = currDist; } if (minDist <= SHARK_SPAWN_CLOSENESS_THRESHOLD) return true; return false; }
private void wrapAround(Actor a) { Rectangle aBounds = a.getBufferedRectangleBounds(0); if (!gameplayBoundaries.Contains(aBounds)) { // 1.) determine the wall(s) with which we are colliding // 2.) if we are completely outside on that wall, then // reflect to the other side of the stage. // off the right side if (aBounds.Left >= gameplayBoundaries.Right) { a.move(-(float)(gameplayBoundaries.Width + (aBounds.Left - gameplayBoundaries.Right)), 0.0f); } // off the left side if (aBounds.Right <= gameplayBoundaries.Left) { a.move((float)(gameplayBoundaries.Width + (gameplayBoundaries.Left - aBounds.Right)), 0.0f); } // off the bottom side if (aBounds.Top >= gameplayBoundaries.Bottom) { a.move(0.0f, -(float)((aBounds.Top - gameplayBoundaries.Bottom) + gameplayBoundaries.Height)); } // off the top side if (aBounds.Bottom <= gameplayBoundaries.Top) { a.move(0.0f, (float)((gameplayBoundaries.Top - aBounds.Bottom) + gameplayBoundaries.Height)); } } }
public override void handleAnimationComplete(Actor.AnimationType t) { if (t == Actor.AnimationType.Death) base.die(); }