Initial Commit
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Zenject.SpaceFighter
|
||||
{
|
||||
public class AudioPlayer
|
||||
{
|
||||
readonly Camera _camera;
|
||||
|
||||
public AudioPlayer(Camera camera)
|
||||
{
|
||||
_camera = camera;
|
||||
}
|
||||
|
||||
public void Play(AudioClip clip)
|
||||
{
|
||||
Play(clip, 1);
|
||||
}
|
||||
|
||||
public void Play(AudioClip clip, float volume)
|
||||
{
|
||||
_camera.GetComponent<AudioSource>().PlayOneShot(clip, volume);
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a57395b833349ce43a64f59377d0e398
|
||||
timeCreated: 1456615647
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,91 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Zenject.SpaceFighter
|
||||
{
|
||||
public enum BulletTypes
|
||||
{
|
||||
FromEnemy,
|
||||
FromPlayer
|
||||
}
|
||||
|
||||
public class Bullet : MonoBehaviour, IPoolable<float, float, BulletTypes, IMemoryPool>
|
||||
{
|
||||
float _startTime;
|
||||
BulletTypes _type;
|
||||
float _speed;
|
||||
float _lifeTime;
|
||||
|
||||
[SerializeField]
|
||||
MeshRenderer _renderer = null;
|
||||
|
||||
[SerializeField]
|
||||
Material _playerMaterial = null;
|
||||
|
||||
[SerializeField]
|
||||
Material _enemyMaterial = null;
|
||||
|
||||
IMemoryPool _pool;
|
||||
|
||||
public BulletTypes Type
|
||||
{
|
||||
get { return _type; }
|
||||
}
|
||||
|
||||
public Vector3 MoveDirection
|
||||
{
|
||||
get { return transform.right; }
|
||||
}
|
||||
|
||||
public void OnTriggerEnter(Collider other)
|
||||
{
|
||||
var enemyView = other.GetComponent<EnemyView>();
|
||||
|
||||
if (enemyView != null && _type == BulletTypes.FromPlayer)
|
||||
{
|
||||
enemyView.Facade.Die();
|
||||
_pool.Despawn(this);
|
||||
}
|
||||
else
|
||||
{
|
||||
var player = other.GetComponent<PlayerFacade>();
|
||||
|
||||
if (player != null && _type == BulletTypes.FromEnemy)
|
||||
{
|
||||
player.TakeDamage(MoveDirection);
|
||||
_pool.Despawn(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
transform.position -= transform.right * _speed * Time.deltaTime;
|
||||
|
||||
if (Time.realtimeSinceStartup - _startTime > _lifeTime)
|
||||
{
|
||||
_pool.Despawn(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnSpawned(float speed, float lifeTime, BulletTypes type, IMemoryPool pool)
|
||||
{
|
||||
_pool = pool;
|
||||
_type = type;
|
||||
_speed = speed;
|
||||
_lifeTime = lifeTime;
|
||||
|
||||
_renderer.material = type == BulletTypes.FromEnemy ? _enemyMaterial : _playerMaterial;
|
||||
|
||||
_startTime = Time.realtimeSinceStartup;
|
||||
}
|
||||
|
||||
public void OnDespawned()
|
||||
{
|
||||
_pool = null;
|
||||
}
|
||||
|
||||
public class Factory : PlaceholderFactory<float, float, BulletTypes, Bullet>
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4bcfdc0e00dba5645b88ed3608c61161
|
||||
timeCreated: 1456594035
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
using UnityEngine;
|
||||
|
||||
#pragma warning disable 649
|
||||
|
||||
namespace Zenject.SpaceFighter
|
||||
{
|
||||
public class ControlsDisplay : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
float _leftPadding;
|
||||
|
||||
[SerializeField]
|
||||
float _topPadding;
|
||||
|
||||
[SerializeField]
|
||||
float _width;
|
||||
|
||||
[SerializeField]
|
||||
float _height;
|
||||
|
||||
public void OnGUI()
|
||||
{
|
||||
var bounds = new Rect(_leftPadding, _topPadding, _width, _height);
|
||||
GUI.Label(bounds, "CONTROLS: WASD to move, Mouse to aim, Left Mouse to fire");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3bc2ba00bfd0f6044825d1848b10a7e0
|
||||
timeCreated: 1456683520
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using ModestTree;
|
||||
using UnityEngine;
|
||||
using Random = UnityEngine.Random;
|
||||
|
||||
namespace Zenject.SpaceFighter
|
||||
{
|
||||
public class EnemySpawner : ITickable, IInitializable
|
||||
{
|
||||
readonly EnemyFacade.Factory _enemyFactory;
|
||||
readonly SignalBus _signalBus;
|
||||
readonly LevelBoundary _levelBoundary;
|
||||
readonly Settings _settings;
|
||||
|
||||
float _desiredNumEnemies;
|
||||
int _enemyCount;
|
||||
float _lastSpawnTime;
|
||||
|
||||
public EnemySpawner(
|
||||
Settings settings,
|
||||
LevelBoundary levelBoundary,
|
||||
SignalBus signalBus,
|
||||
EnemyFacade.Factory enemyFactory)
|
||||
{
|
||||
_enemyFactory = enemyFactory;
|
||||
_signalBus = signalBus;
|
||||
_levelBoundary = levelBoundary;
|
||||
_settings = settings;
|
||||
|
||||
_desiredNumEnemies = settings.NumEnemiesStartAmount;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_signalBus.Subscribe<EnemyKilledSignal>(OnEnemyKilled);
|
||||
}
|
||||
|
||||
void OnEnemyKilled()
|
||||
{
|
||||
_enemyCount--;
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
_desiredNumEnemies += _settings.NumEnemiesIncreaseRate * Time.deltaTime;
|
||||
|
||||
if (_enemyCount < (int)_desiredNumEnemies
|
||||
&& Time.realtimeSinceStartup - _lastSpawnTime > _settings.MinDelayBetweenSpawns)
|
||||
{
|
||||
SpawnEnemy();
|
||||
_enemyCount++;
|
||||
}
|
||||
}
|
||||
|
||||
void SpawnEnemy()
|
||||
{
|
||||
float speed = Random.Range(_settings.SpeedMin, _settings.SpeedMax);
|
||||
float accuracy = Random.Range(_settings.AccuracyMin, _settings.AccuracyMax);
|
||||
|
||||
var enemyFacade = _enemyFactory.Create(accuracy, speed);
|
||||
enemyFacade.Position = ChooseRandomStartPosition();
|
||||
|
||||
_lastSpawnTime = Time.realtimeSinceStartup;
|
||||
}
|
||||
|
||||
Vector3 ChooseRandomStartPosition()
|
||||
{
|
||||
var side = Random.Range(0, 3);
|
||||
var posOnSide = Random.Range(0, 1.0f);
|
||||
|
||||
float buffer = 2.0f;
|
||||
|
||||
switch (side)
|
||||
{
|
||||
case 0:
|
||||
// top
|
||||
{
|
||||
return new Vector3(
|
||||
_levelBoundary.Left + posOnSide * _levelBoundary.Width,
|
||||
_levelBoundary.Top + buffer, 0);
|
||||
}
|
||||
case 1:
|
||||
// right
|
||||
{
|
||||
return new Vector3(
|
||||
_levelBoundary.Right + buffer,
|
||||
_levelBoundary.Top - posOnSide * _levelBoundary.Height, 0);
|
||||
}
|
||||
case 2:
|
||||
// bottom
|
||||
{
|
||||
return new Vector3(
|
||||
_levelBoundary.Left + posOnSide * _levelBoundary.Width,
|
||||
_levelBoundary.Bottom - buffer, 0);
|
||||
}
|
||||
case 3:
|
||||
// left
|
||||
{
|
||||
return new Vector3(
|
||||
_levelBoundary.Left - buffer,
|
||||
_levelBoundary.Top - posOnSide * _levelBoundary.Height, 0);
|
||||
}
|
||||
}
|
||||
|
||||
throw Assert.CreateException();
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float SpeedMin;
|
||||
public float SpeedMax;
|
||||
|
||||
public float AccuracyMin;
|
||||
public float AccuracyMax;
|
||||
|
||||
public float NumEnemiesIncreaseRate;
|
||||
public float NumEnemiesStartAmount;
|
||||
|
||||
public float MinDelayBetweenSpawns = 0.5f;
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1daabbbef5fff9a4b9a2c94659ecdf57
|
||||
timeCreated: 1456420312
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
using UnityEngine;
|
||||
|
||||
#pragma warning disable 649
|
||||
|
||||
namespace Zenject.SpaceFighter
|
||||
{
|
||||
public class Explosion : MonoBehaviour, IPoolable<IMemoryPool>
|
||||
{
|
||||
[SerializeField]
|
||||
float _lifeTime;
|
||||
|
||||
[SerializeField]
|
||||
ParticleSystem _particleSystem;
|
||||
|
||||
float _startTime;
|
||||
|
||||
IMemoryPool _pool;
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (Time.realtimeSinceStartup - _startTime > _lifeTime)
|
||||
{
|
||||
_pool.Despawn(this);
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDespawned()
|
||||
{
|
||||
}
|
||||
|
||||
public void OnSpawned(IMemoryPool pool)
|
||||
{
|
||||
_particleSystem.Clear();
|
||||
_particleSystem.Play();
|
||||
|
||||
_startTime = Time.realtimeSinceStartup;
|
||||
_pool = pool;
|
||||
}
|
||||
|
||||
public class Factory : PlaceholderFactory<Explosion>
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 32bea9a1bf6287e4e936f2de85375172
|
||||
timeCreated: 1456680988
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Zenject.SpaceFighter
|
||||
{
|
||||
public class GameRestartHandler : IInitializable, IDisposable, ITickable
|
||||
{
|
||||
readonly SignalBus _signalBus;
|
||||
readonly Settings _settings;
|
||||
|
||||
bool _isDelaying;
|
||||
float _delayStartTime;
|
||||
|
||||
public GameRestartHandler(
|
||||
Settings settings,
|
||||
SignalBus signalBus)
|
||||
{
|
||||
_signalBus = signalBus;
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_signalBus.Subscribe<PlayerDiedSignal>(OnPlayerDied);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_signalBus.Unsubscribe<PlayerDiedSignal>(OnPlayerDied);
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
if (_isDelaying)
|
||||
{
|
||||
if (Time.realtimeSinceStartup - _delayStartTime > _settings.RestartDelay)
|
||||
{
|
||||
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OnPlayerDied()
|
||||
{
|
||||
// Wait a bit before restarting the scene
|
||||
_delayStartTime = Time.realtimeSinceStartup;
|
||||
_isDelaying = true;
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class Settings
|
||||
{
|
||||
public float RestartDelay;
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2244a42ec7f6e62498da08eaf3d40e0d
|
||||
timeCreated: 1456683096
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Zenject.SpaceFighter
|
||||
{
|
||||
public class LevelBoundary
|
||||
{
|
||||
readonly Camera _camera;
|
||||
|
||||
public LevelBoundary(Camera camera)
|
||||
{
|
||||
_camera = camera;
|
||||
}
|
||||
|
||||
public float Bottom
|
||||
{
|
||||
get { return -ExtentHeight; }
|
||||
}
|
||||
|
||||
public float Top
|
||||
{
|
||||
get { return ExtentHeight; }
|
||||
}
|
||||
|
||||
public float Left
|
||||
{
|
||||
get { return -ExtentWidth; }
|
||||
}
|
||||
|
||||
public float Right
|
||||
{
|
||||
get { return ExtentWidth; }
|
||||
}
|
||||
|
||||
public float ExtentHeight
|
||||
{
|
||||
get { return _camera.orthographicSize; }
|
||||
}
|
||||
|
||||
public float Height
|
||||
{
|
||||
get { return ExtentHeight * 2.0f; }
|
||||
}
|
||||
|
||||
public float ExtentWidth
|
||||
{
|
||||
get { return _camera.aspect * _camera.orthographicSize; }
|
||||
}
|
||||
|
||||
public float Width
|
||||
{
|
||||
get { return ExtentWidth * 2.0f; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6307c1ccd85f4f7478da6f3ec5a63996
|
||||
timeCreated: 1484693361
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user