Skip to content

Commit

Permalink
fixed map, and added virtual keywords to the methods
Browse files Browse the repository at this point in the history
  • Loading branch information
joshika39 committed Nov 14, 2023
1 parent 6df3a31 commit 90ca8a0
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Nuget/.projects.conf
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
GameFramework.Core:1.5.4
GameFramework.UI.WPF:1.2.2
GameFramework.Core:1.5.5
GameFramework.UI.WPF:1.2.4
12 changes: 12 additions & 0 deletions _Tests/GameFramework.ManualTests.Desktop.WPF/GameCanvas/TestMap.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using System.Diagnostics;
using GameFramework.Configuration;
using GameFramework.Core.Factories;
using GameFramework.Impl.Map;
Expand All @@ -9,5 +11,15 @@ public class TestMap : AMap2D<TestMapSource, TestMapView>

public TestMap(TestMapSource mapSource, TestMapView view, IPositionFactory positionFactory, IConfigurationService2D configurationService2D) : base(mapSource, view, positionFactory, configurationService2D)
{ }

private void DoDebug()
{
Debug.WriteLine($"Dimension: {ConfigurationService2D.Dimension}");
Debug.WriteLine("MapObjects:");
foreach (var mapObject2D in MapObjects)
{
Debug.WriteLine(PositionFactory.CreatePosition(mapObject2D.Position.X, mapObject2D.Position.Y));
}
}
}
}
66 changes: 32 additions & 34 deletions _src/GameFramework.Impl/Map/AMap2D.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
using GameFramework.Configuration;
using GameFramework.Core;
using GameFramework.Core.Factories;
using GameFramework.Core.Motion;
using GameFramework.Core.Position;
Expand All @@ -16,8 +15,8 @@ public abstract class AMap2D<TSource, TView> : IMap2D<TSource, TView>, IMouseHan
where TSource : class, IMapSource2D
where TView : class, IMapView2D
{
private readonly IPositionFactory _positionFactory;
private readonly IConfigurationService2D _configurationService2D;
protected readonly IPositionFactory PositionFactory;
protected readonly IConfigurationService2D ConfigurationService2D;
private readonly ObservableCollection<IUnit2D> _units;
private readonly ObservableCollection<IMapObject2D> _mapObjects;

Expand All @@ -26,14 +25,14 @@ public abstract class AMap2D<TSource, TView> : IMap2D<TSource, TView>, IMouseHan
public int SizeX { get; }
public int SizeY { get; }
public ICollection<IUnit2D> Units => _units;
public IEnumerable<IUnit2D> SelectedUnits { get; }
public IEnumerable<IMapObject2D> MapObjects => _mapObjects;
public virtual IEnumerable<IUnit2D> SelectedUnits { get; }
public ICollection<IMapObject2D> MapObjects => _mapObjects;
public IMapObject2D? SelectedObject { get; set; }

protected AMap2D(TSource mapSource, TView view, IPositionFactory positionFactory, IConfigurationService2D configurationService2D)
{
_positionFactory = positionFactory ?? throw new ArgumentNullException(nameof(positionFactory));
_configurationService2D = configurationService2D ?? throw new ArgumentNullException(nameof(configurationService2D));
PositionFactory = positionFactory ?? throw new ArgumentNullException(nameof(positionFactory));
ConfigurationService2D = configurationService2D ?? throw new ArgumentNullException(nameof(configurationService2D));
MapSource = mapSource ?? throw new ArgumentNullException(nameof(mapSource));
View = view ?? throw new ArgumentNullException(nameof(view));
SizeX = MapSource.ColumnCount;
Expand All @@ -50,7 +49,7 @@ protected AMap2D(TSource mapSource, TView view, IPositionFactory positionFactory
}

#region MapObjects
public IEnumerable<IMapObject2D> MapPortion(IPosition2D topLeft, IPosition2D bottomRight)
public virtual IEnumerable<IMapObject2D> MapPortion(IPosition2D topLeft, IPosition2D bottomRight)
{
var objects = MapObjects.ToArray();
for (var y = topLeft.Y; y <= bottomRight.Y; y++)
Expand All @@ -62,21 +61,20 @@ public IEnumerable<IMapObject2D> MapPortion(IPosition2D topLeft, IPosition2D bot
}
}

public IEnumerable<IMapObject2D> MapPortion(IPosition2D center, int radius)
public virtual IEnumerable<IMapObject2D> MapPortion(IPosition2D center, int radius)
{
var top = center.Y - radius < 0 ? 0 : center.Y - radius;
var bottom = center.Y + radius >= SizeY ? SizeY - 1 : center.Y + radius;
var left = center.X - radius < 0 ? 0 : center.X - radius;
var right = center.X + radius >= SizeX ? SizeX - 1 : center.X + radius;
var topLeftPos = _positionFactory.CreatePosition(left, top);
var bottomRightPos = _positionFactory.CreatePosition(right, bottom);
var topLeftPos = PositionFactory.CreatePosition(left, top);
var bottomRightPos = PositionFactory.CreatePosition(right, bottom);
return MapPortion(topLeftPos, bottomRightPos);
}
#endregion



#region Units
public IEnumerable<IUnit2D> GetUnitsAtPortion(IEnumerable<IMapObject2D> mapObjects)
public virtual IEnumerable<IUnit2D> GetUnitsAtPortion(IEnumerable<IMapObject2D> mapObjects)
{
var units = new List<IUnit2D>();
foreach (var mapObject in mapObjects)
Expand All @@ -87,23 +85,23 @@ public IEnumerable<IUnit2D> GetUnitsAtPortion(IEnumerable<IMapObject2D> mapObjec
return units;
}

public IEnumerable<IUnit2D> GetUnitsAtPortion(IPosition2D topLeft, IPosition2D bottomRight)
public virtual IEnumerable<IUnit2D> GetUnitsAtPortion(IPosition2D topLeft, IPosition2D bottomRight)
{
var portion = MapPortion(topLeft, bottomRight);
return GetUnitsAtPortion(portion);
}

public IEnumerable<TUnit> GetUnitsOfTypeAtPortion<TUnit>(IEnumerable<IMapObject2D> mapObjects) where TUnit : IUnit2D
public virtual IEnumerable<TUnit> GetUnitsOfTypeAtPortion<TUnit>(IEnumerable<IMapObject2D> mapObjects) where TUnit : IUnit2D
{
throw new NotImplementedException();
}

public IEnumerable<TUnit> GetAllUnitsOfType<TUnit>() where TUnit : IUnit2D
public virtual IEnumerable<TUnit> GetAllUnitsOfType<TUnit>() where TUnit : IUnit2D
{
throw new NotImplementedException();
}

public TUnit? GetUnit<TUnit>(Guid id) where TUnit : IUnit2D
public virtual TUnit? GetUnit<TUnit>(Guid id) where TUnit : IUnit2D
{
foreach (var unit in Units)
{
Expand All @@ -116,7 +114,7 @@ public IEnumerable<TUnit> GetAllUnitsOfType<TUnit>() where TUnit : IUnit2D
return default;
}

public IUnit2D? GetUnit(Guid id)
public virtual IUnit2D? GetUnit(Guid id)
{
return GetUnit<IUnit2D>(id);
}
Expand All @@ -131,7 +129,7 @@ public virtual void MoveUnit(IUnit2D unit2D, Move2D move)
unit2D.Step(mapObject);
}

public IMapObject2D? SimulateMove(IPosition2D position, Move2D move)
public virtual IMapObject2D? SimulateMove(IPosition2D position, Move2D move)
{
var objects = MapObjects.ToArray();
switch (move)
Expand Down Expand Up @@ -165,20 +163,20 @@ public virtual void MoveUnit(IUnit2D unit2D, Move2D move)
return default;
}

public void RegisterUnit(IUnit2D unit2D)
public virtual void RegisterUnit(IUnit2D unit2D)
{
Units.Add(unit2D);
View.EntityViews = new ObservableCollection<IDynamicMapObjectView>(Units.Select(u => u.View));
}
#endregion

public void SaveProgress()
public virtual void SaveProgress()
{
MapSource.SaveLayout(MapObjects, Units);
}

#region MouseMove
public void OnMouseMove(IScreenSpacePosition screenSpacePosition)
public virtual void OnMouseMove(IScreenSpacePosition screenSpacePosition)
{
MouseMoveOnUnit(screenSpacePosition);
MouseMoveOnMapObject(screenSpacePosition);
Expand All @@ -194,8 +192,8 @@ private void MouseMoveOnUnit(IScreenSpacePosition screenSpacePosition)
}

var pos = unit.ScreenSpacePosition;
if (screenSpacePosition.X >= pos.X && screenSpacePosition.X <= pos.X + _configurationService2D.Dimension &&
screenSpacePosition.Y >= pos.Y && screenSpacePosition.Y <= pos.Y + _configurationService2D.Dimension)
if (screenSpacePosition.X >= pos.X && screenSpacePosition.X <= pos.X + ConfigurationService2D.Dimension &&
screenSpacePosition.Y >= pos.Y && screenSpacePosition.Y <= pos.Y + ConfigurationService2D.Dimension)
{
hoverableUnit.OnHovered();
continue;
Expand All @@ -217,8 +215,8 @@ private void MouseMoveOnMapObject(IScreenSpacePosition screenSpacePosition)
continue;
}
var pos = mapObject.ScreenSpacePosition;
if (screenSpacePosition.X >= pos.X && screenSpacePosition.X <= pos.X + _configurationService2D.Dimension &&
screenSpacePosition.Y >= pos.Y && screenSpacePosition.Y <= pos.Y + _configurationService2D.Dimension)
if (screenSpacePosition.X >= pos.X && screenSpacePosition.X <= pos.X + ConfigurationService2D.Dimension &&
screenSpacePosition.Y >= pos.Y && screenSpacePosition.Y <= pos.Y + ConfigurationService2D.Dimension)
{
hoverableMapObject.OnHovered();
continue;
Expand All @@ -234,7 +232,7 @@ private void MouseMoveOnMapObject(IScreenSpacePosition screenSpacePosition)

#region LeftClick

public void OnMouseLeftClick(IScreenSpacePosition screenSpacePosition)
public virtual void OnMouseLeftClick(IScreenSpacePosition screenSpacePosition)
{
if (ClickUnit(screenSpacePosition))
{
Expand All @@ -254,8 +252,8 @@ private bool ClickUnit(IScreenSpacePosition screenSpacePosition)
}

var pos = unit.ScreenSpacePosition;
if (screenSpacePosition.X >= pos.X && screenSpacePosition.X <= pos.X + _configurationService2D.Dimension &&
screenSpacePosition.Y >= pos.Y && screenSpacePosition.Y <= pos.Y + _configurationService2D.Dimension)
if (screenSpacePosition.X >= pos.X && screenSpacePosition.X <= pos.X + ConfigurationService2D.Dimension &&
screenSpacePosition.Y >= pos.Y && screenSpacePosition.Y <= pos.Y + ConfigurationService2D.Dimension)
{
if (unit.Position == SelectedObject?.Position)
{
Expand Down Expand Up @@ -291,8 +289,8 @@ private void ClickMapObject(IScreenSpacePosition screenSpacePosition)
}

var pos = mapObject.ScreenSpacePosition;
if (screenSpacePosition.X >= pos.X && screenSpacePosition.X <= pos.X + _configurationService2D.Dimension &&
screenSpacePosition.Y >= pos.Y && screenSpacePosition.Y <= pos.Y + _configurationService2D.Dimension)
if (screenSpacePosition.X >= pos.X && screenSpacePosition.X <= pos.X + ConfigurationService2D.Dimension &&
screenSpacePosition.Y >= pos.Y && screenSpacePosition.Y <= pos.Y + ConfigurationService2D.Dimension)
{
if (mapObject == SelectedObject)
{
Expand All @@ -318,12 +316,12 @@ private void ClickMapObject(IScreenSpacePosition screenSpacePosition)

#endregion

public void OnMouseRightClick()
public virtual void OnMouseRightClick()
{
throw new NotImplementedException();
}

public void OnMouseLeftDoubleClick()
public virtual void OnMouseLeftDoubleClick()
{
throw new NotImplementedException();
}
Expand Down
2 changes: 1 addition & 1 deletion _src/GameFramework.UI.WPF/Map/WpfMapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void UpdateEntities()
{
foreach (var entityView in EntityViews)
{
if (entityView is Shape shape)
if (entityView is Shape shape && !Children.Contains(shape))
{
Children.Add(shape);
}
Expand Down
4 changes: 2 additions & 2 deletions _src/GameFramework/Map/IMap2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace GameFramework.Map
{
public interface IMap2D<TSource, TView> : IHasUnits2D
public interface IMap2D<out TSource, out TView> : IHasUnits2D
where TSource : IMapSource2D
where TView : IMapView2D
{
Expand All @@ -15,7 +15,7 @@ public interface IMap2D<TSource, TView> : IHasUnits2D
int SizeX { get; }
int SizeY { get; }

IEnumerable<IMapObject2D> MapObjects { get; }
ICollection<IMapObject2D> MapObjects { get; }
IEnumerable<IMapObject2D> MapPortion(IPosition2D topLeft, IPosition2D bottomRight);
IEnumerable<IMapObject2D> MapPortion(IPosition2D center, int radius);
IMapObject2D? SelectedObject { get; set; }
Expand Down

0 comments on commit 90ca8a0

Please sign in to comment.