Skip to content
Snippets Groups Projects
Commit 99928e61 authored by MuhamadAjiW's avatar MuhamadAjiW
Browse files

feat: world object prefixes, object removal within manager

parent 6d00cf7c
Branches
Tags
2 merge requests!2Base arch,!1Base arch
using UnityEngine;
public class Dummy : EnemyEntity{
// Static attributes
public static string ObjectIdPrefix = "Dummy";
// Attributes
private DummyAnimationController animationController;
public DummyStateController stateController;
......@@ -8,6 +11,7 @@ public class Dummy : EnemyEntity{
// Constructor
new protected void Start(){
base.Start();
SetIdPrefix(ObjectIdPrefix);
stateController = new DummyStateController(this);
animationController = new DummyAnimationController(this);
}
......
using UnityEngine;
public class TestCollectible : Collectible{
// Static attributes
public static string ObjectIdPrefix = "Player";
// Constructor
protected new void Start(){
base.Start();
SetIdPrefix(ObjectIdPrefix);
}
// Functions
protected override void OnCollect(){
Debug.Log("Test collectible collected");
}
......
......@@ -4,6 +4,9 @@ using System.Collections.Generic;
using UnityEngine;
public class Player : AccompaniableCombatant {
// Static attributes
public static string ObjectIdPrefix = "Player";
// Attributes
private PlayerAnimationController animationController;
private PlayerMovementController movementController;
......@@ -14,6 +17,7 @@ public class Player : AccompaniableCombatant {
// Constructor
new void Start(){
base.Start();
SetIdPrefix(ObjectIdPrefix);
Health *= GameConfig.DIFFICULTY_MODIFIERS[GameSaveData.instance.difficulty].PlayerHealthMultiplier;
// TODO: Review, base damage is currently done in the ObjectFactory. Might need to decide which is best
......
......@@ -16,6 +16,10 @@ public class BaseObjectManager : MonoBehaviour{
Debug.Log(string.Format("Object ids in {0}: {1}", ManagerName, idArray));
}
public virtual WorldObject[] GetWorldObjects(){
return GetComponentsInChildren<WorldObject>();
}
public virtual WorldObject GetWorldObject(string id){
WorldObject[] worldObjects = GetComponentsInChildren<WorldObject>();
for (int i = 0; i < worldObjects.Length; i++){
......@@ -23,4 +27,9 @@ public class BaseObjectManager : MonoBehaviour{
}
return null;
}
public virtual void RemoveWorldObject(string id){
WorldObject worldObject = GetWorldObject(id);
Destroy(worldObject.gameObject);
}
}
\ No newline at end of file
using System;
using UnityEngine;
public class DamageableEntity : RigidEntity, IDamageable{
public class DamageableEntity : WorldEntity, IDamageable{
// Attributes
private bool damageable = true;
[SerializeField] private float maxHealth;
......
using UnityEngine;
public class RigidEntity : WorldObject, IRigid {
public class WorldEntity : WorldObject, IRigid {
// Attributes
[SerializeField] private float knockbackResistance;
[SerializeField] private float baseSpeed;
......
using UnityEngine;
public class WorldObject : MonoBehaviour{
// Attributes
private static int autoIncrement = 0;
private int NumberId;
private string Prefix;
public string Id;
// Constructor
protected void Awake(){
Id = autoIncrement.ToString();
NumberId = autoIncrement;
Id = NumberId.ToString();
autoIncrement++;
}
}
\ No newline at end of file
// Functions
protected void SetIdPrefix(string prefix){
Prefix = prefix;
if(Prefix == "") Id = NumberId.ToString();
else Id = Prefix + "_" + Id;
}
}
......@@ -12,7 +12,7 @@ public static class ObjectFactory{
int renderingOrder = 0,
string objectName = "Unnamed Object"
){
GameObject returnObject = parent == null? GameObject.Instantiate(gameObject) : GameObject.Instantiate(gameObject, parent);
GameObject returnObject = parent == null? GameObject.Instantiate(gameObject, ObjectManager.instance.transform) : GameObject.Instantiate(gameObject, parent);
if(position != null) returnObject.transform.position = position.Value;
if(rotation != null) returnObject.transform.rotation = rotation.Value;
if(scale != null) returnObject.transform.localScale = Vector3.Scale(returnObject.transform.localScale, scale.Value);
......@@ -73,7 +73,7 @@ public static class ObjectFactory{
return prefabObject;
}
public static GameObject CreateCollectibleObject(
public static Collectible CreateCollectibleObject(
string prefabPath,
Transform parent = null,
Vector3? position = null,
......@@ -83,10 +83,24 @@ public static class ObjectFactory{
string objectName = "Unnamed Object"
){
GameObject prefabObject = CreateObject(prefabPath, parent == null? ObjectManager.instance.transform : parent, position, scale, rotation, renderingOrder, objectName);
if(!prefabObject.TryGetComponent<Collectible>(out var collectibleObject)) Debug.LogError("Loaded prefab is not a Collectible: " + prefabPath);
if(!prefabObject.TryGetComponent<Collectible>(out var collectible)) Debug.LogError("Loaded prefab is not a Collectible: " + prefabPath);
prefabObject.layer = LayerMask.NameToLayer(GameEnvironmentConfig.LAYER_COLLECTIBLE);
return prefabObject;
return collectible;
}
public static WorldEntity CreateEntity(
string prefabPath,
Transform parent = null,
Vector3? position = null,
Vector3? scale = null,
Quaternion? rotation = null,
int renderingOrder = 0,
string objectName = "Unnamed Object"
){
GameObject prefabObject = CreateObject(prefabPath, parent == null? EntityManager.instance.transform : parent, position, scale, rotation, renderingOrder, objectName);
if(!prefabObject.TryGetComponent<WorldEntity>(out var entity)) Debug.LogError("Loaded prefab is not an entity: " + prefabPath);
return entity;
}
public static void Destroy(GameObject gameObject, float delay = 0){
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment