Skip to content

Commit

Permalink
Add 'Consumable' script to Foods (#555)
Browse files Browse the repository at this point in the history
* Remove 'food' trait from beverages

* Add nutriment substance

this substance will be used for all foods until we figure out our nutrient system better

* Add consumability to food prefabs

* Add missing item reference to consumable script on drink prefabs

* Fix twinkie hold positions

* Fix various bugs

Co-authored-by: Alain Siegrist <[email protected]>
  • Loading branch information
cosmiccoincidence and Alainx277 authored Aug 3, 2020
1 parent 7a7b7f1 commit cd6cba9
Show file tree
Hide file tree
Showing 42 changed files with 2,027 additions and 421 deletions.
79 changes: 35 additions & 44 deletions Assets/Content/Items/Consumables/Food/Consumable.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -57,53 +57,43 @@ public void ConsumeAction(GameObject origin, GameObject target = null)
if (target == null) target = origin;
audio = target.transform.GetComponent<AudioSource>();

if (!audio.isPlaying && content.CurrentVolume > 0)
if (!audio.isPlaying && content.TotalMoles > 0)
{
// Gets player's audio source
audio = target.GetComponentInChildren<AudioSource>();

// Randomly select the audio clip and the pitch
audio.pitch = Random.Range(0.7f,1.5f);
audio.pitch = Random.Range(0.7f, 1.5f);
int audioClip = Random.Range(0, sounds.Length);
AudioClip sound = sounds[audioClip];

// Plays the clip in the client and server
audio.PlayOneShot(sound, audio.pitch);
RpcPlayEatingSound(audio.pitch, audioClip);

Item itemInHand = origin.GetComponentInChildren<Hands>().GetItemInHand();

// Does the portion is in the last one? Does it leaves trash?
if (content.CurrentVolume == 1 && trashPrefab != null )
{
Item trash = Instantiate(trashPrefab, transform.position, transform.rotation).GetComponent<Item>();

// Spawn the trash in the server
NetworkServer.Spawn(trash.gameObject);
trash.GenerateNewIcon();

if (itemInHand == null)

// Check if the item is fully consumed
if (content.TotalMoles - contentPerUse <= 0.001)
{
if (trashPrefab != null)
{
// Destroys the item in the hand, it's all networked
item.Destroy();
Item trash = Instantiate(trashPrefab, transform.position, transform.rotation).GetComponent<Item>();

// Spawn the trash in the server
NetworkServer.Spawn(trash.gameObject);
trash.GenerateNewIcon();

// Replaces the item with trash
ItemHelpers.ReplaceItem(item, trash);
}
else
else if (destroyObject)
{
// Replaces the item in the hand for the trash instance, it's all networked
ItemHelpers.ReplaceItem(itemInHand, trash);
item.Destroy();
}
}
// Last one? Should it destroy the object?
if (content.CurrentVolume - contentPerUse <= 0 && destroyObject)
{
item.Destroy();
}
// Not last one?
else
{
content.RemoveMoles(contentPerUse);
Debug.Log(target.name + " consumed " + transform.name);
}

// Remove contents
content.RemoveMoles(contentPerUse);
}
}

Expand Down Expand Up @@ -140,6 +130,12 @@ public override string GetName(InteractionEvent interactionEvent)

public override bool CanInteract(InteractionEvent interactionEvent)
{
// Range check
if (!InteractionExtensions.RangeCheck(interactionEvent))
{
return false;
}

// easy access to shit
GameObject source = interactionEvent.Source.GetComponentInTree<Creature>().gameObject;
GameObject target = interactionEvent.Target?.GetComponent<Transform>().gameObject;
Expand All @@ -149,8 +145,6 @@ public override bool CanInteract(InteractionEvent interactionEvent)
Creature creature = source.GetComponent<Creature>();
Creature targetCreature = target?.GetComponent<Creature>();

Debug.Log("source: " + source.name + " target: " + target?.name + " item: " + itemInHand?.gameObject.name);

// if there's no targeted creature and no item in hand
if (targetCreature == null && itemInHand == null)
{
Expand All @@ -174,14 +168,13 @@ public override bool CanInteract(InteractionEvent interactionEvent)
Verb = "Feed";
Delay = itemInHand.feedTime;
}

// Range check
if (!InteractionExtensions.RangeCheck(interactionEvent))
// Consumable checks (is playing audio, is empty)
if (itemInHand.audio.isPlaying || itemInHand.content.TotalMoles <= 0)
{
// Consumable checks (is playing audio, is empty)
if (!itemInHand.audio.isPlaying && itemInHand.content.CurrentVolume > 0)
return false;
return false;
}


return true;
}
Expand All @@ -195,9 +188,7 @@ protected override void StartDelayed(InteractionEvent interactionEvent)
GameObject source = interactionEvent.Source?.GetComponentInTree<Creature>().gameObject;
GameObject target = interactionEvent.Target.GetComponent<Transform>().gameObject;
Consumable itemInHand = source?.GetComponentInChildren<Hands>().GetItemInHand()?.GetComponent<Consumable>();

//Debug.Log("source: " + source.name + " target: " + target?.name + " item: " + itemInHand?.gameObject.name);


// Item in hand and interacting with origin
if (target == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &1861453833026372563
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &6114283984059902716
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &6446191997104870732
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &-8023195135660676867
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &8007766618970146251
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &1085376433779955738
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &42966476225087036
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &8537094951476634685
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &3208315790591029780
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ MonoBehaviour:
prefab: {fileID: 6405106075082308508}
attachmentPoint: {fileID: 1680750711217523086}
bulkSize: 2
traits:
- {fileID: 11400000, guid: 76dd1a5ac7fd55d428ed789d29c15f9c, type: 2}
traits: []
--- !u!114 &7628843760292732928
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -234,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &-7843511282263597710
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &846540291069206127
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &-6173571324145430837
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &6766920265302312201
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &5916578783003781652
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &-8588885193856872355
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &-3277866210014622133
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &-7444677638681580398
AudioSource:
m_ObjectHideFlags: 0
Expand Down
2 changes: 1 addition & 1 deletion Assets/Content/Items/Consumables/Food/Drinks/Milk.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ MonoBehaviour:
feedTime: 1
LoadingBarPrefab: {fileID: 1998198171871307260, guid: 58d68fcd6d5f2c5458ad32de2270df37,
type: 3}
item: {fileID: 0}
item: {fileID: 6162823455628302933}
--- !u!82 &-5431605415766148546
AudioSource:
m_ObjectHideFlags: 0
Expand Down
Loading

0 comments on commit cd6cba9

Please sign in to comment.