// These are "factory" methods, they are used when you want to use parameters of the same type and amount // for class constructors // They are used by calling ClassName.FromMethod(PARAMETERS) // for example: Velocity myVelocity = Velocity.FromCoordinates(2, -2); which creates a velocity going towards 2, -2 // or Velocity myVelocity = Velocity.FromDirection(270, 30); which creates a velocity going straight down at a speed of 30 public static Velocity FromDirection(float directionDegrees, float speed) { Velocity v = new Velocity(); v.direction = new Vector2((float)Math.Cos(directionDegrees * DEG_TO_RAD) * speed, (float)Math.Sin(directionDegrees * DEG_TO_RAD) * speed); v.speed = speed; return v; }
public static Velocity FromCoordinates(float x, float y) { Velocity v = new Velocity(); v.direction = new Vector2(x, y); v.speed = v.direction.Length(); return v; }
// Load sprite from existing texture public BasicSprite(Texture2D tex, bool collide) { spriteTex = tex; spritePos = new Vector2(0, 0); spriteSize = spriteTex.Bounds.Size; velocity = Velocity.Zero; collidable = collide; if (!collidable) borderColor = Color.Blue; }
// Load sprite from file, requires you pass in a game instance for content loading // when subclassing BasicSprite you must create the same Constructors in the subclass with :base(parameters) // the parameters should match the ones here public BasicSprite(Game game, string file, bool collide) { try { spriteTex = game.Content.Load<Texture2D>(file); // load the texture spritePos = new Vector2(0, 0); // initial position spriteSize = spriteTex.Bounds.Size; // get the size from the texture size velocity = Velocity.Zero; collidable = collide; if (!collidable) borderColor = Color.Blue; } catch (ContentLoadException) { Console.WriteLine(file + " not loaded. Probably can't be found.\n"); } }
public void updatePositionFromVelocity(Velocity v) { spritePos = getUpdatePositionFromVelocity(v); }
public Vector2 getUpdatePositionFromVelocity(Velocity v) { return spritePos + v.getDirection(); }