Skip to content

Instantly share code, notes, and snippets.

Created April 23, 2014 02:32
Show Gist options
  • Save anonymous/11201010 to your computer and use it in GitHub Desktop.
Save anonymous/11201010 to your computer and use it in GitHub Desktop.
using Microsoft.Xna.Framework;
using Monocle;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TowerFall
{
public class PlayerHair : Component
{
private const int SIZE = 3;
public Entity Follow;
public Vector2 Position;
public float Alpha;
private Vector2[] offsets;
private Subtexture[] images;
private Vector2 previousPosition;
private Vector2 previousEntityPosition;
private SineWave sine;
private float scale;
private Vector2 addSpeed;
private int links;
private float linkDist;
private float timeSinceLastUpdate;
public PlayerHair(Entity follow, Vector2 position, float scale)
: base(true, true)
{
Follow = follow;
previousPosition = Position = position;
previousEntityPosition = follow.Position;
this.scale = scale;
Alpha = 1;
if (scale > 1)
{
links = 4;
linkDist = 5;
}
else
{
links = 5;
linkDist = 3;
}
offsets = new Vector2[links];
for (int i = 0; i < links; i++)
offsets[i] = new Vector2(0, SIZE * i);
images = new Subtexture[links];
for (int i = 0; i < links - 1; i++)
images[i] = TFGame.Atlas["player/hair"];
images[links - 1] = TFGame.Atlas["player/hairEnd"];
sine = new SineWave(30);
}
public void AddSpeed(Vector2 speed)
{
addSpeed = speed;
}
public override void Update()
{
sine.Update();
timeSinceLastUpdate += Engine.DeltaTime;
if (timeSinceLastUpdate < 1 / 60f)
return;
else
timeSinceLastUpdate -= 1 / 60f;
Vector2 speed = Follow.Position - previousEntityPosition;
Vector2 difference = (Position - previousPosition) * .2f + speed + addSpeed;
if (addSpeed != Vector2.Zero)
addSpeed = Calc.Approach(addSpeed, Vector2.Zero, .1f);
previousEntityPosition = Follow.Position;
previousPosition = Position;
if (difference == Vector2.Zero)
difference = -Vector2.UnitY * .2f;
float mag = difference.LengthSquared()/9;
for (int i = 1; i < links; i++)
{
Vector2 prev = offsets[i - 1];
Vector2 add = -difference * mag * .5f;
add = Vector2.Lerp(add, offsets[i] - prev, (i / (float)offsets.Length) * .3f);
add.Y += .06f + .04f * sine.Value * Math.Abs(speed.X);
if (Math.Abs(speed.X) <= .2f)
add.X += .08f * sine.Value * Math.Abs(speed.Y);
add.Normalize();
add *= linkDist;
offsets[i] = prev + add;
}
}
public override void Render()
{
for (int i = 0; i < links; i++)
{
Vector2 at = Follow.Position + Position + offsets[i];
float angle = (i == 0 ? 0 : Calc.Angle(offsets[i], offsets[i - 1]));
Draw.TextureCentered(images[i], at, Color.White * Alpha * Alpha, scale, angle);
}
}
public void RenderOutline()
{
for (int i = 0; i < links; i++)
{
Vector2 at = Follow.Position + Position + offsets[i];
float angle = (i == 0 ? 0 : Calc.Angle(offsets[i], offsets[i - 1]));
for (int j = -1; j < 2; j++)
for (int k = -1; k < 2; k++)
if (j != 0 || k != 0)
Draw.TextureCentered(images[i], at + new Vector2(j, k), Color.Black, scale, angle);
}
}
}
}
@marquavious
Copy link

all this for hair?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment