-
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.
- Loading branch information
1 parent
a278a16
commit de4c60c
Showing
16 changed files
with
450 additions
and
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Blocktest/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Adjacencies/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Blocktest/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Renderable/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=renderables/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Smoothable/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary> |
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,66 @@ | ||
using Blocktest.Rendering; | ||
using Shared.Code; | ||
using Shared.Code.Block_System; | ||
namespace Blocktest.Block_System; | ||
|
||
public class RenderableTile : TileShared { | ||
public readonly Renderable Renderable; | ||
|
||
public RenderableTile(TileShared tile, bool background) : base(tile.SourceBlock, | ||
tile.Transform.Position / GlobalsShared.GridSize) { | ||
Renderable = new Renderable(Transform, background ? Layer.BackgroundBlocks : Layer.ForegroundBlocks, | ||
BlockSpritesManager.AllBlocksSprites[tile.SourceBlock.BlockId].BlockSprite, tile.Color); | ||
} | ||
|
||
/// <summary> | ||
/// This method is called whenever an adjacent (according to a tilemap's adjacency variable) tile is placed or removed. | ||
/// Used for smoothing. | ||
/// </summary> | ||
/// <param name="position">The position of the current tile.</param> | ||
/// <param name="tilemap">The tilemap the tile is on.</param> | ||
public void UpdateAdjacencies(Vector2Int position, TilemapShared tilemap) { | ||
if (!SourceBlock.BlockSmoothing) { | ||
return; | ||
} // If the tile doesn't smooth, don't even try | ||
|
||
int bitmask = 0; // Uses bitmask smoothing, look it up | ||
|
||
if (HasSmoothableTile(position + Vector2Int.Up, tilemap)) { | ||
bitmask += 1; | ||
} | ||
if (HasSmoothableTile(position + Vector2Int.Down, tilemap)) { | ||
bitmask += 2; | ||
} | ||
if (HasSmoothableTile(position + Vector2Int.Right, tilemap)) { | ||
bitmask += 4; | ||
} | ||
if (HasSmoothableTile(position + Vector2Int.Left, tilemap)) { | ||
bitmask += 8; | ||
} | ||
|
||
Renderable.Appearance = BlockSpritesManager.AllBlocksSprites[SourceBlock.BlockId].SpriteSheet | ||
.OrderedSprites[bitmask]; | ||
} | ||
|
||
/// <summary> | ||
/// Whether or not the tile at a certain <paramref name="position" /> can smooth with this tile. | ||
/// </summary> | ||
/// <param name="position">The position of the tile to check for smoothing.</param> | ||
/// <param name="tilemap">The tilemap on which the tile you want to check for smoothing is.</param> | ||
/// <returns>Whether or not the tile can smooth with this tile.</returns> | ||
private bool HasSmoothableTile(Vector2Int position, TilemapShared tilemap) { | ||
if (tilemap.TryGetTile(position, out TileShared? tile)) { | ||
return SourceBlock.SmoothSelf | ||
? IsSameTileType(tile) | ||
: tile.SourceBlock.BlockId != 0; // Don't smooth with air, possibly find nicer way to do this later. | ||
} | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// If the tile provided is the same type (references the same block) as the current tile. | ||
/// </summary> | ||
/// <param name="otherTile">The other tile to check.</param> | ||
/// <returns>Whether or not the other block is the same type as the current tile</returns> | ||
private bool IsSameTileType(TileShared otherTile) => otherTile.SourceBlock == SourceBlock; | ||
} |
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,60 @@ | ||
using Blocktest.Rendering; | ||
using Shared.Code; | ||
using Shared.Code.Block_System; | ||
namespace Blocktest.Block_System; | ||
|
||
public class RenderableTilemap { | ||
private readonly TilemapShared _tilemap; | ||
|
||
private readonly RenderableTile[,] _renderables; | ||
private readonly Camera _camera; | ||
|
||
/// <summary> | ||
/// A list of <see cref="Vector2Int" />s that specify which blocks should be refreshed when a tile is placed/destroyed. | ||
/// Defaults to the changed block and all cardinal directions. | ||
/// </summary> | ||
private static readonly List<Vector2Int> Adjacencies = new() | ||
{ Vector2Int.Zero, Vector2Int.Up, Vector2Int.Down, Vector2Int.Left, Vector2Int.Right }; | ||
|
||
|
||
public RenderableTilemap(TilemapShared newTilemap, Camera camera) { | ||
_tilemap = newTilemap; | ||
_camera = camera; | ||
_renderables = new RenderableTile[_tilemap.TilemapSize.X, _tilemap.TilemapSize.Y]; | ||
UpdateRenderables(); | ||
newTilemap.OnTileChanged += OnTilemapChanged; | ||
} | ||
|
||
private void OnTilemapChanged(TileShared tile, Vector2Int location) { | ||
_camera.RenderedComponents.Remove(_renderables[location.X, location.Y].Renderable); | ||
|
||
foreach (Vector2Int dir in Adjacencies) { | ||
if (location.X + dir.X < 0 || | ||
location.X + dir.X >= _tilemap.TilemapSize.X || | ||
location.Y + dir.Y < 0 || | ||
location.Y + dir.Y >= _tilemap.TilemapSize.Y) { | ||
continue; | ||
} | ||
_renderables[location.X + dir.X, location.Y + dir.Y].UpdateAdjacencies(location + dir, _tilemap); | ||
} | ||
|
||
RenderableTile newTile = new RenderableTile(tile, _tilemap.Background); | ||
_renderables[location.X, location.Y] = newTile; | ||
newTile.UpdateAdjacencies(location, _tilemap); | ||
_camera.RenderedComponents.Add(newTile.Renderable); | ||
} | ||
|
||
private void UpdateRenderables() { | ||
_camera.RenderedComponents.Clear(); | ||
for (int x = 0; x < _tilemap.TilemapSize.X; x++) | ||
for (int y = 0; y < _tilemap.TilemapSize.Y; y++) { | ||
if (!_tilemap.TryGetTile(new Vector2Int(x, y), out TileShared? tile)) { | ||
continue; | ||
} | ||
RenderableTile newTile = new RenderableTile(tile, _tilemap.Background); | ||
_renderables[x, y] = newTile; | ||
_camera.RenderedComponents.Add(newTile.Renderable); | ||
newTile.UpdateAdjacencies(new Vector2Int(x, y), _tilemap); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,65 @@ | ||
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) => 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,35 @@ | ||
using Shared.Code.Components; | ||
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); | ||
} | ||
} |
Oops, something went wrong.