-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from blocktest-game/camera
Adds cameras
- Loading branch information
Showing
7 changed files
with
230 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System.Collections; | ||
namespace Blocktest.Rendering; | ||
|
||
public sealed class Camera { | ||
private readonly Color _backgroundColor; | ||
private readonly Vector2 _size; | ||
|
||
public readonly List<Renderable> RenderedComponents = new(); | ||
|
||
public Rectangle RenderLocation; | ||
public readonly RenderTarget2D RenderTarget; | ||
public Vector2 Position; | ||
|
||
public Camera(Vector2 position, Vector2 size, GraphicsDevice graphicsDevice, Color? backgroundColor = null) { | ||
Position = position; | ||
_size = size; | ||
_backgroundColor = backgroundColor ?? Color.CornflowerBlue; | ||
RenderTarget = new RenderTarget2D(graphicsDevice, (int)size.X, (int)size.Y, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.DiscardContents); | ||
} | ||
|
||
public void Draw(GraphicsDevice graphics, SpriteBatch spriteBatch) { | ||
graphics.SetRenderTarget(RenderTarget); | ||
graphics.Clear(_backgroundColor); | ||
|
||
spriteBatch.Begin(); | ||
|
||
foreach (Renderable component in RenderedComponents) { | ||
if (component.Appearance == null) { | ||
continue; | ||
} | ||
|
||
Vector2 worldPosition = component.Transform.Position; | ||
Vector2 cameraPosition = worldPosition - Position; | ||
|
||
if (worldPosition.X + component.Appearance.Bounds.Width < Position.X && | ||
worldPosition.X > Position.X + _size.X && | ||
worldPosition.Y + component.Appearance.Bounds.Height < Position.Y && | ||
worldPosition.Y > Position.Y + _size.Y) { | ||
continue; | ||
} | ||
|
||
Vector2 flippedPosition = new(cameraPosition.X, RenderTarget.Height - cameraPosition.Y - component.Appearance.Bounds.Height); | ||
|
||
Rectangle positionBounds = new((int)flippedPosition.X, (int)flippedPosition.Y, (int)(component.Appearance.Bounds.Width * component.Transform.Scale.X), | ||
(int)(component.Appearance.Bounds.Height * component.Transform.Scale.Y)); | ||
|
||
spriteBatch.Draw(component.Appearance.Texture, positionBounds, component.Appearance.Bounds, | ||
component.RenderColor, component.Transform.Rotation, component.Transform.Origin, SpriteEffects.None, (float)component.Layer / EnumCount); | ||
} | ||
|
||
spriteBatch.End(); | ||
|
||
graphics.SetRenderTarget(null); | ||
} | ||
|
||
private static readonly int EnumCount = Enum.GetValues(typeof(Layer)).Length; | ||
|
||
public Vector2 CameraToWorldPos(Vector2 mouseState) { | ||
return new((mouseState.X - RenderLocation.X) / RenderLocation.Width * RenderTarget.Width + Position.X, Position.Y + RenderTarget.Height - | ||
(mouseState.Y - RenderLocation.Y) / RenderLocation.Height * RenderTarget.Height); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace Blocktest.Rendering; | ||
|
||
public enum Layer { | ||
Top = 0, | ||
Player = 1, | ||
Default = 2, | ||
ForegroundBlocks = 3, | ||
BackgroundBlocks = 4 | ||
} | ||
|
||
public sealed class Renderable { | ||
public readonly Transform Transform; | ||
public Drawable? Appearance; | ||
public Color RenderColor; | ||
public Layer Layer; | ||
|
||
public Renderable(Transform transform, Layer layer = Layer.Default, Drawable? appearance = null, Color? renderColor = null) { | ||
Transform = transform; | ||
Layer = layer; | ||
Appearance = appearance; | ||
RenderColor = renderColor ?? Color.White; | ||
} | ||
|
||
public void Draw(SpriteBatch spriteBatch, Vector2 cameraPosition) { | ||
if (Appearance == null) { | ||
return; | ||
} | ||
|
||
spriteBatch.Draw(Appearance.Texture, Transform.Position - cameraPosition, Appearance.Bounds, RenderColor, Transform.Rotation, Transform.Origin, Transform.Scale, | ||
SpriteEffects.None, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Blocktest.Rendering; | ||
|
||
public sealed class Transform { | ||
public Vector2 Position; | ||
public float Rotation; | ||
public Vector2 Scale; | ||
public Vector2 Origin; | ||
|
||
public Transform(Vector2 position, Vector2? scale = null, float rotation = 0, Vector2? origin = null) { | ||
Position = position; | ||
Scale = scale ?? Vector2.One; | ||
Rotation = rotation; | ||
Origin = origin ?? Vector2.Zero; | ||
} | ||
} |
Oops, something went wrong.