chore: Added Initial Engine Code

This commit is contained in:
Syntriax 2023-11-23 22:07:49 +03:00
parent a47586851c
commit 5fcf63c5a7
40 changed files with 1773 additions and 0 deletions

484
Engine.Core/.gitignore vendored Normal file
View File

@ -0,0 +1,484 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from `dotnet new gitignore`
# dotenv files
.env
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET
project.lock.json
project.fragment.lock.json
artifacts/
# Tye
.tye/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
*.ncb
*.aps
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp
# JetBrains Rider
*.sln.iml
.idea
##
## Visual studio for Mac
##
# globs
Makefile.in
*.userprefs
*.usertasks
config.make
config.status
aclocal.m4
install-sh
autom4te.cache/
*.tar.gz
tarballs/
test-results/
# Mac bundle stuff
*.dmg
*.app
# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
# Vim temporary swap files
*.swp

View File

@ -0,0 +1,6 @@
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the class implementing it has Assignable fields that are necessary for the engine to work properly.
/// </summary>
public interface IAssignable { }

View File

@ -0,0 +1,26 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="IBehaviourController"/> field.
/// </summary>
public interface IAssignableBehaviourController : IAssignable
{
/// <summary>
/// Callback triggered when the <see cref="IBehaviourController"/> value has has been assigned a new value.
/// </summary>
Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; }
/// <inheritdoc cref="IBehaviourController" />
IBehaviourController BehaviourController { get; }
/// <summary>
/// Assign a value to the <see cref="IBehaviourController"/> field of this object
/// </summary>
/// <param name="behaviourController">New <see cref="IBehaviourController"/> to assign.</param>
/// <returns>
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
/// </returns>
bool Assign(IBehaviourController behaviourController);
}

View File

@ -0,0 +1,26 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="IEntity"/> field.
/// </summary>
public interface IAssignableEntity : IAssignable
{
/// <summary>
/// Callback triggered when the <see cref="IEntity"/> value has has been assigned a new value.
/// </summary>
Action<IAssignableEntity>? OnEntityAssigned { get; set; }
/// <inheritdoc cref="IEntity" />
IEntity Entity { get; }
/// <summary>
/// Assign a value to the <see cref="IEntity"/> field of this object
/// </summary>
/// <param name="entity">New <see cref="IEntity"/> to assign.</param>
/// <returns>
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
/// </returns>
bool Assign(IEntity entity);
}

View File

@ -0,0 +1,26 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="IGameObject"/> field.
/// </summary>
public interface IAssignableGameObject : IAssignable
{
/// <summary>
/// Callback triggered when the <see cref="IGameObject"/> value has has been assigned a new value.
/// </summary>
Action<IAssignableGameObject>? OnGameObjectAssigned { get; set; }
/// <inheritdoc cref="IGameObject" />
IGameObject GameObject { get; }
/// <summary>
/// Assign a value to the <see cref="IGameObject"/> field of this object
/// </summary>
/// <param name="gameObject">New <see cref="IGameObject"/> to assign.</param>
/// <returns>
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
/// </returns>
bool Assign(IGameObject gameObject);
}

View File

@ -0,0 +1,26 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="ISprite"/> field.
/// </summary>
public interface IAssignableSprite : IAssignable
{
/// <summary>
/// Callback triggered when the <see cref="ISprite"/> value has has been assigned a new value.
/// </summary>
Action<IAssignableSprite>? OnSpriteAssigned { get; set; }
/// <inheritdoc cref="ISprite" />
ISprite Sprite { get; }
/// <summary>
/// Assign a value to the <see cref="ISprite"/> field of this object
/// </summary>
/// <param name="sprite">New <see cref="ISprite"/> to assign.</param>
/// <returns>
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
/// </returns>
bool Assign(ISprite sprite);
}

View File

@ -0,0 +1,26 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="IStateEnable"/> field.
/// </summary>
public interface IAssignableStateEnable : IAssignable
{
/// <summary>
/// Callback triggered when the <see cref="IStateEnable"/> value has has been assigned a new value.
/// </summary>
Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; }
/// <inheritdoc cref="IStateEnable" />
IStateEnable StateEnable { get; }
/// <summary>
/// Assign a value to the <see cref="IStateEnable"/> field of this object
/// </summary>
/// <param name="stateEnable">New <see cref="IStateEnable"/> to assign.</param>
/// <returns>
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
/// </returns>
bool Assign(IStateEnable stateEnable);
}

View File

@ -0,0 +1,26 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Indicates the object is an <see cref="IAssignable"/> with an assignable <see cref="ITransform"/> field.
/// </summary>
public interface IAssignableTransform : IAssignable
{
/// <summary>
/// Callback triggered when the <see cref="ITransform"/> value has has been assigned a new value.
/// </summary>
Action<IAssignableTransform>? OnTransformAssigned { get; set; }
/// <inheritdoc cref="ITransform" />
ITransform Transform { get; }
/// <summary>
/// Assign a value to the <see cref="ITransform"/> field of this object
/// </summary>
/// <param name="transform">New <see cref="ITransform"/> to assign.</param>
/// <returns>
/// <see cref="true"/>, if the value given assigned successfully assigned, <see cref="false"/> if not.
/// </returns>
bool Assign(ITransform transform);
}

View File

@ -0,0 +1,19 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Responsible for every behaviour an object in the game might have, controlled by <see cref="IBehaviourController"/>.
/// </summary>
public interface IBehaviour : IEntity, IAssignableBehaviourController, IAssignableStateEnable, IInitialize
{
/// <summary>
/// Callback triggered when the <see cref="Priority"/> has changed.
/// </summary>
Action<IBehaviour>? OnPriorityChanged { get; set; }
/// <summary>
/// Call priority of the <see cref="IBehaviour"/>.
/// </summary>
int Priority { get; set; }
}

View File

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Xna.Framework;
namespace Syntriax.Engine.Core.Abstract;
/// <summary>
/// Responsible for controlling <see cref="IBehaviour"/>s and notify them accordingly about the engine's updates. Connected to an <see cref="IGameObject"/>.
/// </summary>
public interface IBehaviourController : IAssignableGameObject
{
/// <summary>
/// Callback triggered when the <see cref="Update(GameTime)"/> is called.
/// </summary>
Action<IBehaviourController, GameTime>? OnUpdate { get; set; }
/// <summary>
/// Callback triggered when the <see cref="OnPreDraw(GameTime)"/> is called.
/// </summary>
Action<IBehaviourController, GameTime>? OnPreDraw { get; set; }
/// <summary>
/// Callback triggered when the <see cref="IBehaviourController"/> has been registered a new <see cref="IBehaviour"/>.
/// </summary>
Action<IBehaviourController, IBehaviour>? OnBehaviourAdded { get; set; }
/// <summary>
/// Callback triggered when the <see cref="IBehaviourController"/> has been removed an existing <see cref="IBehaviour"/>.
/// </summary>
Action<IBehaviourController, IBehaviour>? OnBehaviourRemoved { get; set; }
/// <summary>
/// Registers the provided <see cref="IBehaviour"/> to be controlled by the <see cref="IBehaviourController"/>.
/// </summary>
/// <param name="behaviour">Uninitialized <see cref="IBehaviour"/> to be registered.</param>
/// <typeparam name="T">An implemented class of <see cref="IBehaviour"/></typeparam>
/// <returns>The provided <see cref="IBehaviour"/> class after initialization.</returns>
T AddBehaviour<T>(T behaviour) where T : class, IBehaviour;
/// <summary>
/// Instantiates the provided <see cref="IBehaviour"/> type and registers it to the <see cref="IBehaviourController"/>.
/// </summary>
/// <param name="args">Constructor parameters for the given <see cref="IBehaviour"/> class.</param>
/// <typeparam name="T">An implemented class of <see cref="IBehaviour"/></typeparam>
/// <returns>The instantiated <see cref="IBehaviour"/> class after initialization.</returns>
T AddBehaviour<T>(params object?[]? args) where T : class, IBehaviour;
/// <summary>
/// Looks up and tries to get the <see cref="IBehaviour"/> that is controlled by the <see cref="IBehaviourController"/>.
/// </summary>
/// <param name="behaviour">If return value is <see cref="true"/> outputs the class found in the <see cref="IBehaviourController"/>. If the return value is falls, this parameter is <see cref="null"/></param>
/// <typeparam name="T">An implemented class or <see cref="interface"/></typeparam>
/// <returns>
/// <see cref="true"/>, if the type of <see cref="IBehaviour"/> is present in the <see cref="IBehaviourController"/>, <see cref="false"/> if not.
/// </returns>
bool TryGetBehaviour<T>([NotNullWhen(returnValue: true)] out T? behaviour);
/// <typeparam name="T">An implemented class or <see cref="interface"/> of <see cref="IBehaviour"/></typeparam>
/// <returns>Returns a list of all the matching <see cref="IBehaviour"/>s found in the <see cref="IBehaviourController"/>.</returns>
IList<T> GetBehaviours<T>() where T : IBehaviour;
/// <summary>
/// Removes the <see cref="IBehaviour"/> found in the <see cref="IBehaviourController"/>.
/// </summary>
/// <param name="removeAll">If all of the instances of the given Type is to be removed or not.</param>
/// <typeparam name="T">An implemented class or <see cref="interface"/> of <see cref="IBehaviour"/></typeparam>
void RemoveBehaviour<T>(bool removeAll = false) where T : IBehaviour;
/// <summary>
/// To be called in every frame of the engine. Responsible for notifying <see cref="IBehaviour"/>'s under the <see cref="IBehaviourController"/>'s control that a new frame is happening.
/// </summary>
/// <param name="gameTime"><see cref="GameTime"/> information from the game.</param>
void Update(GameTime gameTime);
/// <summary>
/// To be called before every draw call from the engine. Responsible for notifying <see cref="IBehaviour"/>'s under the <see cref="IBehaviourController"/>'s control that the engine is about to start drawing into the screen.
/// </summary>
/// <param name="gameTime"><see cref="GameTime"/> information from the game.</param>
void UpdatePreDraw(GameTime gameTime);
}

