Basic functionality

This commit is contained in:
James Hodgson
2022-04-13 01:41:08 +01:00
parent cde9a606dc
commit 2efbbfa61a
9 changed files with 814 additions and 1 deletions

View File

@@ -0,0 +1,47 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collectible : MonoBehaviour
{
public int score = 1;
public bool collected = false;
public bool isEnabled = true;
Renderer r;
// Start is called before the first frame update
void Start()
{
r = GetComponent<Renderer>();
if (collected || !isEnabled)
{
Hide();
}
}
// Update is called once per frame
void Update()
{
}
public void Collect()
{
collected = true;
Hide();
}
void Hide()
{
r.enabled = false;
}
void Show()
{
r.enabled = true;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4723232ca862917cf809ab8453f3bb95
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,18 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collectibles : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4be239dc47d456e83b0048ae3e6c7d7e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,82 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishCollector : MonoBehaviour
{
public GameObject collectiblesContainer;
// Total score of all collectibles
public int totalScore = 0;
Collectible[] collectibles;
Renderer r;
CharacterController cc;
// Start is called before the first frame update
void Start()
{
r = GetComponent<Renderer>();
cc = GetComponent<CharacterController>();
collectibles = collectiblesContainer.GetComponentsInChildren<Collectible>();
}
// FixedUpdate is called consistently at 50 fps (by default)
// Figured we can do collection checks slightly less frequently :)
void FixedUpdate()
{
Vector3 fishPosition = r.bounds.center;
foreach (Collectible collectible in collectibles)
{
if (collectible.collected || !collectible.isEnabled)
{
// Skip collectibles already collected or disabled
continue;
}
Renderer collectibleRenderer = collectible.GetComponent<Renderer>();
float collectibleRadius = collectibleRenderer.bounds.extents.magnitude;
Vector3 collectiblePosition = collectibleRenderer.bounds.center;
bool x = false;
bool y = false;
bool z = false;
// Debug.Log(String.Format("FishPosition: {0}, CollectiblePosition: {1}, CollectibleRadius: {2}", fishPosition, collectiblePosition, collectibleRadius));
// Check if we are colliding with the collectible
if (fishPosition.x >= collectiblePosition.x - collectibleRadius &&
fishPosition.x <= collectiblePosition.x + collectibleRadius)
{
x = true;
}
if (fishPosition.y >= collectiblePosition.y - collectibleRadius &&
fishPosition.y <= collectiblePosition.y + collectibleRadius)
{
y = true;
}
if (fishPosition.z >= collectiblePosition.z - collectibleRadius &&
fishPosition.z <= collectiblePosition.z + collectibleRadius)
{
z = true;
}
if (x && y && z)
{
totalScore += collectible.score;
collectible.Collect();
// Assume we aren't colliding with 2 at the same time!
break;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 066790d93d3867f8c9ff65939b6b10bd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: