Commit eb02ff1b authored by 15김민규's avatar 15김민규

sang-un will implement LoadDropTable

parent ac20fee3
monsterid,name,itemid,weight
1,몬스터1,2,3
,,3,2
2,몬스터2,5,3
,,6,1
,,7,2
3,몬스터3,4,1
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class Enemy : MonoBehaviour { public class Enemy : MonoBehaviour {
// Use this for initialization private readonly float maxHealth;
void Start () { private readonly float weight;
private float currHealth;
} private EnemyManager.State currState;
private List<int[]> dropTable; // [item ID, numerator]
// Update is called once per frame private Dictionary<EnemyManager.State, EnemyManager.Action<int>> actionByState;
void Update () { public Enemy(uint id, float maxHealth, float weight) {
this.maxHealth = maxHealth;
} this.weight = weight;
this.currHealth = maxHealth;
this.dropTable = GetDropTable(id);
this.actionByState = GetActionByState(id);
this.currState = EnemyManager.State.Idle;
}
public void GetDamaged(float damage) {
float unitDist = 3;
currHealth -= damage;
if(currHealth <= 0) {
mamaWooWooWoo_I_Dont_Wanna_Die();
return;
}
float knockback_dist = damage * unitDist / weight;
// do something - knockback animation
}
private List<int[]> GetDropTable(uint id) {
List<int[]> resultList = new List<int[]>();
return resultList;
}
private Dictionary<EnemyManager.State, EnemyManager.Action<int>> GetActionByState(uint id) {
var resultDictionary = new Dictionary<EnemyManager.State, EnemyManager.Action<int>>();
return resultDictionary;
}
private void mamaWooWooWoo_I_Dont_Wanna_Die() {
int dropItemId = -1;
if (dropTable != null) {
float denominator = dropTable[dropTable.Count - 1][1];
float numerator = Random.Range(0f, denominator);
foreach (var drop in dropTable) {
if (numerator <= drop[1]) {
dropItemId = drop[0];
break;
}
}
}
// spawn a item that has ID
// destroy itself (or, deactivate for pooling)
Destroy(gameObject);
}
} }
...@@ -2,15 +2,29 @@ ...@@ -2,15 +2,29 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class EnemyManager : MonoBehaviour { public class EnemyManager : Singleton<EnemyManager> {
public enum State {
Idle,
Track,
Attack
} // 상속을 통해 수정할 가능성 높음. 염두만 해 두자.
// Use this for initialization public delegate void Action<T>();
void Start () {
private Dictionary<uint, List<int[]>> dropTableByID;
} private Dictionary<uint, List<int[]>> actionDictByID;
// Update is called once per frame protected EnemyManager() {
void Update () { string dropTableDataPath = null;
string actionTableDataPath = null;
}
LoadDropTable(dropTableDataPath);
LoadActionTable(actionTableDataPath);
}
private void LoadDropTable(string dataPath) {
}
private void LoadActionTable(string dataPath) {
}
} }
using UnityEngine;
using System.Collections;
/// <summary>
/// Be aware this will not prevent a non singleton constructor
/// such as `T myT = new T();`
/// To prevent that, add `protected T () {}` to your singleton class.
/// As a note, this is made as MonoBehaviour because we need Coroutines.
/// </summary>
public abstract class Singleton<T> : MonoBehaviour where T : MonoBehaviour {
private static T _instance = null;
private static object _syncobj = new object();
private static bool appIsClosing = false;
public static T Instance {
get {
if (appIsClosing)
return null;
lock (_syncobj) {
if (_instance == null) {
T[] objs = FindObjectsOfType<T>();
if (objs.Length > 0)
_instance = objs[0];
if (objs.Length > 1)
Debug.LogError("There is more than one " + typeof(T).Name + " in the scene.");
if (_instance == null) {
string goName = typeof(T).ToString();
GameObject go = GameObject.Find(goName);
if (go == null)
go = new GameObject(goName);
_instance = go.AddComponent<T>();
}
}
return _instance;
}
}
}
/// <summary>
/// When Unity quits, it destroys objects in a random order.
/// In principle, a Singleton is only destroyed when application quits.
/// If any script calls Instance after it have been destroyed,
/// it will create a buggy ghost object that will stay on the Editor scene
/// even after stopping playing the Application. Really bad!
/// So, this was made to be sure we're not creating that buggy ghost object.
/// </summary>
protected virtual void OnApplicationQuit() {
// release reference on exit
appIsClosing = true;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: a4301571906f4094aba1a7696df8d3ba
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
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