View File

@ -0,0 +1,5 @@
namespace Syntriax.Engine.Core.Abstract;
public interface IEntity : IAssignableStateEnable
{
}

View File

@ -0,0 +1,12 @@
using System;
using Microsoft.Xna.Framework;
namespace Syntriax.Engine.Core.Abstract;
public interface IGameObject : IEntity, IAssignableTransform, IAssignableBehaviourController, INameable, IInitialize
{
Action<IGameObject, GameTime>? OnUpdated { get; set; }
void Update(GameTime time);
}

View File

@ -0,0 +1,13 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
public interface IInitialize : IEntity
{
Action<IInitialize>? OnInitialized { get; set; }
Action<IInitialize>? OnFinalized { get; set; }
bool Initialized { get; }
bool Initialize();
bool Finalize();
}

View File

@ -0,0 +1,9 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
public interface INameable
{
Action<IEntity>? OnNameChanged { get; set; }
string Name { get; set; }
}

View File

@ -0,0 +1,17 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Syntriax.Engine.Core.Abstract;
// TODO Probably gonna have to rethink this
public interface ISprite
{
Action<ISprite>? OnTextureChanged { get; set; }
Action<ISprite>? OnColorChanged { get; set; }
Action<ISprite>? OnDepthChanged { get; set; }
Texture2D Texture2D { get; set; }
Color Color { get; set; }
float Depth { get; set; }
}

View File

@ -0,0 +1,9 @@
using System;
namespace Syntriax.Engine.Core.Abstract;
public interface IStateEnable : IAssignableEntity
{
Action<IStateEnable>? OnEnabledChanged { get; set; }
bool Enabled { get; set; }
}

View File

@ -0,0 +1,17 @@
using System;
using Microsoft.Xna.Framework;
namespace Syntriax.Engine.Core.Abstract;
public interface ITransform
{
Action<ITransform>? OnPositionChanged { get; set; }
Action<ITransform>? OnScaleChanged { get; set; }
Action<ITransform>? OnRotationChanged { get; set; }
Vector2 Position { get; set; }
Vector2 Scale { get; set; }
float Rotation { get; set; }
}

99
Engine.Core/Behaviour.cs Normal file
View File

@ -0,0 +1,99 @@
using System;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core;
public abstract class Behaviour : IBehaviour
{
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
public Action<IInitialize>? OnInitialized { get; set; } = null;
public Action<IInitialize>? OnFinalized { get; set; } = null;
public Action<IBehaviour>? OnPriorityChanged { get; set; } = null;
private IBehaviourController _behaviourController = null!;
private IStateEnable _stateEnable = null!;
private bool _initialized = false;
private int _priority = 0;
public IStateEnable StateEnable => _stateEnable;
public IBehaviourController BehaviourController => _behaviourController;
public bool Initialized
{
get => _initialized;
private set
{
if (value == _initialized)
return;
_initialized = value;
if (value)
OnInitialized?.Invoke(this);
else
OnFinalized?.Invoke(this);
}
}
public int Priority
{
get => _priority;
set
{
if (value == _priority)
return;
_priority = value;
OnPriorityChanged?.Invoke(this);
}
}
public bool Assign(IStateEnable stateEnable)
{
if (_initialized)
return false;
_stateEnable = stateEnable;
_stateEnable.Assign(this);
OnStateEnableAssigned?.Invoke(this);
return true;
}
public bool Assign(IBehaviourController behaviourController)
{
if (_behaviourController is not null)
return false;
_behaviourController = behaviourController;
OnBehaviourControllerAssigned?.Invoke(this);
return true;
}
public bool Initialize()
{
if (Initialized)
return false;
NotAssignedException.Check(this, _behaviourController);
NotAssignedException.Check(this, _stateEnable);
Initialized = true;
return true;
}
public bool Finalize()
{
if (!Initialized)
return false;
_behaviourController = null!;
_stateEnable = null!;
Initialized = false;
return true;
}
}

View File

@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public class BehaviourController : IBehaviourController
{
public Action<IBehaviourController, GameTime>? OnUpdate { get; set; } = null;
public Action<IBehaviourController, GameTime>? OnPreDraw { get; set; } = null;
public Action<IBehaviourController, IBehaviour>? OnBehaviourAdded { get; set; } = null;
public Action<IBehaviourController, IBehaviour>? OnBehaviourRemoved { get; set; } = null;
public Action<IAssignableGameObject>? OnGameObjectAssigned { get; set; } = null;
private readonly IList<IBehaviour> behaviours = new List<IBehaviour>(Constants.BEHAVIOURS_SIZE_INITIAL);
private IGameObject _gameObject = null!;
public IGameObject GameObject => _gameObject;
public T AddBehaviour<T>(T behaviour) where T : class, IBehaviour
{
InsertBehaviourByPriority(behaviour);
behaviour.Initialize();
behaviour.OnPriorityChanged += OnPriorityChange;
OnBehaviourAdded?.Invoke(this, behaviour);
return behaviour;
}
public T AddBehaviour<T>(params object?[]? args) where T : class, IBehaviour
=> AddBehaviour(new Factory.BehaviourFactory().Instantiate<T>(_gameObject, args));
public bool TryGetBehaviour<T>([NotNullWhen(returnValue: true)] out T? behaviour)
{
foreach (var behaviourItem in behaviours)
{
if (behaviourItem is not T result)
continue;
behaviour = result;
return true;
}
behaviour = default;
return false;
}
public IList<T> GetBehaviours<T>() where T : IBehaviour
{
IList<T> behaviours = new List<T>();
foreach (var behaviourItem in this.behaviours)
{
if (behaviourItem is not T behaviour)
continue;
behaviours ??= new List<T>();
behaviours.Add(behaviour);
}
return behaviours;
}
public void RemoveBehaviour<T>(bool removeAll = false) where T : IBehaviour
{
for (int i = behaviours.Count; i >= 0; i--)
{
if (behaviours[i] is not T behaviour)
continue;
behaviour.OnPriorityChanged -= OnPriorityChange;
behaviour.Finalize();
behaviours.RemoveAt(i);
OnBehaviourRemoved?.Invoke(this, behaviour);
if (!removeAll)
return;
}
}
public bool Assign(IGameObject gameObject)
{
if (_gameObject is not null)
return false;
_gameObject = gameObject;
OnGameObjectAssigned?.Invoke(this);
return true;
}
public void Update(GameTime gameTime)
{
if (!GameObject.StateEnable.Enabled)
return;
OnUpdate?.Invoke(this, gameTime);
}
public void UpdatePreDraw(GameTime gameTime)
{
if (!GameObject.StateEnable.Enabled)
return;
OnPreDraw?.Invoke(this, gameTime);
}
public BehaviourController() { }
public BehaviourController(IGameObject gameObject) => Assign(gameObject);
private void InsertBehaviourByPriority<T>(T behaviour) where T : class, IBehaviour
{
int i;
for (i = 0; i < behaviours.Count; i++)
{
if (behaviours[i].Priority > behaviour.Priority)
continue;
behaviours.Insert(i, behaviour);
return;
}
if (i == 0 || i == behaviours.Count)
behaviours.Add(behaviour);
}
private void OnPriorityChange(IBehaviour behaviour)
{
behaviours.Remove(behaviour);
InsertBehaviourByPriority(behaviour);
}
}

View File

