Skip to content

Commit

Permalink
Camera update, more physics, input delays
Browse files Browse the repository at this point in the history
In this commit, I redid most parts of camera, becouse camera was in this
framework since I created it, but I forgot to update it when I was
making changes to Spooker. So now, I simplified using camera (using
views for creating camera was totally pointless) and removed void Apply,
what was before used to apply camera to renderwindow, but becouse I
changed how camera works, that void was pointless too. Also, fixed minor
bugs in animations.
I also added one easy, but big features, and that is adding delay to
input. Becouse Spooker input class do not depends on elapsed time or on
game speed, I added there simple float what will control how often will
game input updates.
And, one big feature what was already in Spooker before, but I removed
it when I was redesigning sprites and textures are sprite collisions
(AABB rectangles, bounding boxes collisions and pixel perfect
collisions).
  • Loading branch information
deathbeam committed Apr 6, 2014
1 parent bb65301 commit 18c2b84
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 57 deletions.
5 changes: 4 additions & 1 deletion Spooker/Graphics/Animations/AnimatedSprite.cs
Expand Up @@ -61,6 +61,9 @@ public void AddAnim(string name)

public void PlayAnim(string name)
{
if (_currentAnim == name)
return;

_pause = false;
_currentAnim = name;
_timeSinceStart = 0;
Expand Down Expand Up @@ -102,7 +105,7 @@ public void Update(GameTime gameTime)
// it's time to a next frame?
if (!_pause && _currentAnim != null &&_timeSinceStart > duration)
{
_timeSinceStart -= duration;
_timeSinceStart = 0;

SourceRect = this[_currentAnim].GetNextFrame ();
}
Expand Down
20 changes: 14 additions & 6 deletions Spooker/Graphics/Animations/Animation.cs
Expand Up @@ -16,31 +16,39 @@ namespace Spooker.Graphics.Animations
public class Animation
{
private int _currentFrame;
private List<Rectangle> _frames;

/// <summary>String used to identify this animation</summary>
public string Name;

/// <summary>List with the frames of the animation</summary>
public List<Rectangle> Frames;

/// <summary>Defines for how long will be one frame drawn</summary>
public TimeSpan Duration;

public Animation (string name)
{
Name = name;
Frames = new List<Rectangle> ();
_frames = new List<Rectangle> ();
}

public void Add(Rectangle frame)
{
_frames.Add (frame);
}

public void Remove(Rectangle frame)
{
_frames.Remove (frame);
}

public Rectangle GetNextFrame()
{
_currentFrame++;

if (_currentFrame == Frames.Count)
if (_currentFrame == _frames.Count)
_currentFrame = 0;

// Get the current frame
return Frames [_currentFrame];
return _frames [_currentFrame];
}
}
}
Expand Down
77 changes: 28 additions & 49 deletions Spooker/Graphics/Camera.cs
Expand Up @@ -22,8 +22,7 @@ namespace Spooker.Graphics
////////////////////////////////////////////////////////////
public class Camera : IUpdateable
{
internal View View;
internal Vector2 ActualPosition;
private Vector2 ActualPosition;

////////////////////////////////////////////////////////////
/// <summary>
Expand All @@ -43,18 +42,17 @@ public class Camera : IUpdateable

////////////////////////////////////////////////////////////
/// <summary>
/// Toggle for automatic position rounding. Useful if pixel
/// sizes become inconsistent or font blurring occurs.
/// Center point of the camera
/// </summary>
////////////////////////////////////////////////////////////
public bool RoundPosition;
public Vector2 Position;

////////////////////////////////////////////////////////////
/// <summary>
/// Center point of the camera
/// Size of camera visible area
/// </summary>
////////////////////////////////////////////////////////////
public Vector2 Position;
public Vector2 Size;

////////////////////////////////////////////////////////////
/// <summary>
Expand All @@ -63,7 +61,9 @@ public class Camera : IUpdateable
////////////////////////////////////////////////////////////
public Vector2 Transform(Vector2 point)
{
return point - new Vector2(Bounds.X, Bounds.Y);
return new Vector2 (
point.X - (ActualPosition.X - (Size.X / 2)),
point.Y - (ActualPosition.Y - (Size.Y / 2)));
}

////////////////////////////////////////////////////////////
Expand All @@ -75,10 +75,10 @@ public Rectangle Bounds
{
get {
return new Rectangle(
(int)(View.Center.X - (View.Size.X / 2)),
(int)(View.Center.Y - (View.Size.Y / 2)),
(int)View.Size.X,
(int)View.Size.Y);
(int)(ActualPosition.X - (Size.X / 2)),
(int)(ActualPosition.Y - (Size.Y / 2)),
(int)Size.X,
(int)Size.Y);
}
}

