Skip to content

Commit

Permalink
General Improvements, and started working on Physics Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
Papishushi committed Dec 28, 2023
1 parent 47f21a8 commit f22aca7
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
* A typeless collection used for pooling objects and reusing them.
*/

using ScapeCore.Core.Batching.Tools;
using Serilog;
using System;
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Linq;

namespace ScapeCore.Core.Batching.Tools
namespace ScapeCore.Core.Collections.Pooling
{
public sealed class ObjectPool : IDisposable
{
Expand Down
6 changes: 5 additions & 1 deletion Core/Core.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Batching\Tools\DeeplyMutableType.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Batching\Tools\DeeplyMutable.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Batching\Events\LoadBatchEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Batching\Tools\ObjectPool.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Collections\Pooling\ObjectPool.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Batching\Tools\ReflectiveEnumerator.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Batching\Events\RenderBatchEventArgs.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Batching\Resources\ResourceLoadAttribute.cs" />
Expand All @@ -35,7 +35,10 @@
<Compile Include="$(MSBuildThisFileDirectory)Engine\MonoBehaviour.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Engine\Tilemaps\TileBase.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Engine\Tilemaps\TileRenderer.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FPSMetric.cs" />
<Compile Include="$(MSBuildThisFileDirectory)LLAM.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Physics2D\Collider2D.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Physics2D\RigidBody2D.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SceneManagement\Scene.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SceneManagement\SceneManager.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Serialization\Streamers\GZipStreamer.cs" />
Expand All @@ -50,6 +53,7 @@
<Folder Include="$(MSBuildThisFileDirectory)Batching\Events\" />
<Folder Include="$(MSBuildThisFileDirectory)Batching\Resources\" />
<Folder Include="$(MSBuildThisFileDirectory)Collections\Merkle\" />
<Folder Include="$(MSBuildThisFileDirectory)Collections\Pooling\" />
<Folder Include="$(MSBuildThisFileDirectory)Serialization\Streamers\" />
<Folder Include="$(MSBuildThisFileDirectory)Serialization\Tools\" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Core/Engine/Components/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
using Microsoft.Xna.Framework.Graphics;
using ScapeCore.Core.Batching.Events;
using ScapeCore.Core.Batching.Resources;
using ScapeCore.Targets;
using Serilog;
using System.Text;

namespace ScapeCore.Core.Engine.Components
Expand Down
3 changes: 3 additions & 0 deletions Core/Engine/Components/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class Transform : Component
public Vector2 _rotation = Vector2.Zero;
public Vector2 _scale = Vector2.One;

public Vector2 LocalPosition { get => _position; set => _position = value; }
public Vector2 LocalRotation { get => _rotation; set => _rotation = value; }
public Vector2 LocalScale { get => _scale; set => _scale = value; }
public Vector2 Position { get => (gameObject?.parent?.transform?.Position ?? Vector2.Zero) + _position; set => _position = value; }
public Vector2 Rotation { get => (gameObject?.parent?.transform?.Rotation ?? Vector2.Zero) + _rotation; set => _rotation = value; }
public Vector2 Scale { get => (gameObject?.parent?.transform?.Scale ?? Vector2.One) * _scale; set => _scale = value; }
Expand Down
56 changes: 15 additions & 41 deletions Core/Engine/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,8 @@ public IEnumerator<T> GetBehaviours<T>() where T : Behaviour
}
var temp = new T();
behaviours.Add(temp);
if (typeof(Component).IsAssignableFrom(temp.GetType()))
temp.To<Component>().gameObject = this;
if (typeof(MonoBehaviour).IsAssignableFrom(temp.GetType()))
temp.To<MonoBehaviour>().gameObject = this;
if (typeof(IEntityComponentModel).IsAssignableFrom(temp.GetType()))
((IEntityComponentModel)temp).gameObject = this;
return temp;
}
public T? AddBehaviour<T>(T behaviour) where T : Behaviour
Expand All @@ -137,10 +135,8 @@ public IEnumerator<T> GetBehaviours<T>() where T : Behaviour
}
if (behaviour == null) return null;
behaviours.Add(behaviour);
if (typeof(Component).IsAssignableFrom(behaviour.GetType()))
behaviour.To<Component>().gameObject = this;
if (typeof(MonoBehaviour).IsAssignableFrom(behaviour.GetType()))
behaviour.To<MonoBehaviour>().gameObject = this;
if (typeof(IEntityComponentModel).IsAssignableFrom(behaviour.GetType()))
((IEntityComponentModel)behaviour).gameObject = this;
return behaviour;
}

Expand All @@ -161,10 +157,8 @@ public IEnumerator<T> GetBehaviours<T>() where T : Behaviour
{
if (behaviour == null) continue;
this.behaviours.Add(behaviour);
if (typeof(Component).IsAssignableFrom(behaviour.GetType()))
behaviour.To<Component>().gameObject = this;
if (typeof(MonoBehaviour).IsAssignableFrom(behaviour.GetType()))
behaviour.To<MonoBehaviour>().gameObject = this;
if (typeof(IEntityComponentModel).IsAssignableFrom(behaviour.GetType()))
((IEntityComponentModel)behaviour).gameObject = this;
l.Add(behaviour);
}
return l.GetEnumerator();
Expand All @@ -184,10 +178,8 @@ public IEnumerator<T> GetBehaviours<T>() where T : Behaviour
T? temp = behaviours.Find(x => x.GetType() == typeof(T))?.To<T>();
if (temp == null) return null;
behaviours.Remove(temp);
if (typeof(Component).IsAssignableFrom(temp.GetType()))
temp.To<Component>().gameObject = null;
if (typeof(MonoBehaviour).IsAssignableFrom(temp.GetType()))
temp.To<MonoBehaviour>().gameObject = null;
if (typeof(IEntityComponentModel).IsAssignableFrom(temp.GetType()))
((IEntityComponentModel)temp).gameObject = null;
return temp;
}

Expand All @@ -204,10 +196,8 @@ public IEnumerator<T> GetBehaviours<T>() where T : Behaviour
}
if (behaviour == null) return null;
behaviours.Remove(behaviour);
if (typeof(Component).IsAssignableFrom(behaviour.GetType()))
behaviour.To<Component>().gameObject = null;
if (typeof(MonoBehaviour).IsAssignableFrom(behaviour.GetType()))
behaviour.To<MonoBehaviour>().gameObject = null;
if (typeof(IEntityComponentModel).IsAssignableFrom(behaviour.GetType()))
((IEntityComponentModel)behaviour).gameObject = null;
return behaviour;
}

Expand All @@ -227,10 +217,8 @@ public IEnumerator<T> RemoveBehaviours<T>() where T : Behaviour
{
if (behaviour == null) continue;
behaviours.Remove(behaviour);
if (typeof(Component).IsAssignableFrom(behaviour.GetType()))
behaviour.To<Component>().gameObject = null;
if (typeof(MonoBehaviour).IsAssignableFrom(behaviour.GetType()))
behaviour.To<MonoBehaviour>().gameObject = null;
if (typeof(IEntityComponentModel).IsAssignableFrom(behaviour.GetType()))
((IEntityComponentModel)behaviour).gameObject = null;
l.Add(behaviour);
}
return l.GetEnumerator();
Expand All @@ -253,30 +241,16 @@ public IEnumerator<T> RemoveBehaviours<T>(params T[] behaviours) where T : Behav
if (behaviour == null) continue;
if (!behaviours.Contains(behaviour)) continue;
this.behaviours.Remove(behaviour);
if (typeof(Component).IsAssignableFrom(behaviour.GetType()))
behaviour.To<Component>().gameObject = null;
if (typeof(MonoBehaviour).IsAssignableFrom(behaviour.GetType()))
behaviour.To<MonoBehaviour>().gameObject = null;
if (typeof(IEntityComponentModel).IsAssignableFrom(behaviour.GetType()))
((IEntityComponentModel)behaviour).gameObject = null;
l.Add(behaviour);
}
return l.GetEnumerator();
}