@ -0,0 +1,58 @@
using System;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public abstract class BehaviourOverride : Behaviour
{
protected IGameObject GameObject => BehaviourController.GameObject;
protected ITransform Transform => BehaviourController.GameObject.Transform;
public BehaviourOverride()
{
OnInitialized += OnInitialize;
OnFinalized += OnFinalize;
}
protected virtual void OnInitialize() { }
private void OnInitialize(IInitialize _)
{
BehaviourController.OnPreDraw += PreDraw;
BehaviourController.OnUpdate += Update;
OnInitialize();
}
protected virtual void OnFinalize() { }
private void OnFinalize(IInitialize _)
{
BehaviourController.OnPreDraw -= PreDraw;
BehaviourController.OnUpdate -= Update;
OnFinalize();
}
protected virtual void OnUpdatePreEnabledCheck(GameTime time) { }
protected virtual void OnUpdate(GameTime time) { }
private void Update(IBehaviourController _, GameTime time)
{
OnUpdatePreEnabledCheck(time);
if (!StateEnable.Enabled)
return;
OnUpdate(time);
}
protected virtual void OnPreDrawPreEnabledCheck(GameTime time) { }
protected virtual void OnPreDraw(GameTime time) { }
private void PreDraw(IBehaviourController _, GameTime time)
{
OnPreDrawPreEnabledCheck(time);
if (!StateEnable.Enabled)
return;
OnPreDraw(time);
}
}

View File

@ -0,0 +1,42 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core.Abstract;
using ISprite = Syntriax.Engine.Core.Abstract.ISprite;
namespace Syntriax.Engine.Core.Behaviours;
public class DrawableSpriteBehaviour : Behaviour, IDrawBehaviour, IAssignableSprite
{
public Action<IAssignableSprite>? OnSpriteAssigned { get; set; } = null;
private ISprite _sprite = null!;
public ISprite Sprite => _sprite;
public void Draw(SpriteBatch spriteBatch)
{
if (!StateEnable.Enabled)
return;
Vector2 position = BehaviourController.GameObject.Transform.Position;
Vector2 scale = BehaviourController.GameObject.Transform.Scale;
Rectangle rectangle = new Rectangle((int)position.X, (int)position.Y, (int)(Sprite.Texture2D.Width * scale.X), (int)(Sprite.Texture2D.Height * scale.Y));
spriteBatch.Draw(Sprite.Texture2D, rectangle, Sprite.Color);
}
public bool Assign(ISprite sprite)
{
if (_sprite is not null)
return false;
_sprite = sprite;
OnSpriteAssigned?.Invoke(this);
return true;
}
}

View File

@ -0,0 +1,9 @@
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core.Behaviours;
public interface IDrawBehaviour : IBehaviour
{
public void Draw(SpriteBatch spriteBatch);
}

View File

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.1.303" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,20 @@
using System;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core.Exceptions;
public class AssignException : Exception
{
public AssignException() : base("Assign operation has failed.") { }
public AssignException(string? message) : base(message) { }
// public static AssignException FromStateEnable(IStateEnable? stateEnable)
// => new AssignException($"{nameof(IGameObject.AssignStateEnable)} failed on type {stateEnable?.GetType().ToString() ?? "\"null\""}");
public static AssignException From<T, T2>(T to, T2? value)
=> new AssignException($"Assign operation has failed on T: {typeof(T).FullName}, value: {value?.GetType().ToString() ?? "\"null\""}");
// public static AssignException FromBehaviourController(IBehaviourController? behaviourController)
// => new AssignException($"{nameof(IGameObject.AssignBehaviourController)} failed on type {behaviourController?.GetType().ToString() ?? "\"null\""}");
}
// throw new Exception($"{nameof(IGameObject.AssignTransform)} failed on type {transform?.GetType().ToString() ?? "null"} for type {typeof(T).FullName}");
// throw new Exception($"{nameof(IGameObject.AssignBehaviourController)} failed on type {behaviourController?.GetType().ToString() ?? "null"} for type {typeof(T).FullName}");
// throw new Exception($"{nameof(IGameObject.AssignStateEnable)} failed on type {stateEnable?.GetType().ToString() ?? "null"} for type {typeof(T).FullName}");

View File

@ -0,0 +1,21 @@
using System;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core.Exceptions;
public class NotAssignedException : Exception
{
public NotAssignedException() : base("The object has not been assigned.") { }
public NotAssignedException(string? message) : base(message) { }
public static NotAssignedException From<T1, T2>(T1 to, T2? value) where T1 : IAssignable
=> new NotAssignedException($"{typeof(T2).Name} has not been assigned to {typeof(T1).Name}");
public static void Check<T1, T2>(T1 to, T2? value) where T1 : IAssignable
{
if (value is not null)
return;
throw From(to, value);
}
}

