Add new archerytarget model, finish archerygame

This commit is contained in:
Toomas Tamm
2021-02-18 02:14:54 +02:00
parent 4688dcf9a6
commit 2af8f2e576
22 changed files with 1927 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using Random = UnityEngine.Random;
@@ -8,13 +9,17 @@ namespace Bow
{
public class ArcheryRange : MonoBehaviour
{
public TMP_Text highScoreText;
public TMP_Text timeLeftText;
public TMP_Text scoreText;
public Transform targetStartPosition;
public Transform targetEndPosition;
public GameObject targetPrefab;
public StartTarget startTarget;
public Vector3 minRandomOffset = new Vector3(0f, 0f, -5f);
public Vector3 maxRandomOffset = new Vector3(0f, 0f, 5f);
public Vector3 minRandomOffset = new Vector3(0f, -2f, -5f);
public Vector3 maxRandomOffset = new Vector3(0f, 2f, 5f);
public float roundLength = 60f;
public float targetSpawnTime = 3f;
@@ -30,6 +35,7 @@ namespace Bow
// Start is called before the first frame update
void Start()
{
highScoreText.text = "High Score: 0";
_roundActive = false;
_targets = new List<ArcheryTarget>();
_score = 0;
@@ -43,12 +49,13 @@ namespace Bow
{
if (!_roundActive) return;
if (Time.time >= _roundEndTime + roundLength)
if (Time.time >= _roundEndTime)
{
Reset();
}
else
{
timeLeftText.text = $"Time Left: {Math.Ceiling((_roundEndTime - Time.time) % 60)}";
if (Time.time >= _nextTargetTime)
{
_nextTargetTime = Time.time + targetSpawnTime;
@@ -61,11 +68,16 @@ namespace Bow
{
var randomPos = targetStartPosition.position + new Vector3(
Random.Range(minRandomOffset.x, maxRandomOffset.x),
Random.Range(minRandomOffset.y, maxRandomOffset.y), Random.Range(minRandomOffset.z, maxRandomOffset.z));
(float) Math.Round(Random.Range(minRandomOffset.y, maxRandomOffset.y)),
Random.Range(minRandomOffset.z, maxRandomOffset.z));
var prefab = Instantiate(targetPrefab, randomPos, Quaternion.identity, null);
prefab.transform.Rotate(new Vector3(0, -90, 0));
ArcheryTarget target = prefab.GetComponent<ArcheryTarget>();
target.endPosition = targetEndPosition.position;
target.minRandomOffset = minRandomOffset;
target.maxRandomOffset = maxRandomOffset;
target.archeryRange = this;
_targets.Add(target);
@@ -83,9 +95,10 @@ namespace Bow
_targets = new List<ArcheryTarget>();
if (_maxScore < _score) _maxScore = _score;
_score = 0;
Debug.Log(_maxScore);
highScoreText.text = $"High Score: {_maxScore}";
startTarget.ShowTarget();
_roundActive = false;
timeLeftText.text = "";
}
public void StartRound()
@@ -97,8 +110,8 @@ namespace Bow
public void AddScore(float distance)
{
_score += _startEndDistance - (_startEndDistance - distance);
Debug.Log(_score);
_score += distance;
scoreText.text = $"Score: {_score}";
}
}
}

View File

@@ -1,21 +1,54 @@
using System;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Bow
{
public class ArcheryTarget : MonoBehaviour
{
public GameObject pointsText;
public Vector3 endPosition;
public float forwardSpeed = 2f;
public float sidewaysSpeed = 1f;
public ArcheryRange archeryRange;
public Vector3 minRandomOffset = new Vector3();
public Vector3 maxRandomOffset = new Vector3();
private bool _flipDirection;
private void Start()
{
_flipDirection = Random.value > 0.5f;
var position = transform.position;
minRandomOffset.z += position.z;
maxRandomOffset.z += position.z;
}
// Update is called once per frame
void Update()
{
float step = forwardSpeed * Time.deltaTime;
var position = transform.position;
if (Math.Abs(position.x - endPosition.x) < 0.1) Destroy(gameObject);
if (Math.Abs(position.z - maxRandomOffset.z) < 0.1)
{
_flipDirection = true;
}
if (Math.Abs(position.z - minRandomOffset.z) < 0.1)
{
_flipDirection = false;
}
float desiredZ = _flipDirection ? maxRandomOffset.z : minRandomOffset.z;
transform.position = Vector3.MoveTowards(position,
new Vector3(endPosition.x, position.y, position.z), step);
new Vector3(endPosition.x, position.y, desiredZ), step);
}
@@ -23,7 +56,16 @@ namespace Bow
{
if (arrow == null) return;
archeryRange.AddScore(Vector3.Distance(transform.position, endPosition));
var position = transform.position;
float score = (float) Math.Round(Vector3.Distance(position, endPosition));
archeryRange.AddScore(score);
GameObject prefab = Instantiate(pointsText, position, Quaternion.Euler(0, 90f, 0), null);
PointsText target = prefab.GetComponent<PointsText>();
target.SetPoints(score);
Destroy(arrow.gameObject);
Destroy(gameObject);
}

View File

@@ -0,0 +1,17 @@
using UnityEngine;
namespace Bow
{
public class ArcheryTargetCollisionProxy : MonoBehaviour
{
public ArcheryTarget target;
public void OnArrowHit(Arrow arrow)
{
if (arrow == null) return;
if (target == null) return;
target.OnArrowHit(arrow);
}
}
}

View File

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

View File

@@ -0,0 +1,37 @@
using TMPro;
using UnityEngine;
namespace Bow
{
public class PointsText : MonoBehaviour
{
public TMP_Text text;
public float destroyTime = 2f;
public float upSpeed = 2f;
private float _destroyOnTime;
private void Start()
{
_destroyOnTime = Time.time + destroyTime;
}
public void Update()
{
if (_destroyOnTime <= Time.time)
{
Destroy(gameObject);
}
float step = upSpeed * Time.deltaTime;
var position = transform.position;
transform.position = Vector3.MoveTowards(position,
new Vector3(position.x, position.y + 1f, position.z), step);
}
public void SetPoints(float points)
{
text.text = $"+{points}";
}
}
}

View File

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