Skip to content
Snippets Groups Projects
Commit 681b6f00 authored by Kimi Heinonen's avatar Kimi Heinonen
Browse files

Lisatty scriptit city ja resourcepool

parent 3e893fb8
No related branches found
No related tags found
No related merge requests found
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class City : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
/**
* Kayttaa resurssia r, maaran 'amount' verran (amount >= 0).
* Voidaan kayttaa maximissaan sen verran resursseja mita niita on kaytossa.
*/
public void useResource(Resource r, int amount)
{
if (amount<0)
{
return;
}
ResourcePool[] lista = gameObject.GetComponents<ResourcePool>();
for (int i = 0; i < lista.Length; i++)
{
if (lista[i].getResource().GetType().Equals(r.GetType()))
{
lista[i].useResource(amount);
}
}
}
/**
* Lisaa resurssia r, maaran 'amount' verran (amount >= 0).
* Voidaan lisata maximissaan resurssin maximikapasiteettiin asti,
* ts. jos nykyinen resurssimaara + lisatty maara >= maximikapasiteetti
* niin uusi resurssimaara = maximikapasiteetti
*/
public void addResource(Resource r, int amount)
{
if (amount<0)
{
return;
}
ResourcePool[] lista = gameObject.GetComponents<ResourcePool>();
for (int i = 0; i < lista.Length; i++)
{
if (lista[i].getResource().GetType().Equals(r.GetType()))
{
lista[i].addResource(amount);
return;
}
}
ResourcePool respool = gameObject.AddComponent<ResourcePool>();
respool.setResource(r);
respool.addResource(amount);
}
}
fileFormatVersion: 2
guid: dfe928a858872424991e927606dd4bc3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResourcePool : MonoBehaviour
{
[SerializeField]
private Resource r;
[SerializeField]
private int amount = 0;
[SerializeField]
private int maxCapacity = 100;
// Start is called before the first frame update
void Start()
{
}
public void setResource(Resource r)
{
this.r = r;
}
public Resource getResource()
{
return r;
}
/**
* Kayttaa resurssia maaran X verran
*/
public void useResource(int x)
{
if (this.amount - x <= 0)
{
this.amount = 0;
}
else
{
this.amount -= x;
}
}
/**
* Lisaa resurssia maaran X verran
*/
public void addResource(int x)
{
if (this.amount + x >= this.maxCapacity)
{
this.amount = this.maxCapacity;
}
else
{
this.amount += x;
}
}
}
fileFormatVersion: 2
guid: d5f9cff662cdcb44d9481366eacfa431
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment