Update Project
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
using UnityEngine;
|
||||
|
||||
#pragma warning disable 649
|
||||
#pragma warning disable 618
|
||||
|
||||
namespace Zenject.Asteroids
|
||||
{
|
||||
public class Ship : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
MeshRenderer _meshRenderer;
|
||||
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
[SerializeField]
|
||||
ParticleSystem _particleSystem;
|
||||
#else
|
||||
[SerializeField]
|
||||
ParticleEmitter _particleEmitter;
|
||||
#endif
|
||||
|
||||
ShipStateFactory _stateFactory;
|
||||
ShipState _state;
|
||||
|
||||
[Inject]
|
||||
public void Construct(ShipStateFactory stateFactory)
|
||||
{
|
||||
_stateFactory = stateFactory;
|
||||
}
|
||||
|
||||
public MeshRenderer MeshRenderer
|
||||
{
|
||||
get { return _meshRenderer; }
|
||||
}
|
||||
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
public ParticleSystem ParticleEmitter
|
||||
{
|
||||
get { return _particleSystem; }
|
||||
}
|
||||
#else
|
||||
public ParticleEmitter ParticleEmitter
|
||||
{
|
||||
get { return _particleEmitter; }
|
||||
}
|
||||
#endif
|
||||
|
||||
public Vector3 Position
|
||||
{
|
||||
get { return transform.position; }
|
||||
set { transform.position = value; }
|
||||
}
|
||||
|
||||
public Quaternion Rotation
|
||||
{
|
||||
get { return transform.rotation; }
|
||||
set { transform.rotation = value; }
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
ChangeState(ShipStates.WaitingToStart);
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
_state.Update();
|
||||
}
|
||||
|
||||
public void OnTriggerEnter(Collider other)
|
||||
{
|
||||
_state.OnTriggerEnter(other);
|
||||
}
|
||||
|
||||
public void ChangeState(ShipStates state)
|
||||
{
|
||||
if (_state != null)
|
||||
{
|
||||
_state.Dispose();
|
||||
_state = null;
|
||||
}
|
||||
|
||||
_state = _stateFactory.CreateState(state);
|
||||
_state.Start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aab42c7e45a13404daf7de27c7366efc
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
using ModestTree;
|
||||
|
||||
namespace Zenject.Asteroids
|
||||
{
|
||||
public enum ShipStates
|
||||
{
|
||||
Moving,
|
||||
Dead,
|
||||
WaitingToStart,
|
||||
Count
|
||||
}
|
||||
|
||||
public class ShipStateFactory
|
||||
{
|
||||
readonly ShipStateWaitingToStart.Factory _waitingFactory;
|
||||
readonly ShipStateMoving.Factory _movingFactory;
|
||||
readonly ShipStateDead.Factory _deadFactory;
|
||||
|
||||
public ShipStateFactory(
|
||||
ShipStateDead.Factory deadFactory,
|
||||
ShipStateMoving.Factory movingFactory,
|
||||
ShipStateWaitingToStart.Factory waitingFactory)
|
||||
{
|
||||
_waitingFactory = waitingFactory;
|
||||
_movingFactory = movingFactory;
|
||||
_deadFactory = deadFactory;
|
||||
}
|
||||
|
||||
public ShipState CreateState(ShipStates state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case ShipStates.Dead:
|
||||
{
|
||||
return _deadFactory.Create();
|
||||
}
|
||||
case ShipStates.WaitingToStart:
|
||||
{
|
||||
return _waitingFactory.Create();
|
||||
}
|
||||
case ShipStates.Moving:
|
||||
{
|
||||
return _movingFactory.Create();
|
||||
}
|
||||
}
|
||||
|
||||
throw Assert.CreateException();
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 732d3e232d9f7084ca4f03e6129bcbb6
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,9 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 35ea476df49732c408f30d07debaf227
|
||||
folderAsset: yes
|
||||
timeCreated: 1461796083
|
||||
licenseType: Pro
|
||||
DefaultImporter:
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Zenject.Asteroids
|
||||
{
|
||||
public class BrokenShipFactory : PlaceholderFactory<Transform>
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b9b5eb959c3c75149a4153c9c7d7a7d4
|
||||
timeCreated: 1528645987
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Zenject.Asteroids
|
||||
{
|
||||
public class ExplosionFactory : PlaceholderFactory<Transform>
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
fileFormatVersion: 2
|
||||
guid: aa6c7bf121e4f8343b05c7d09eb12ec6
|
||||
timeCreated: 1528645987
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Zenject.Asteroids
|
||||
{
|
||||
public abstract class ShipState : IDisposable
|
||||
{
|
||||
public abstract void Update();
|
||||
|
||||
public virtual void Start()
|
||||
{
|
||||
// optionally overridden
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
// optionally overridden
|
||||
}
|
||||
|
||||
public virtual void OnTriggerEnter(Collider other)
|
||||
{
|
||||
// optionally overridden
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a68789b75fb09ef43b854fd3c59241b0
|
||||
timeCreated: 1461796083
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Zenject.Asteroids
|
||||
{
|
||||
public class ShipStateDead : ShipState
|
||||
{
|
||||
readonly SignalBus _signalBus;
|
||||
readonly BrokenShipFactory _brokenShipFactory;
|
||||
readonly ExplosionFactory _explosionFactory;
|
||||
readonly Settings _settings;
|
||||
readonly Ship _ship;
|
||||
|
||||
GameObject _shipBroken;
|
||||
GameObject _explosion;
|
||||
|
||||
public ShipStateDead(
|
||||
Settings settings, Ship ship,
|
||||
ExplosionFactory explosionFactory,
|
||||
BrokenShipFactory brokenShipFactory,
|
||||
SignalBus signalBus)
|
||||
{
|
||||
_signalBus = signalBus;
|
||||
_brokenShipFactory = brokenShipFactory;
|
||||
_explosionFactory = explosionFactory;
|
||||
_settings = settings;
|
||||
_ship = ship;
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
_ship.MeshRenderer.enabled = false;
|
||||
|
||||
_ship.ParticleEmitter.gameObject.SetActive(false);
|
||||
|
||||
_explosion = _explosionFactory.Create().gameObject;
|
||||
_explosion.transform.position = _ship.Position;
|
||||
|
||||
_shipBroken = _brokenShipFactory.Create().gameObject;
|
||||
_shipBroken.transform.position = _ship.Position;
|
||||
_shipBroken.transform.rotation = _ship.Rotation;
|
||||
|
||||
foreach (var rigidBody in _shipBroken.GetComponentsInChildren<Rigidbody>())
|
||||
{
|
||||
var randomTheta = Random.Range(0, Mathf.PI * 2.0f);
|
||||
var randomDir = new Vector3(Mathf.Cos(randomTheta), Mathf.Sin(randomTheta), 0);
|
||||
rigidBody.AddForce(randomDir * _settings.explosionForce);
|
||||
}
|
||||
|
||||
_signalBus.Fire<ShipCrashedSignal>();
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
_ship.MeshRenderer.enabled = true;
|
||||
|
||||
_ship.ParticleEmitter.gameObject.SetActive(true);
|
||||
|
||||
GameObject.Destroy(_explosion);
|
||||
GameObject.Destroy(_shipBroken);
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float explosionForce;
|
||||
}
|
||||
|
||||
public class Factory : PlaceholderFactory<ShipStateDead>
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 379e1c748b9152a449b52cd01e25a642
|
||||
timeCreated: 1461796083
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using ModestTree;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Zenject.Asteroids
|
||||
{
|
||||
public class ShipStateMoving : ShipState
|
||||
{
|
||||
readonly Settings _settings;
|
||||
readonly Camera _mainCamera;
|
||||
readonly Ship _ship;
|
||||
|
||||
Vector3 _lastPosition;
|
||||
float _oscillationTheta;
|
||||
|
||||
public ShipStateMoving(
|
||||
Settings settings, Ship ship,
|
||||
[Inject(Id = "Main")]
|
||||
Camera mainCamera)
|
||||
{
|
||||
_ship = ship;
|
||||
_settings = settings;
|
||||
_mainCamera = mainCamera;
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
UpdateThruster();
|
||||
Move();
|
||||
ApplyOscillation();
|
||||
}
|
||||
|
||||
void ApplyOscillation()
|
||||
{
|
||||
var obj = _ship.MeshRenderer.gameObject;
|
||||
|
||||
var cycleInterval = 1.0f / _settings.oscillationFrequency;
|
||||
var thetaMoveSpeed = 2 * Mathf.PI / cycleInterval;
|
||||
|
||||
_oscillationTheta += thetaMoveSpeed * Time.deltaTime;
|
||||
|
||||
obj.transform.position = obj.transform.parent.position + new Vector3(0, _settings.oscillationAmplitude * Mathf.Sin(_oscillationTheta), 0);
|
||||
}
|
||||
|
||||
void UpdateThruster()
|
||||
{
|
||||
var speed = (_ship.Position - _lastPosition).magnitude / Time.deltaTime;
|
||||
var speedPx = Mathf.Clamp(speed / _settings.speedForMaxEmisssion, 0.0f, 1.0f);
|
||||
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
var emission = _ship.ParticleEmitter.emission;
|
||||
emission.rateOverTime = _settings.maxEmission * speedPx;
|
||||
#else
|
||||
_ship.ParticleEmitter.maxEmission = _settings.maxEmission * speedPx;
|
||||
#endif
|
||||
}
|
||||
|
||||
void Move()
|
||||
{
|
||||
var mouseRay = _mainCamera.ScreenPointToRay(Input.mousePosition);
|
||||
var mousePos = mouseRay.origin;
|
||||
mousePos.z = 0;
|
||||
|
||||
_lastPosition = _ship.Position;
|
||||
_ship.Position = Vector3.Lerp(_ship.Position, mousePos, Mathf.Min(1.0f, _settings.moveSpeed * Time.deltaTime));
|
||||
|
||||
var moveDelta = _ship.Position - _lastPosition;
|
||||
var moveDistance = moveDelta.magnitude;
|
||||
|
||||
if (moveDistance > 0.01f)
|
||||
{
|
||||
var moveDir = moveDelta / moveDistance;
|
||||
_ship.Rotation = Quaternion.LookRotation(-moveDir);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
_lastPosition = _ship.Position;
|
||||
|
||||
_ship.ParticleEmitter.gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
_ship.ParticleEmitter.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
public override void OnTriggerEnter(Collider other)
|
||||
{
|
||||
Assert.That(other.GetComponent<Asteroid>() != null);
|
||||
_ship.ChangeState(ShipStates.Dead);
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float moveSpeed;
|
||||
public float rotateSpeed;
|
||||
|
||||
public float speedForMaxEmisssion;
|
||||
public float maxEmission;
|
||||
|
||||
public float oscillationFrequency;
|
||||
public float oscillationAmplitude;
|
||||
}
|
||||
|
||||
public class Factory : PlaceholderFactory<ShipStateMoving>
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6519cef92721b644c9e0b46f2f92430a
|
||||
timeCreated: 1461796083
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Zenject.Asteroids
|
||||
{
|
||||
public class ShipStateWaitingToStart : ShipState
|
||||
{
|
||||
readonly Settings _settings;
|
||||
readonly Ship _ship;
|
||||
|
||||
float _theta;
|
||||
|
||||
public ShipStateWaitingToStart(
|
||||
Ship ship,
|
||||
Settings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
_ship = ship;
|
||||
}
|
||||
|
||||
public override void Start()
|
||||
{
|
||||
_ship.Position = _settings.StartOffset;
|
||||
_ship.Rotation = Quaternion.AngleAxis(90.0f, Vector3.up) * Quaternion.AngleAxis(90.0f, Vector3.right);
|
||||
}
|
||||
|
||||
public override void Update()
|
||||
{
|
||||
_ship.Position = _settings.StartOffset + Vector3.up * _settings.Amplitude * Mathf.Sin(_theta);
|
||||
_theta += Time.deltaTime * _settings.Frequency;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public Vector3 StartOffset;
|
||||
public float Amplitude;
|
||||
public float Frequency;
|
||||
}
|
||||
|
||||
public class Factory : PlaceholderFactory<ShipStateWaitingToStart>
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ce3520353cbeb084691b14a57a7bec93
|
||||
timeCreated: 1461796083
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user