protected override void OnCreate()
{
if (SceneManager.CurrentScene.TryGetTarget(out var scene))
scene.GameObjects.Add(this);
else
{
var i = SceneManager.AddScene(new Scene("Scene", 0));
if (i == -1)
{
Log.Warning("{Ga} wasn't correctly created. There was a problem adding it to current scene or to a new one.", nameof(GameObject));
return;
}
var currentScene = SceneManager.Get(i);
currentScene!.GameObjects.Add(this);
}

}

protected override void OnDestroy()
Expand Down
13 changes: 0 additions & 13 deletions Core/Engine/MonoBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ protected override void OnCreate()
Log.Warning("{Mo} wasn't correctly created. {LLAM} instance is GCed.", nameof(MonoBehaviour), typeof(LLAM).FullName);
return;
}
if (SceneManager.CurrentScene.TryGetTarget(out var scene))
scene.MonoBehaviours.Add(this);
else
{
var i = SceneManager.AddScene(new("Scene", 0));
if (i == -1)
{
Log.Warning("{Mo} wasn't correctly created. There was a problem adding it to current scene or creating a new one.", nameof(MonoBehaviour));
return;
}
var currentScene = SceneManager.Get(i);
currentScene!.MonoBehaviours.Add(this);
}
Game.OnStart += StartWrapper;
Game.OnUpdate += UpdateWrapper;
}
Expand Down
46 changes: 46 additions & 0 deletions Core/FPSMetric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* -*- encoding: utf-8 with BOM -*-
* .▄▄ · ▄▄· ▄▄▄· ▄▄▄·▄▄▄ . ▄▄· ▄▄▄ ▄▄▄ .
* ▐█ ▀. ▐█ ▌▪▐█ ▀█ ▐█ ▄█▀▄.▀· ▐█ ▌▪▪ ▀▄ █·▀▄.▀·
* ▄▀▀▀█▄██ ▄▄▄█▀▀█ ██▀·▐▀▀▪▄ ██ ▄▄ ▄█▀▄ ▐▀▀▄ ▐▀▀▪▄
* ▐█▄▪▐█▐███▌▐█ ▪▐▌▐█▪·•▐█▄▄▌ ▐███▌▐█▌.▐▌▐█•█▌▐█▄▄▌
* ▀▀▀▀ ·▀▀▀ ▀ ▀ .▀ ▀▀▀ ·▀▀▀ ▀█▄▀▪.▀ ▀ ▀▀▀
* https://github.com/Papishushi/ScapeCore
*
* Copyright (c) 2023 Daniel Molinero Lucas
* This file is subject to the terms and conditions defined in
* file 'LICENSE.txt', which is part of this source code package.
*
* FPSMetric.cs
* You can use this class to check the current FPS of your game.
*/

using System;

namespace ScapeCore.Targets
{
public sealed class FPSMetric
{
private static int _fps;
private static int _framesSumatory;
private static TimeSpan _fpsStartTime;
private static TimeSpan _fpsEndTime;

public static int FPS { get => _fps; }

internal FPSMetric()
{
if (_fpsStartTime == TimeSpan.Zero)
_fpsStartTime = DateTime.Now.TimeOfDay;
_framesSumatory++;
_fpsEndTime = DateTime.Now.TimeOfDay;
if (_fpsEndTime.Seconds == _fpsStartTime.Seconds + 1)
{
_fpsStartTime = TimeSpan.Zero;
_fpsEndTime = TimeSpan.Zero;
_fps = _framesSumatory;
_framesSumatory = 0;
}
}
}
}
11 changes: 8 additions & 3 deletions Core/LLAM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

namespace ScapeCore.Targets
{

//Low Level Automation Module
public class LLAM : Game
{
Expand All @@ -55,6 +56,8 @@ public class LLAM : Game
public GraphicsDeviceManager Graphics { get => _graphics; }
public SpriteBatch? SpriteBatch { get => _spriteBatch; }
public static WeakReference<LLAM?> Instance { get; private set; }
private GameTime _time;
public GameTime Time { get => _time; }

internal event UpdateBatchEventHandler? OnUpdate;
internal event StartBatchEventHandler? OnStart;
Expand Down Expand Up @@ -107,7 +110,7 @@ public LLAM()
_graphics = new(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
IsFixedTimeStep = true;
IsFixedTimeStep = false;
}

protected override void Initialize()
Expand All @@ -133,14 +136,13 @@ protected override void Update(GameTime gameTime)
//Debug.WriteLine("Update...");
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();

_time = gameTime;
// TODO: Add your update logic here
OnStart?.Invoke(this, new(string.Empty));
Log.Verbose("{{{@source}}}\t{@args}", GetHashCode(), $"Start cycle number\t{_si++}\t|\tPatch size\t{OnStart?.GetInvocationList().Length ?? 0}");
OnStart = null;
OnUpdate?.Invoke(this, new(gameTime, string.Empty));
Log.Verbose("{{{@source}}}\t{@args}", GetHashCode(), $"Update cycle number\t{_ui++}\t|\tPatch size\t{OnUpdate?.GetInvocationList().Length ?? 0}");

base.Update(gameTime);
}

Expand All @@ -149,6 +151,9 @@ protected override void Draw(GameTime gameTime)
//Debug.WriteLine("Draw...");
GraphicsDevice.Clear(Color.CornflowerBlue);

_ = new FPSMetric();
//Log.Debug("{fps}", FPSMetric.FPS);

//Render Patches
_spriteBatch!.Begin();
OnRender?.Invoke(this, new(gameTime, string.Empty));
Expand Down
81 changes: 81 additions & 0 deletions Core/Physics2D/Collider2D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* -*- encoding: utf-8 with BOM -*-
* .▄▄ · ▄▄· ▄▄▄· ▄▄▄·▄▄▄ . ▄▄· ▄▄▄ ▄▄▄ .
* ▐█ ▀. ▐█ ▌▪▐█ ▀█ ▐█ ▄█▀▄.▀· ▐█ ▌▪▪ ▀▄ █·▀▄.▀·
* ▄▀▀▀█▄██ ▄▄▄█▀▀█ ██▀·▐▀▀▪▄ ██ ▄▄ ▄█▀▄ ▐▀▀▄ ▐▀▀▪▄
* ▐█▄▪▐█▐███▌▐█ ▪▐▌▐█▪·•▐█▄▄▌ ▐███▌▐█▌.▐▌▐█•█▌▐█▄▄▌
* ▀▀▀▀ ·▀▀▀ ▀ ▀ .▀ ▀▀▀ ·▀▀▀ ▀█▄▀▪.▀ ▀ ▀▀▀
* https://github.com/Papishushi/ScapeCore
*
* Copyright (c) 2023 Daniel Molinero Lucas
* This file is subject to the terms and conditions defined in
* file 'LICENSE.txt', which is part of this source code package.
*
* Collider2D.cs
* Represent a 2D Collider with a size and an attached RigidBody.
*/

using Microsoft.Xna.Framework;
using ScapeCore.Core.Engine;
using ScapeCore.Core.Engine.Components;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ScapeCore.Core.Physics2D
{
public class Collider2D : Component
{
protected static readonly List<Collider2D> _colliders = new();

public Vector2 size;
public RigidBody2D? attachedRigidbody;

public Collider2D()
{
_colliders.Add(this);
size = new Vector2(10,10);
attachedRigidbody = null;
}

public Collider2D(Vector2 size, RigidBody2D? attachedRigidbody)
{
_colliders.Add(this);
this.size = size;
this.attachedRigidbody = attachedRigidbody;
}

internal List<Collider2D> IsIntersecting()
{
List<Collider2D> result = new List<Collider2D>();
foreach (var collider in _colliders)
{
if (collider == this) continue;
var rb = collider.attachedRigidbody;
if (rb == null || rb.transform == null || transform == null)
continue;

float deltaX = Math.Abs(transform.Position.X - rb.transform.Position.X);
float deltaY = Math.Abs(transform.Position.Y - rb.transform.Position.Y);

float intersectX = (size.X + collider.size.X) * 0.5f;
float intersectY = (size.Y + collider.size.Y) * 0.5f;

if (deltaX < intersectX && deltaY < intersectY)
{
// There is an intersection
result.Add(collider);
}
}

return result;
}

protected override void OnDestroy()
{
_colliders.Remove(this);
base.OnDestroy();
}
}
}
Loading

0 comments on commit f22aca7

Please sign in to comment.