From 9e91220be4d574892a8b8f864d5f37d2bc4fd6ef Mon Sep 17 00:00:00 2001 From: s-prechtl Date: Wed, 8 Jun 2022 22:19:40 +0200 Subject: [PATCH] Major Fishing changes --- Assets/Resources/Items/Fish.asset | 20 ++++++ Assets/Resources/Items/Fish.asset.meta | 8 +++ Assets/Scenes/MainScene.unity | 15 ++++- Assets/Scripts/FishingController.cs | 82 ++++++++++++++++++++++++ Assets/Scripts/FishingController.cs.meta | 11 ++++ Assets/Scripts/Item.cs | 16 +++-- Assets/Scripts/ItemContainer.cs | 12 +++- Assets/Scripts/Tiles/BaseTile.cs | 10 --- Assets/Scripts/Tiles/FarmlandTile.cs | 6 +- Assets/Scripts/Tiles/GrassTile.cs | 4 +- Assets/Scripts/Tiles/WaterTile.cs | 38 ++++------- 11 files changed, 173 insertions(+), 49 deletions(-) create mode 100644 Assets/Resources/Items/Fish.asset create mode 100644 Assets/Resources/Items/Fish.asset.meta create mode 100644 Assets/Scripts/FishingController.cs create mode 100644 Assets/Scripts/FishingController.cs.meta diff --git a/Assets/Resources/Items/Fish.asset b/Assets/Resources/Items/Fish.asset new file mode 100644 index 0000000..42befb7 --- /dev/null +++ b/Assets/Resources/Items/Fish.asset @@ -0,0 +1,20 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5f096d187f76a734fac022700054dfec, type: 3} + m_Name: Fish + m_EditorClassIdentifier: + displayName: + description: + id: 0 + selectedSprite: {fileID: 0} + defaultSprite: {fileID: 0} + cost: 0 diff --git a/Assets/Resources/Items/Fish.asset.meta b/Assets/Resources/Items/Fish.asset.meta new file mode 100644 index 0000000..1b1f709 --- /dev/null +++ b/Assets/Resources/Items/Fish.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 7fc4aaa640c1c1e49a0c0a9241fbcf07 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index e640feb..7f760e9 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -1536,7 +1536,7 @@ MonoBehaviour: m_HandleRect: {fileID: 2006577138} m_Direction: 2 m_Value: 1 - m_Size: 0.5932421 + m_Size: 0.59324205 m_NumberOfSteps: 0 m_OnValueChanged: m_PersistentCalls: @@ -6739,6 +6739,7 @@ GameObject: - component: {fileID: 1800469989} - component: {fileID: 1800469991} - component: {fileID: 1800469992} + - component: {fileID: 1800469993} m_Layer: 0 m_Name: Player m_TagString: Untagged @@ -6804,6 +6805,18 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 05183797fdda4aa9ac518eee0d2d85d4, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &1800469993 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1800469988} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 00d85fb65e728cd429cb33d865a5d31a, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1001 &1805366398 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/FishingController.cs b/Assets/Scripts/FishingController.cs new file mode 100644 index 0000000..be98cd6 --- /dev/null +++ b/Assets/Scripts/FishingController.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Data.Common; +using UnityEngine; +using Random = UnityEngine.Random; + +public class FishingController : MonoBehaviour { + #region Singleton + + public static FishingController instance; + + private void Awake() { + if (instance != null) { + Debug.LogWarning("More than one instance of FishingController found"); + } + + instance = this; + } + + #endregion + + private double _fishingTime; + private bool _fishing; + private bool _catchable; + private bool _caught; + private double _fishCooldown; + private readonly double _totalFishCooldown = 1.5f; + private readonly double _maxTime = 2f; + private bool _spawnFish; + + public bool Fishing => _fishing; + + // Start is called before the first frame update + void Start() { + ResetFishing(); + } + + // Update is called once per frame + void Update() { + if (_fishing) { + if (!_catchable) { + _fishCooldown += Time.deltaTime; + if (_spawnFish && _fishCooldown >= _totalFishCooldown && Random.Range(0, 100) <= 1) { + _spawnFish = false; + //particles for fish -> catchable true + + } + } else { + if (!_caught) { + _fishingTime += Time.deltaTime; + } + } + } + } + + private void ResetFishing() { + _fishing = false; + _catchable = false; + _fishingTime = 0f; + _fishCooldown = 1.5f; + _spawnFish = true; + } + + public void StartFishing() { + _fishing = true; + } + + public void TryCatch() { + if (_fishing && _catchable) { + if (_fishingTime <= _maxTime) { + _fishing = false; + _caught = true; + Inventory.instance.AddItem(ItemContainer.Instance.GetItemByName("Fish"), (int)(3/_fishingTime)); + } else { + _spawnFish = true; + _catchable = false; + _fishingTime = 0f; + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/FishingController.cs.meta b/Assets/Scripts/FishingController.cs.meta new file mode 100644 index 0000000..486f755 --- /dev/null +++ b/Assets/Scripts/FishingController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 00d85fb65e728cd429cb33d865a5d31a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Item.cs b/Assets/Scripts/Item.cs index 1e50d59..66ee52b 100644 --- a/Assets/Scripts/Item.cs +++ b/Assets/Scripts/Item.cs @@ -5,18 +5,26 @@ using UnityEngine; public class Item : ScriptableObject, IComparable { public string displayName; public string description; - public int id; //TODO: create an actual ID System that makes snens + private int _id = -1; public Sprite selectedSprite; public Sprite defaultSprite; public int cost; - + + public int ID => _id; + public Item(string displayName, string description, int id) { this.displayName = displayName; this.description = description; - this.id = id; + this._id = id; } public int CompareTo(Item other) { - return this.id - other.id; + return this._id - other._id; + } + + public void SetID(int id) { + if (_id != -1) { + _id = id; + } } } diff --git a/Assets/Scripts/ItemContainer.cs b/Assets/Scripts/ItemContainer.cs index e0b84d5..1ed56b2 100644 --- a/Assets/Scripts/ItemContainer.cs +++ b/Assets/Scripts/ItemContainer.cs @@ -26,8 +26,10 @@ public class ItemContainer : MonoBehaviour { string[] files = Directory.GetFiles("Assets\\Resources\\Items", "*.asset", SearchOption.AllDirectories); foreach (string file in files) { - String path = file.Replace("Assets\\Resources\\", "").Replace(".asset", ""); - _allItems.Add(Resources.Load(path)); + String path = StripPath(file); + Item currItem = Resources.Load(path); + currItem.SetID(_allItems.Count); + _allItems.Add(currItem); } } @@ -41,7 +43,11 @@ public class ItemContainer : MonoBehaviour { return null; } + private String StripPath(String path) { + return path.Replace("Assets\\Resources\\", "").Replace(".asset", ""); + } + public int GetItemIdByName(String name) { - return GetItemByName(name).id; + return GetItemByName(name).ID; } } diff --git a/Assets/Scripts/Tiles/BaseTile.cs b/Assets/Scripts/Tiles/BaseTile.cs index 77df98a..11eeaa3 100644 --- a/Assets/Scripts/Tiles/BaseTile.cs +++ b/Assets/Scripts/Tiles/BaseTile.cs @@ -14,16 +14,6 @@ namespace Tiles this._sprite = GenerateSpriteFromFile(pathToImageFile); } - protected void Start() - { - - } - - protected void Update() - { - - } - public void DayLightStep() { diff --git a/Assets/Scripts/Tiles/FarmlandTile.cs b/Assets/Scripts/Tiles/FarmlandTile.cs index 7ad967c..27c0ef1 100644 --- a/Assets/Scripts/Tiles/FarmlandTile.cs +++ b/Assets/Scripts/Tiles/FarmlandTile.cs @@ -21,18 +21,18 @@ namespace Tiles ItemContainer ic = ItemContainer.Instance; - if (usable.id == ic.GetItemIdByName("Hoe")) + if (usable.ID == ic.GetItemIdByName("Hoe")) { Debug.Log("Farmland hydrated"); //_hydrated = true; } - if (usable.id == ic.GetItemIdByName("Wheat Seed") && _crop == null) + if (usable.ID == ic.GetItemIdByName("Wheat Seed") && _crop == null) { Plant(); } - if (usable.id == ic.GetItemIdByName("Scythe") && _crop != null && _crop.FullyGrown) + if (usable.ID == ic.GetItemIdByName("Scythe") && _crop != null && _crop.FullyGrown) { Harvest(); } diff --git a/Assets/Scripts/Tiles/GrassTile.cs b/Assets/Scripts/Tiles/GrassTile.cs index 3dba8bc..2902e7a 100644 --- a/Assets/Scripts/Tiles/GrassTile.cs +++ b/Assets/Scripts/Tiles/GrassTile.cs @@ -20,9 +20,9 @@ namespace Tiles if (usable != null) { base.Clicked(usable); - if (usable.id == ic.GetItemIdByName("Hoe")) { + if (usable.ID == ic.GetItemIdByName("Hoe")) { rv = new FarmlandTile(); - } else if (usable.id == ic.GetItemIdByName("Shovel")) { + } else if (usable.ID == ic.GetItemIdByName("Shovel")) { rv = new WaterTile(); } } diff --git a/Assets/Scripts/Tiles/WaterTile.cs b/Assets/Scripts/Tiles/WaterTile.cs index 3ddd957..4781be3 100644 --- a/Assets/Scripts/Tiles/WaterTile.cs +++ b/Assets/Scripts/Tiles/WaterTile.cs @@ -1,43 +1,29 @@ using System.Collections; using UnityEngine; -namespace Tiles -{ +namespace Tiles { public class WaterTile : BaseTile { - private double _fishingTime; - private bool _fishing = false; - private bool _catchable = false; - private int _lastCall = 0; public WaterTile() : base("Assets/Farming Asset Pack/Split Assets/farming_tileset_023.png") { } public override BaseTile Clicked(UsableItem usable) { base.Clicked(usable); + BaseTile rv = null; ItemContainer ic = ItemContainer.Instance; - if (usable.id == ic.GetItemIdByName("Fishing Rod")) { - if (!_fishing) { - _fishing = true; - _fishingTime = 0f; - Fish(); - } else if (_catchable) { - _fishing = false; - Debug.Log("Fished for" + _fishingTime/1000); + if (usable.ID == ic.GetItemIdByName("Fishing Rod")) { + FishingController fc = FishingController.instance; + if (!fc.Fishing) { + fc.StartFishing(); + } else { + fc.TryCatch(); } -} - - return null; - } - - private void Fish() { - if (_fishing) { - if (_catchable) { - // _fishingTime += _lastCall - System.DateTime.Now; deltaTime between last recursive call - } - //Fish(); + } else if (usable.ID == ic.GetItemIdByName("Shovel")) { + rv = new GrassTile(); } - + + return rv; } } } \ No newline at end of file