Here I'm going to add some little bits of code that I find handy. Might be some use to somebody.
Desaturate a Colour in C#/XNA
//c = the colour to desaturate
//amount = the amount of desaturation between 0 and 1
Color desaturate(Color c, float amount)>
{
Color gray = new Color((c.G * 0.59f), (c.R * 0.3f), (c.B * 0.11f));
Color newC = Color.Lerp(gray, c, amount);
return new Color(newC.R, newC.G, newC.B, 255); //the extra 255 is the alpha value. include this to make sure the color is fully opaque
}
Get Angle Between Two points in C#/XNA
//p1 = start point
//p2 = end/destination point
public float getAngleFromPoints(Vector2 p1, Vector2 p2)
{
float angle = (float)Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
if (angle < 0)
{
angle += (float)(2 * Math.PI);
}
return angle;
}
Convert Between Degrees and Radians in C#/XNA
//r = the angle in radians to convert
public float RadiansToDegrees(float r)
{
return (float)(r * 180 / Math.PI);
}
//d = the angle in degrees to convert
public float DegreesToRadians(float d)
{
return (float)(Math.PI * d / 180);
}
Detect keyboard and controller input in C#/XNA
public class MyKeyboard
{
private KeyboardState oldState;
private KeyboardState newState;
public MyKeyboard()
{
oldState = Keyboard.GetState();
}
//call this at the start of your game loop
public void init()
{
newState = Keyboard.GetState();
}
//call this at the end of your game loop
public void reset()
{
oldState = newState;
}
/////////////////////////////////////////////////////////////////////////////////
//check if a key is being held down
public bool keyDown(Keys k)
{
if (newState.IsKeyDown(k))
{
return true;
}
return false;
}
//check if a key has just been pressed
public bool keyPressed(Keys k)
{
if (newState.IsKeyDown(k) && oldState.IsKeyDown(k) == false)
{
return true;
}
return false;
}
//check if a key has just been released
public bool keyReleased(Keys k)
{
if (newState.IsKeyDown(k) == false && oldState.IsKeyDown(k))
{
return true;
}
return false;
}
}
public class MyController
{
private GamePadState oldState;
private GamePadState newState;
public MyController()
{
oldState = GamePad.GetState(PlayerIndex.One);
}
//call this at the start of your game loop
public void init()
{
newState = GamePad.GetState(PlayerIndex.One);
}
//call this at the end of your game loop
public void reset()
{
oldState = newState;
}
/////////////////////////////////////////////////////////////////////////////////
//check if a button is being held down
public bool buttonDown(Buttons b)
{
if (newState.IsButtonDown(b))
{
return true;
}
return false;
}
//check if a button has just been pressed
public bool buttonPressed(Buttons b)
{
if (newState.IsButtonDown(b) && oldState.IsButtonDown(b) == false)
{
return true;
}
return false;
}
//check if a button has just been released
public bool buttonReleased(Buttons b)
{
if (newState.IsButtonDown(b) == false && oldState.IsButtonDown(b))
{
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////////
//get the value of the left thumb stick
public Vector2 leftStick()
{
return (new Vector2(newState.ThumbSticks.Left.X, newState.ThumbSticks.Left.Y));
}
//get the value of the right thumb stick
public Vector2 rightStick()
{
return (new Vector2(newState.ThumbSticks.Right.X, newState.ThumbSticks.Right.Y));
}
//get the value of the left trigger
public float leftTrigger()
{
return newState.Triggers.Left;
}
//get the value of the right trigger
public float rightTrigger()
{
return newState.Triggers.Right;
}
}
No comments:
Post a Comment