Expand All @@ -87,9 +87,13 @@ public Rectangle Bounds
/// Checks if object is visible in current camera area
/// </summary>
////////////////////////////////////////////////////////////
public bool ObjectIsVisible(Rectangle bounds)
public bool ObjectIsVisible(Vector2 position, Vector2 size)
{
return (Bounds.Intersects(bounds));
return (Bounds.Intersects(new Rectangle(
(int)position.X,
(int)position.Y,
(int)size.X,
(int)size.Y)));
}

////////////////////////////////////////////////////////////
Expand All @@ -101,71 +105,46 @@ public static Camera Default
{
get
{
return new Camera(new Rectangle(0, 0, 800, 600))
return new Camera(new Rectangle(400, 300, 800, 600))
{
Smoothness = 0.33f,
Smooth = false,
RoundPosition = true
};
}
}

////////////////////////////////////////////////////////////
/// <summary>
/// Creates new instance of Camera class using Rectangle
/// </summary>
////////////////////////////////////////////////////////////
public Camera(Rectangle rect)
: this(new View (new FloatRect(rect.X, rect.Y, rect.Width, rect.Height)))
{
}

////////////////////////////////////////////////////////////
/// <summary>
/// Creates new instance of Camera class using View
/// Creates new instance of Camera class.
/// </summary>
////////////////////////////////////////////////////////////
public Camera(View view)
public Camera(Rectangle rectangle)
{
View = new View(view);
View.Center = View.Size / 2;
Position = new Vector2 (View.Center);
Size = new Vector2 ((float)rectangle.Width, (float)rectangle.Height);
Position = new Vector2 ((float)rectangle.X, (float)rectangle.Y);
ActualPosition = Position;
}

////////////////////////////////////////////////////////////
/// <summary>
/// Applies camera position to view.
/// </summary>
////////////////////////////////////////////////////////////
public void Apply()
{
var center = ActualPosition;

if (RoundPosition)
{
center.X = (float)Math.Round(ActualPosition.X, 1);
center.Y = (float)Math.Round(ActualPosition.Y, 1);
}

View.Center = center.ToSfml();
}

////////////////////////////////////////////////////////////
/// <summary>
/// Updates camera position based on camera smooth settings.
/// </summary>
////////////////////////////////////////////////////////////
public void Update(GameTime gameTime)
{
if (ActualPosition == Position)
return;

if (Smooth)
{
var dir = Vector2.Direction(ActualPosition, Position);
var len = Vector2.Distance(ActualPosition, Position);
ActualPosition += Vector2.LengthDir(dir, len * Smoothness);
}
else
{
ActualPosition = Position;
}
}
}
}
10 changes: 9 additions & 1 deletion Spooker/Input/GameInput.cs
Expand Up @@ -8,7 +8,8 @@ namespace Spooker.Input
public class GameInput : IUpdateable
{
private readonly List<InputAction> _actions;

private float _lastUpdate;
public float UpdateDelay;
public KeyboardManager Keyboard;
public MouseManager Mouse;

Expand Down Expand Up @@ -39,6 +40,13 @@ public void RemoveAction(string name)

public void Update(GameTime gameTime)
{
var dt = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
_lastUpdate += dt;

if (_lastUpdate < UpdateDelay)
return;

_lastUpdate = 0;
Keyboard.Update (gameTime);
Mouse.Update (gameTime);

Expand Down

0 comments on commit 18c2b84

Please sign in to comment.