Skip to content

Commit

Permalink
Added support for the [Asset] attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreylanters committed Jan 9, 2021
1 parent ed3dfa1 commit 028d8be
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 6 deletions.
Binary file modified .github/WIKI/lifecycle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified .github/WIKI/lifecycle.sketch
Binary file not shown.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Entity Component System

[![npm](https://img.shields.io/badge/upm-3.6.0-232c37.svg?style=for-the-badge)]()
[![npm](https://img.shields.io/badge/upm-3.7.0-232c37.svg?style=for-the-badge)]()
[![npm](https://img.shields.io/github/stars/elraccoone/unity-entity-component-system.svg?style=for-the-badge)]()
[![npm](https://img.shields.io/badge/build-passing-brightgreen.svg?style=for-the-badge)]()

Expand Down Expand Up @@ -61,6 +61,7 @@ Use build in file generator to create new instances for any of these types.
```cs
/// Base class for every controller.
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
/// NOTE: This class allows the usage of [Asset] properties.
abstract class Controller {

/// A reference to the controller.
Expand Down Expand Up @@ -95,14 +96,16 @@ abstract class Controller {
bool HasService<S> () where S : IService, new();
/// Gets an asset from this controller.
AssetType GetAsset<AssetType> (string name);
/// Gets an asset from this controller.
System.Object GetAsset (string name);
/// Check whether this controller has an asset.
bool HasAsset (string name);
}
```

```cs
/// Base class for every entity component.
/// NOTE: This class allows the usage of [Referenced] and [Protected] objects and primitives.
/// NOTE: This class allows the usage of [Referenced] and [Protected] properties.
abstract class EntityComponent<EntityComponentType, EntitySystemType> : UnityEngine.MonoBehaviour, IEntityComponent
where EntityComponentType : EntityComponent<EntityComponentType, EntitySystemType>, new()
where EntitySystemType : EntitySystem<EntitySystemType, EntityComponentType>, new() {
Expand Down Expand Up @@ -144,6 +147,7 @@ abstract class EntityComponent<EntityComponentType, EntitySystemType> : UnityEng
```cs
/// Base class for every entity system.
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
/// NOTE: This class allows the usage of [Asset] properties.
abstract class EntitySystem<EntitySystemType, EntityComponentType> : IEntitySystem
where EntitySystemType : EntitySystem<EntitySystemType, EntityComponentType>, new()
where EntityComponentType : EntityComponent<EntityComponentType, EntitySystemType>, new() {
Expand Down Expand Up @@ -226,6 +230,7 @@ abstract class EntitySystem<EntitySystemType, EntityComponentType> : IEntitySyst
```cs
/// Base class for every service
/// NOTE: This class allows the usage of [Injected] systems, services and controller.
/// NOTE: This class allows the usage of [Asset] properties.
abstract class Service<ServiceType> : IService
where ServiceType : Service<ServiceType>, new() {

Expand Down
46 changes: 46 additions & 0 deletions Runtime/Attributes/Asset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
namespace ElRaccoone.EntityComponentSystem {

/// Describes an asset which should be assigned automatically.
[System.AttributeUsage (System.AttributeTargets.Field)]
public class Asset : System.Attribute {

/// Defines whether the field name should be used for getting the asset from
/// the controller. When set to false, the asset name string will be used
/// instead.
private bool useFieldNameAsAssetName = false;

/// The asset name will be used when defined that the field name should not
/// be used for finding the asset name. Thus using this asset name property
/// instead.
private string assetName = "";

/// Defines the property as an asset, when the controller is done
/// registering, the asset will be fetched from the controller using the
/// name of the property field.
public Asset () {
this.useFieldNameAsAssetName = true;
this.assetName = "";
}

/// Defines the property as an asset, when the controller is done
/// registering, the asset will be fetched from the controller using the
/// given asset name.
public Asset(string assetName) {
this.useFieldNameAsAssetName = false;
this.assetName = assetName;
}

/// Sets the attributes values on an object.
public static void SetAttributeValues (System.Object target) {
var _fields = target.GetType ().GetFields (
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
foreach (var _field in _fields) {
var _assetFieldAttribute = System.Attribute.GetCustomAttribute (_field, typeof (Asset)) as Asset;
if (_assetFieldAttribute != null)
_field.SetValue (target, Controller.Instance.GetAsset(
_assetFieldAttribute.useFieldNameAsAssetName == true ? _field.Name : _assetFieldAttribute.assetName));
}
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Attributes/Asset.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 15 additions & 3 deletions Runtime/Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ public void Register (params System.Type[] typesOf) {
Injected.SetAttributeValues (this.systems[_systemIndex]);
for (var _serviceIndex = 0; _serviceIndex < this.services.Count; _serviceIndex++)
Injected.SetAttributeValues (this.services[_serviceIndex]);

// Set Values of the 'Asset' attributes
Asset.SetAttributeValues (this);
for (var _systemIndex = 0; _systemIndex < this.systems.Count; _systemIndex++)
Asset.SetAttributeValues (this.systems[_systemIndex]);
for (var _serviceIndex = 0; _serviceIndex < this.services.Count; _serviceIndex++)
Asset.SetAttributeValues (this.services[_serviceIndex]);
}

/// Enables or disabled a system, enabling the systems allows them to invoke
Expand Down Expand Up @@ -223,11 +230,16 @@ public System.Object GetService (System.Type typeOf) {
}

/// Gets an asset from this controller.
public AssetType GetAsset<AssetType> (string name) where AssetType : UnityEngine.Object {
public UnityEngine.Object GetAsset (string name) {
for (var _i = 0; _i < this.assets.Length; _i++)
if (this.assets[_i].name == name)
return this.assets[_i] as AssetType;
throw new System.Exception ("Unable to get asset, it was not added to the controller");
return this.assets[_i];
throw new System.Exception ($"Unable to get asset '{name}', it was not found on the controller");
}

/// Gets an asset from this controller.
public AssetType GetAsset<AssetType> (string name) where AssetType : UnityEngine.Object {
return this.GetAsset(name) as AssetType;
}

/// Check whether this controller has an asset.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nl.elraccoone.entity-component-system",
"displayName": "Entity Component System",
"version": "3.6.0",
"version": "3.7.0",
"unity": "2019.1",
"description": "A better approach to game design that allows you to concentrate on the actual problems you are solving: the data and behavior that make up your game. By moving from object-oriented to data-oriented design it will be easier for you to reuse the code and easier for others to understand and work on it."
}

0 comments on commit 028d8be

Please sign in to comment.