View File

@ -0,0 +1,6 @@
namespace Syntriax.Engine.Core.Factory.Abstract;
public interface IFactory<TInterface> where TInterface : class
{
T Get<T>(params object?[]? args) where T : class, TInterface;
}

View File

@ -0,0 +1,21 @@
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core.Factory;
public class BehaviourControllerFactory
{
public IBehaviourController Instantiate(IGameObject gameObject)
=> Instantiate<BehaviourController>(gameObject);
public T Instantiate<T>(IGameObject gameObject, params object?[]? args)
where T : class, IBehaviourController
{
T behaviourController = TypeFactory.Get<T>(args);
if (!behaviourController.Assign(gameObject))
throw AssignException.From(behaviourController, gameObject);
return behaviourController;
}
}

View File

@ -0,0 +1,27 @@
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core.Factory;
public class BehaviourFactory
{
public T Instantiate<T>(IGameObject gameObject, params object?[]? args) where T : class, IBehaviour
=> Instantiate<T>(gameObject, stateEnable: null, args);
public T Instantiate<T>(IGameObject gameObject, IStateEnable? stateEnable, params object?[]? args)
where T : class, IBehaviour
{
T behaviour = TypeFactory.Get<T>(args);
stateEnable ??= TypeFactory.Get<StateEnable>();
if (!stateEnable.Assign(behaviour))
throw AssignException.From(stateEnable, behaviour);
if (!behaviour.Assign(gameObject.BehaviourController))
throw AssignException.From(behaviour, gameObject.BehaviourController);
if (!behaviour.Assign(stateEnable))
throw AssignException.From(behaviour, stateEnable);
return behaviour;
}
}

View File

@ -0,0 +1,23 @@
using System;
using Syntriax.Engine.Core.Factory.Abstract;
namespace Syntriax.Engine.Core.Factory;
public abstract class FactoryBase<TInterface> : IFactory<TInterface>
where TInterface : class
{
public virtual T Get<T>(params object?[]? args) where T : class, TInterface
{
T? result;
if (args is not null && args.Length != 0)
result = Activator.CreateInstance(typeof(T), args) as T;
else
result = Activator.CreateInstance(typeof(T)) as T;
if (result is null)
throw new Exception($"{typeof(TInterface).Name} of type {typeof(T).Name} could not be created.");
return result;
}
}

View File

@ -0,0 +1,39 @@
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core.Factory;
public class GameObjectFactory
{
public T Instantiate<T>(params object?[]? args) where T : class, IGameObject
=> Instantiate<T>(transform: null, behaviourController: null, stateEnable: null, args);
public T Instantiate<T>(
ITransform? transform = null,
IBehaviourController? behaviourController = null,
IStateEnable? stateEnable = null,
params object?[]? args
)
where T : class, IGameObject
{
T gameObject = TypeFactory.Get<T>(args);
transform ??= TypeFactory.Get<Transform>();
behaviourController ??= TypeFactory.Get<BehaviourController>();
stateEnable ??= TypeFactory.Get<StateEnable>();
if (!behaviourController.Assign(gameObject))
throw AssignException.From(behaviourController, gameObject);
if (!stateEnable.Assign(gameObject))
throw AssignException.From(stateEnable, gameObject);
if (!gameObject.Assign(transform))
throw AssignException.From(gameObject, transform);
if (!gameObject.Assign(behaviourController))
throw AssignException.From(gameObject, behaviourController);
if (!gameObject.Assign(stateEnable))
throw AssignException.From(gameObject, stateEnable);
return gameObject;
}
}

View File

@ -0,0 +1,19 @@
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core.Factory;
public class StateEnableFactory
{
public IStateEnable Instantiate(IEntity entity) => Instantiate<StateEnable>(entity);
public T Instantiate<T>(IEntity entity, params object?[]? args) where T : class, IStateEnable
{
T stateEnable = TypeFactory.Get<T>(args);
if (!stateEnable.Assign(entity))
throw AssignException.From(stateEnable, entity);
return stateEnable;
}
}

View File

@ -0,0 +1,10 @@
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core.Factory;
public class TransformFactory
{
public ITransform Instantiate() => TypeFactory.Get<Transform>();
public T Instantiate<T>(params object?[]? args) where T : class, ITransform
=> TypeFactory.Get<T>(args);
}

View File

