/// <summary> /// アイテムを獲得する /// </summary> /// <param name="item"></param> /// <returns>アイテムを獲得したかどうか /// スロットの関係などで獲得できなかった場合はfalseを返す</returns> public bool GetItem(Item item) { AlignItems(); bool result = false; //既に持っていてそれに重ねることができる場合はそうする for(var i = 0;i < SlotCount;i++) { if(ItemSlot[i] != null && ItemSlot[i].Combine(item)) { result = true; break; } } if (!result) { //アイテムスロットの空きを取得 int brank = ItemSlot.FindAll((i) => i == null).Count; //獲得するアイテムが要するスロット数を満たしていれば //アイテムを獲得する if(item.Size < brank) { ItemSlot[SlotCount - brank] = item; for(var i = 0;i < item.Size - 1;i++) { ItemSlot[SlotCount - brank + 1 + i] = new ConnectedSlot(item); } item.SetPlayer(this); result = true; } } return result; }
/// <summary> /// 指定された名前のJsonファイルを読みに行って、その中身をステージ上に出す /// </summary> /// <param name="filename"></param> public static void Load(string filename) { TextAsset asset = Resources.Load("StageJson/" + filename) as TextAsset; if(asset == null) { Debug.Log("ステージ読み込めなかった(ステージ名:" + filename + ")"); return; } JsonNode json = JsonNode.Parse(asset.text); Transform parent; parent = GameObject.Find("/Terrains").transform; foreach(var item in json["Terrains"]) { GameObject g = new Terrain(item).Load(); if (g != null) g.transform.SetParent(parent); } parent = GameObject.Find("/Enemies").transform; foreach(var item in json["Enemies"]) { GameObject g = new Enemy(item).Load(); if (g != null) g.transform.SetParent(parent); } parent = GameObject.Find("/Gimmicks").transform; foreach (var item in json["Gimmicks"]) { GameObject g = new Gimmick(item).Load(); if (g != null) g.transform.SetParent(parent); } parent = GameObject.Find("/Items").transform; foreach (var item in json["Items"]) { GameObject g = new Item(item).Load(); if (g != null) g.transform.SetParent(parent); } }
/// <summary> /// ItemSlotを、nullがListのインデックスの大きい所へ来るようにソートする /// たとえば、{Item1, Item2, Connected, null, Item3, null}とかだったら /// {Item1, Item2, Connected, Item3, null, null}になってほしい /// </summary> public void AlignItems() { Item[] items = new Item[ItemSlot.Count]; ItemSlot.CopyTo(items); List<Item> list = new List<Item>(items); list.RemoveAll(i => i == null || i.GetType() == typeof(ConnectedSlot)); for(var i = 0;i < SlotCount - 1;i++) { ItemSlot[i] = null; } int index = 0; foreach(var item in list) { ItemSlot[index] = item; for(var i = 0;i < item.Size - 1;i++) { ItemSlot[index + i + 1] = new ConnectedSlot(item); } index += item.Size; } }
/// <summary> /// すでにitemを所持していた時に、それに獲得したアイテムを足す /// </summary> /// <param name="item"></param> /// <returns>アイテムを足せたかどうか</returns> public abstract bool Combine(Item item);
/// <summary> /// 現在シーン上に配置されているオブジェクトをいくつかのタグごとで別のJson用オブジェクトにして、Jsonファイルに書き込む /// </summary> /// <param name="filename">保存するステージの名前</param> /// <param name="limit">ステージの制限時間</param> /// <param name="slotcount">アイテムスロットの個数</param> /// <param name="region">ステージの(行動可能)範囲</param> public static void Save(string filename) { Stage stage = new Stage(); foreach(var g in GameObject.FindGameObjectsWithTag("Terrain")) { if (g.transform.parent.name != "Terrains") continue; Terrain terrain = new Terrain(g); stage.Terrains.Add(terrain); } foreach(var g in GameObject.FindGameObjectsWithTag("Enemy")) { if (g.transform.parent.name != "Enemies") continue; Enemy enemy = new Enemy(g); stage.Enemies.Add(enemy); } foreach(var g in GameObject.FindGameObjectsWithTag("Gimmick")) { if (g.transform.parent.name != "Gimmicks") continue; Gimmick gimmick = new Gimmick(g); stage.Gimmicks.Add(gimmick); } foreach(var g in GameObject.FindGameObjectsWithTag("Item")) { if (g.transform.parent.name != "Items") continue; Item item = new Item(g); stage.Items.Add(item); } string jsontext = JsonMapper.ToJson(stage); StreamWriter writer = new StreamWriter(Application.dataPath + "/Resources/StageJson/" + filename + ".json"); writer.Write(jsontext); writer.Close(); Debug.Log("ステージ作った(ステージ名:" + filename + ")"); }
/// <summary> /// 指定された名前のJsonファイルを読みに行って、その中身をステージ上に出す /// </summary> /// <param name="stagename"></param> public static void Load(string stagename) { TextAsset asset = Resources.Load("StageJson/" + GameSetting.PlayerCount + "P/" + stagename) as TextAsset; if(asset == null) { Debug.Log("ステージ読み込めなかった(ステージ名:" + stagename + ")"); return; } JsonNode json = JsonNode.Parse(asset.text); GameSetting.TimeLimit = (int)json["TimeLimit"].Get<long>(); GameSetting.SlotCount = (int)json["SlotCount"].Get<long>(); Transform parent; parent = (GameObject.Find("/Players") as GameObject).transform; //foreach (var item in json["Players"]) //{ // GameObject g = new Player(item).Load(); // if (g != null) // { // g.transform.SetParent(parent); // GameSetting.PlayerCount++; // } //} for(var i = 0;i < json["Players"].Count;i++) { string prefabname = GameSetting.Players[i].PrefabName; GameObject g = new Player(json["Players"][i]).Load(prefabname); if(g != null) { g.name = "Player" + (i + 1); g.transform.SetParent(parent); } } parent = (GameObject.Find("/Terrains") as GameObject).transform; foreach(var item in json["Terrains"]) { GameObject g = new Terrain(item).Load(); if (g != null) g.transform.SetParent(parent); } parent = (GameObject.Find("/Gimmicks") as GameObject).transform; foreach (var item in json["Gimmicks"]) { GameObject g = new Gimmick(item).Load(); if (g != null) g.transform.SetParent(parent); } GameSetting.FlagCount = 0; parent = (GameObject.Find("/Flags") as GameObject).transform; foreach (var item in json["Flags"]) { GameObject g = new Flag(item).Load(); if (g != null) { g.transform.SetParent(parent); GameSetting.FlagCount++; } } parent = (GameObject.Find("/Items") as GameObject).transform; foreach (var item in json["Items"]) { GameObject g = new Item(item).Load(); if (g != null) g.transform.SetParent(parent); } }
/// <summary> /// 現在シーン上に配置されているオブジェクトをいくつかのタグごとで別のJson用オブジェクトにして、Jsonファイルに書き込む /// </summary> /// <param name="stagename">保存するステージの名前</param> /// <param name="limit">ステージの制限時間</param> /// <param name="slotcount">アイテムスロットの個数</param> /// <param name="region">ステージの(行動可能)範囲</param> public static void Save(string stagename, int limit, int slotcount, Region region) { Stage stage = new Stage(); stage.TimeLimit = limit; stage.SlotCount = slotcount; stage.StageRegion = region; foreach(var g in GameObject.FindGameObjectsWithTag("Player")) { Player player = new Player(g); stage.Players.Add(player); } foreach(var g in GameObject.FindGameObjectsWithTag("Terrain")) { Terrain terrain = new Terrain(g); stage.Terrains.Add(terrain); } foreach(var g in GameObject.FindGameObjectsWithTag("Gimmick")) { Gimmick gimmick = new Gimmick(g); stage.Gimmicks.Add(gimmick); } foreach(var g in GameObject.FindGameObjectsWithTag("Flag")) { Flag flag = new Flag(g); stage.Flags.Add(flag); } foreach(var g in GameObject.FindGameObjectsWithTag("Item")) { Item item = new Item(g); stage.Items.Add(item); } string jsontext = JsonMapper.ToJson(stage); StreamWriter writer = new StreamWriter(Application.dataPath + "/Resources/StageJson/" + stagename + ".json"); writer.Write(jsontext); writer.Close(); Debug.Log("ステージ作った(ステージ名:" + stagename + ")"); }
private Item CreateSimpleItem(ItemID itemId, Material material, Vector3 position) { var componentSource = GetComponentSource(material, position); Item newItem; if (material==Material.BlueGem || material == Material.GreenGem) /* special case - fix 23/03/2016 chatmetaleux */ { newItem = new Item(position, itemId.ToString(), (material == Material.BlueGem) ? "Sapphire":"Emerald"); } else newItem = new Item(position, itemId.ToString(), material.ToString()); if (componentSource != null) { newItem.CrafterHistory = componentSource.Character.History; if (componentSource.ShouldDestroy) { componentSource.Character.LeftRegion(); } } GnomanEmpire.Instance.EntityManager.SpawnEntityImmediate(newItem); return newItem; }
private Item CreateComplexItem(ItemID itemId, Material material, Vector3 position) { Item newItem; List<Item> components; if (itemId == ItemID.Bag || itemId == ItemID.Barrel || itemId == ItemID.Crate || itemId == ItemID.Wheelbarrow) { components = CreateComponents(itemId, material, position); newItem = new StorageContainer(position, itemId.ToString(), components) {CrafterHistory = creator.History}; } else { components = CreateComponents(itemId, material, position); if (components.Any()) { newItem = new Item(position, itemId.ToString(), components) { CrafterHistory = creator.History }; } else { newItem = new Item(position, itemId.ToString(), material.ToString()); } } GnomanEmpire.Instance.EntityManager.SpawnEntityImmediate(newItem); GnomanEmpire.Instance.Fortress.AddItem(newItem); RemoveComponents(components); return newItem; }
/// <summary> /// 指定された名前のJsonファイルを読みに行って、その中身をステージ上に出す /// </summary> /// <param name="stagename"></param> public static void Load(string stagename) { TextAsset asset = Resources.Load("StageJson/" + stagename) as TextAsset; if(asset == null) { Debug.Log("ステージ読み込めなかった(ステージ名:" + stagename + ")"); return; } JsonNode json = JsonNode.Parse(asset.text); GameSetting.TimeLimit = (int)json["TimeLimit"].Get<long>(); GameSetting.SlotCount = (int)json["SlotCount"].Get<long>(); GameSetting.StageName = stagename; Transform parent; parent = (GameObject.Find("/Players") as GameObject).transform; foreach (var item in json["Players"]) { GameObject g = new Player(item).Load(); if (g != null) g.transform.SetParent(parent); } parent = (GameObject.Find("/Terrains") as GameObject).transform; foreach(var item in json["Terrains"]) { GameObject g = new Terrain(item).Load(); if (g != null) g.transform.SetParent(parent); } parent = (GameObject.Find("/Gimmicks") as GameObject).transform; foreach (var item in json["Gimmicks"]) { GameObject g = new Gimmick(item).Load(); if (g != null) g.transform.SetParent(parent); } parent = (GameObject.Find("/Flags") as GameObject).transform; foreach (var item in json["Flags"]) { GameObject g = new Flag(item).Load(); if (g != null) g.transform.SetParent(parent); } parent = (GameObject.Find("/Items") as GameObject).transform; foreach (var item in json["Items"]) { GameObject g = new Item(item).Load(); if (g != null) g.transform.SetParent(parent); } }