Creation de ResourceManager, ResourceMaker et ResourceRemover
Permet la gestion, la création et la prise en charge du cout des resources respectivement.
This commit is contained in:
parent
92146f563c
commit
61775fdf76
1491
Assets/Scenes/TestResource.unity
Normal file
1491
Assets/Scenes/TestResource.unity
Normal file
File diff suppressed because it is too large
Load Diff
7
Assets/Scenes/TestResource.unity.meta
Normal file
7
Assets/Scenes/TestResource.unity.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 904f54d590ea9a542b03b82ca0aa1a01
|
||||||
|
DefaultImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
47
Assets/Scripts/ResourceMaker.cs
Normal file
47
Assets/Scripts/ResourceMaker.cs
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class ResourceMaker : MonoBehaviour
|
||||||
|
{
|
||||||
|
[SerializeField] private int _resourceMakingAmount;
|
||||||
|
[SerializeField] private ResourceChoice _resourceChoice;
|
||||||
|
|
||||||
|
private ResourceManager _resourceManager;
|
||||||
|
private enum ResourceChoice
|
||||||
|
{
|
||||||
|
Rock,
|
||||||
|
Wood,
|
||||||
|
Food
|
||||||
|
};
|
||||||
|
|
||||||
|
public void GenerateResource()
|
||||||
|
{
|
||||||
|
_resourceManager = ResourceManager.getInstance();
|
||||||
|
Make();
|
||||||
|
}
|
||||||
|
|
||||||
|
private IEnumerator Make()
|
||||||
|
{
|
||||||
|
switch (_resourceChoice)
|
||||||
|
{
|
||||||
|
case ResourceChoice.Rock:
|
||||||
|
_resourceManager.RockAmount = _resourceMakingAmount;
|
||||||
|
break;
|
||||||
|
case ResourceChoice.Wood:
|
||||||
|
_resourceManager.WoodAmount = _resourceMakingAmount;
|
||||||
|
break;
|
||||||
|
case ResourceChoice.Food:
|
||||||
|
_resourceManager.FoodAmount = _resourceMakingAmount;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
yield return new WaitForSecondsRealtime(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void StopGenerate()
|
||||||
|
{
|
||||||
|
StopCoroutine(Make());
|
||||||
|
}
|
||||||
|
}
|
||||||
11
Assets/Scripts/ResourceMaker.cs.meta
Normal file
11
Assets/Scripts/ResourceMaker.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 9dd6bad38b516d64e8e23a5822ca37bb
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
77
Assets/Scripts/ResourceManager.cs
Normal file
77
Assets/Scripts/ResourceManager.cs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sert d'inventaire et gère l'accès aux ressources
|
||||||
|
/// </summary>
|
||||||
|
public class ResourceManager : MonoBehaviour
|
||||||
|
{
|
||||||
|
private static ResourceManager _instance = null;
|
||||||
|
private int _rockAmount = 0;
|
||||||
|
private int _woodAmount = 0;
|
||||||
|
private int _foodAmount = 0;
|
||||||
|
|
||||||
|
private const int MAX = 100;
|
||||||
|
private const int MIN = 0;
|
||||||
|
|
||||||
|
public ResourceManager() { }
|
||||||
|
|
||||||
|
public int RockAmount
|
||||||
|
{
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_rockAmount + value < MAX)
|
||||||
|
{
|
||||||
|
_rockAmount += value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get { return _rockAmount; }
|
||||||
|
}
|
||||||
|
public int WoodAmount
|
||||||
|
{
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_woodAmount + value < MAX)
|
||||||
|
{
|
||||||
|
_woodAmount += value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get { return _woodAmount; }
|
||||||
|
}
|
||||||
|
public int FoodAmount
|
||||||
|
{
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (_foodAmount + value < MAX)
|
||||||
|
{
|
||||||
|
_foodAmount += value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get { return _foodAmount; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ResourceManager getInstance()
|
||||||
|
{
|
||||||
|
if (_instance == null)
|
||||||
|
{
|
||||||
|
_instance = new ResourceManager();
|
||||||
|
}
|
||||||
|
return _instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(int rock, int wood, int food)
|
||||||
|
{
|
||||||
|
_rockAmount = _rockAmount - rock < MIN ? MIN : _rockAmount - rock;
|
||||||
|
_rockAmount = _rockAmount - wood < MIN ? MIN : _rockAmount - wood;
|
||||||
|
_foodAmount = _foodAmount - food < MIN ? MIN : _foodAmount - food;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool EnoughFor(int rock, int wood, int food = 0)
|
||||||
|
{
|
||||||
|
if (rock > _rockAmount || wood > _woodAmount || food > _foodAmount)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
11
Assets/Scripts/ResourceManager.cs.meta
Normal file
11
Assets/Scripts/ResourceManager.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 45e6e814b8c91334ca1d6d0e7f530fef
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
61
Assets/Scripts/ResourceRemover.cs
Normal file
61
Assets/Scripts/ResourceRemover.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
using UnityEngine.UIElements;
|
||||||
|
|
||||||
|
public class ResourceRemover : MonoBehaviour
|
||||||
|
{
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private int _rock;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private int _wood;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private int _food;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private Button _button;
|
||||||
|
|
||||||
|
[SerializeField]
|
||||||
|
private TextField _text;
|
||||||
|
|
||||||
|
private ResourceManager _resourceManager;
|
||||||
|
|
||||||
|
// Start is called before the first frame update
|
||||||
|
void Start()
|
||||||
|
{
|
||||||
|
_resourceManager = ResourceManager.getInstance();
|
||||||
|
|
||||||
|
_button.clicked += () =>
|
||||||
|
{
|
||||||
|
//Will remove resources as well as execute placement action defined in the Editors
|
||||||
|
_resourceManager.Remove(_rock, _wood, _food);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update is called once per frame
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
|
ChangeAvailability();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ChangeAvailability()
|
||||||
|
{
|
||||||
|
//checks if player has enough resources then chooses state of button's availability
|
||||||
|
|
||||||
|
StyleColor textColor = _text.style.color;
|
||||||
|
if (_resourceManager.EnoughFor(_rock, _wood, _food) && textColor == Color.red)
|
||||||
|
{
|
||||||
|
textColor = Color.green;
|
||||||
|
}
|
||||||
|
else if (textColor == Color.green)
|
||||||
|
{
|
||||||
|
textColor = Color.red;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
11
Assets/Scripts/ResourceRemover.cs.meta
Normal file
11
Assets/Scripts/ResourceRemover.cs.meta
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: adc0c9da23034d342aec05775f0bd941
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
16
ProjectSettings/ShaderGraphSettings.asset
Normal file
16
ProjectSettings/ShaderGraphSettings.asset
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
%YAML 1.1
|
||||||
|
%TAG !u! tag:unity3d.com,2011:
|
||||||
|
--- !u!114 &1
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 61
|
||||||
|
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: de02f9e1d18f588468e474319d09a723, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
|
customInterpolatorErrorThreshold: 32
|
||||||
|
customInterpolatorWarningThreshold: 16
|
||||||
Loading…
x
Reference in New Issue
Block a user