@ -0,0 +1,21 @@
using System;
namespace Syntriax.Engine.Core.Factory;
public static class TypeFactory
{
public static T Get<T>(params object?[]? args) where T : class
{
T? result;
if (args is not null && args.Length != 0)
result = Activator.CreateInstance(typeof(T), args) as T;
else
result = Activator.CreateInstance(typeof(T)) as T;
if (result is null)
throw new Exception($"{typeof(T).Name} of type {typeof(T).Name} could not be created.");
return result;
}
}

View File

@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Factory;
namespace Syntriax.Engine.Core;
public class GameManager
{
public Game Game { get; private set; } = null!;
private IList<IGameObject> _gameObjects = new List<IGameObject>(Constants.GAME_OBJECTS_SIZE_INITIAL);
private GameObjectFactory _gameObjectFactory = null!;
private GameObjectFactory GameObjectFactory
{
get
{
if (_gameObjectFactory is null)
_gameObjectFactory = new GameObjectFactory();
return _gameObjectFactory;
}
}
public IList<IGameObject> GameObjects => _gameObjects;
public void RegisterGameObject(IGameObject gameObject)
{
if (_gameObjects.Contains(gameObject))
throw new Exception($"{nameof(IGameComponent)} named {gameObject.Name} is already registered to the {nameof(GameManager)}.");
_gameObjects.Add(gameObject);
}
public T InstantiateGameObject<T>() where T : class, IGameObject
{
T gameObject = GameObjectFactory.Instantiate<T>();
_gameObjects.Add(gameObject);
return gameObject;
}
// public TGameObject RegisterGameObject<TGameObject, TTransform, TBehaviourController, TStateEnable>()
// where TGameObject : class, IGameObject
// where TTransform : class, ITransform
// where TBehaviourController : class, IBehaviourController
// where TStateEnable : class, IStateEnable
// {
// TGameObject gameObject = Factory.GameObjectFactory.Get<TGameObject>();
// _gameObjects.Add(gameObject);
// return gameObject;
// }
public void RemoveGameObject(IGameObject gameObject)
{
if (!_gameObjects.Contains(gameObject))
throw new Exception($"{nameof(IGameComponent)} named {gameObject.Name} is not registered to the {nameof(GameManager)}.");
_gameObjects.Remove(gameObject);
}
}

141
Engine.Core/GameObject.cs Normal file
View File

@ -0,0 +1,141 @@
using System;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core.Abstract;
using Syntriax.Engine.Core.Exceptions;
namespace Syntriax.Engine.Core;
public class GameObject : IGameObject
{
public Action<IAssignableStateEnable>? OnStateEnableAssigned { get; set; } = null;
public Action<IAssignableTransform>? OnTransformAssigned { get; set; } = null;
public Action<IAssignableBehaviourController>? OnBehaviourControllerAssigned { get; set; } = null;
public Action<IEntity>? OnNameChanged { get; set; } = null;
public Action<IInitialize>? OnInitialized { get; set; } = null;
public Action<IInitialize>? OnFinalized { get; set; } = null;
public Action<IGameObject, GameTime>? OnUpdated { get; set; } = null;
private ITransform _transform = null!;
private IBehaviourController _behaviourController = null!;
private IStateEnable _stateEnable = null!;
private string _name = nameof(GameObject);
private bool _initialized = false;
public ITransform Transform => _transform;
public IBehaviourController BehaviourController => _behaviourController;
public IStateEnable StateEnable => _stateEnable;
public bool Initialized
{
get => _initialized;
private set
{
if (value == _initialized)
return;
_initialized = value;
if (value)
OnInitialized?.Invoke(this);
else
OnFinalized?.Invoke(this);
}
}
public string Name
{
get => _name;
set
{
if (value == _name) return;
_name = value;
OnNameChanged?.Invoke(this);
}
}
public bool Initialize()
{
if (Initialized)
return false;
NotAssignedException.Check(this, _transform);
NotAssignedException.Check(this, _behaviourController);
NotAssignedException.Check(this, _stateEnable);
Initialized = true;
return true;
}
public void Update(GameTime time)
{
if (!_stateEnable.Enabled)
return;
OnUpdated?.Invoke(this, time);
}
public bool Finalize()
{
if (!Initialized)
return false;
System.Threading.Tasks.Parallel.ForEach(
_behaviourController.GetBehaviours<IBehaviour>(),
behaviour => behaviour.Finalize()
);
_transform = null!;
_behaviourController = null!;
_stateEnable = null!;
Initialized = false;
return true;
}
public bool Assign(IStateEnable stateEnable)
{
if (_stateEnable is not null)
return false;
_stateEnable = stateEnable;
OnStateEnableAssigned?.Invoke(this);
return true;
}
public bool Assign(ITransform transform)
{
if (_transform is not null)
return false;
_transform = transform;
OnTransformAssigned?.Invoke(this);
return true;
}
public bool Assign(IBehaviourController behaviourController)
{
if (_behaviourController is not null)
return false;
_behaviourController = behaviourController;
OnBehaviourControllerAssigned?.Invoke(this);
return true;
}
public GameObject() { OnBehaviourControllerAssigned += ConnectBehaviourController; }
private void ConnectBehaviourController(IAssignableBehaviourController controller)
{
controller.BehaviourController.OnBehaviourAdded += OnBehaviourAdded;
controller.BehaviourController.OnBehaviourRemoved += OnBehaviourRemoved;
}
private void OnBehaviourRemoved(IBehaviourController _, IBehaviour behaviour) { if (Initialized) behaviour.Initialize(); }
private void OnBehaviourAdded(IBehaviourController _, IBehaviour behaviour) { if (Initialized) behaviour.Finalize(); }
}

59
Engine.Core/Sprite.cs Normal file
View File

@ -0,0 +1,59 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
// TODO Probably gonna have to rethink this
public class Sprite : ISprite
{
public Action<ISprite>? OnTextureChanged { get; set; }
public Action<ISprite>? OnColorChanged { get; set; }
public Action<ISprite>? OnDepthChanged { get; set; }
private Texture2D _texture = null!;
private Color _color = Color.White;
private float _depth = 0f;
public Texture2D Texture2D
{
get => _texture;
set
{
if (_texture == value)
return;
_texture = value;
OnTextureChanged?.Invoke(this);
}
}
public Color Color
{
get => _color;
set
{
if (_color == value)
return;
_color = value;
OnColorChanged?.Invoke(this);
}
}
public float Depth
{
get => _depth;
set
{
if (_depth == value)
return;
_depth = value;
OnDepthChanged?.Invoke(this);
}
}
}

View File

@ -0,0 +1,39 @@
using System;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public class StateEnable : IStateEnable
{
public Action<IAssignableEntity>? OnEntityAssigned { get; set; } = null;
public Action<IStateEnable>? OnEnabledChanged { get; set; } = null;
private bool _enabled = true;
private IEntity _entity = null!;
public IEntity Entity => _entity;
public bool Enabled
{
get => _enabled;
set
{
if (value == _enabled)
return;
_enabled = value;
OnEnabledChanged?.Invoke(this);
}
}
public bool Assign(IEntity entity)
{
if (_entity is not null)
return false;
_entity = entity;
OnEntityAssigned?.Invoke(this);
return true;
}
}

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace Syntriax.Engine.Core;
internal static class Constants
{
internal static int BEHAVIOURS_SIZE_INITIAL = 16;
internal static int GAME_OBJECTS_SIZE_INITIAL = 256;
}

57
Engine.Core/Transform.cs Normal file
View File

@ -0,0 +1,57 @@
using System;
using Microsoft.Xna.Framework;
using Syntriax.Engine.Core.Abstract;
namespace Syntriax.Engine.Core;
public class Transform : ITransform
{
public Action<ITransform>? OnPositionChanged { get; set; } = null;
public Action<ITransform>? OnScaleChanged { get; set; } = null;
public Action<ITransform>? OnRotationChanged { get; set; } = null;
private Vector2 _position = Vector2.Zero;
private Vector2 _scale = Vector2.One;
private float _rotation = 0f;
public Vector2 Position
{
get => _position;
set
{
if (value == _position)
return;
_position = value;
OnPositionChanged?.Invoke(this);
}
}
public Vector2 Scale
{
get => _scale;
set
{
if (value == _scale)
return;
_scale = value;
OnScaleChanged?.Invoke(this);
}
}
public float Rotation
{
get => _rotation;
set
{
if (value == _rotation)
return;
_rotation = value;
OnRotationChanged?.Invoke(this);
}
}
}

View File

@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Engine.Core", "Engine.Core\Engine.Core.csproj", "{71719EAD-1B6B-4229-B39E-E53A2E8BFAB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -11,4 +13,10 @@ Global
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{71719EAD-1B6B-4229-B39E-E53A2E8BFAB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71719EAD-1B6B-4229-B39E-E53A2E8BFAB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71719EAD-1B6B-4229-B39E-E53A2E8BFAB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71719EAD-1B6B-4229-B39E-E53A2E8BFAB1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal