Add namespaces

This commit is contained in:
Adam Ramberg 2020-03-18 02:04:33 +01:00
parent a90a336980
commit 60bfe01b84
8 changed files with 141 additions and 121 deletions

View File

@ -2,27 +2,30 @@
using UnityEngine.Assertions;
using UnityAtoms.BaseAtoms;
public class DestroyMe : MonoBehaviour
namespace UnityAtoms.Examples
{
[SerializeField]
FloatReference _delay = new FloatReference(-1f);
void Start()
public class DestroyMe : MonoBehaviour
{
Assert.IsNotNull(_delay);
if (_delay.Value >= 0f)
[SerializeField]
FloatReference _delay = new FloatReference(-1f);
void Start()
{
Destroy(gameObject, _delay.Value);
Assert.IsNotNull(_delay);
if (_delay.Value >= 0f)
{
Destroy(gameObject, _delay.Value);
}
}
}
public void DestroyImmediate() => Destroy(gameObject);
public void DestroyImmediate() => Destroy(gameObject);
public void DestroyIfZeroOfBelow(int value)
{
if (value <= 0)
public void DestroyIfZeroOfBelow(int value)
{
DestroyImmediate();
if (value <= 0)
{
DestroyImmediate();
}
}
}
}

View File

@ -1,17 +1,20 @@
using UnityEngine;
public static class Rigidbody2DExtensions
namespace UnityAtoms.Examples
{
public static void Move(this Rigidbody2D body, Vector2 input, float speed, float deltaTime)
public static class Rigidbody2DExtensions
{
var direction = input.normalized;
var targetVelocity = direction * speed;
body.velocity = Vector2.Lerp(body.velocity, targetVelocity, 10f * deltaTime);
if (direction.magnitude > 0f)
public static void Move(this Rigidbody2D body, Vector2 input, float speed, float deltaTime)
{
float lookAtTargetAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
body.transform.rotation = Quaternion.AngleAxis(lookAtTargetAngle, Vector3.forward);
var direction = input.normalized;
var targetVelocity = direction * speed;
body.velocity = Vector2.Lerp(body.velocity, targetVelocity, 10f * deltaTime);
if (direction.magnitude > 0f)
{
float lookAtTargetAngle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
body.transform.rotation = Quaternion.AngleAxis(lookAtTargetAngle, Vector3.forward);
}
}
}
}

View File

@ -1,11 +1,14 @@
using UnityEngine;
using UnityAtoms.BaseAtoms;
public class UnitHealth : MonoBehaviour
namespace UnityAtoms.Examples
{
public int Health { get => _health.Value; set => _health.Value = value; }
public class UnitHealth : MonoBehaviour
{
public int Health { get => _health.Value; set => _health.Value = value; }
[SerializeField]
private IntReference _health;
}
[SerializeField]
private IntReference _health;
}
}

View File

@ -1,16 +1,19 @@
using UnityEngine;
using UnityAtoms.FSM;
public class GameStateDispatcher : MonoBehaviour
namespace UnityAtoms.Examples
{
[SerializeField]
private FiniteStateMachineReference _gameStateRef;
public void DispatchGameOverIfDead(int health)
public class GameStateDispatcher : MonoBehaviour
{
if (health <= 0)
[SerializeField]
private FiniteStateMachineReference _gameStateRef;
public void DispatchGameOverIfDead(int health)
{
_gameStateRef.Machine.Dispatch("SetGameOver");
if (health <= 0)
{
_gameStateRef.Machine.Dispatch("SetGameOver");
}
}
}
}
}

View File

@ -1,48 +1,51 @@
using UnityAtoms.BaseAtoms;
using UnityEngine;
public class PlayerShooting : MonoBehaviour
namespace UnityAtoms.Examples
{
[SerializeField]
private GameObject _projectile;
[SerializeField]
private StringConstant _playerTag;
void Update()
public class PlayerShooting : MonoBehaviour
{
var shootDirection = Vector3.zero;
var rot = Quaternion.identity;
[SerializeField]
private GameObject _projectile;
if (Input.GetKeyDown(KeyCode.UpArrow))
{
shootDirection = Vector3.up;
rot = Quaternion.Euler(0f, 0f, 90f);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
shootDirection = Vector3.down;
rot = Quaternion.Euler(0f, 0f, -90f);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
shootDirection = Vector3.right;
rot = Quaternion.Euler(0f, 0f, 0f);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
shootDirection = Vector3.left;
rot = Quaternion.Euler(0f, 0f, 180f);
}
[SerializeField]
private StringConstant _playerTag;
if (shootDirection != Vector3.zero)
void Update()
{
var spawnPos = transform.position + shootDirection * 0.6f;
var projectile = Instantiate(_projectile, spawnPos, rot);
projectile.GetComponent<DecreaseHealth>().TagsAffected.Remove(_playerTag); // Turn off friendly fire
var shootDirection = Vector3.zero;
var rot = Quaternion.identity;
if (Input.GetKeyDown(KeyCode.UpArrow))
{
shootDirection = Vector3.up;
rot = Quaternion.Euler(0f, 0f, 90f);
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
shootDirection = Vector3.down;
rot = Quaternion.Euler(0f, 0f, -90f);
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
shootDirection = Vector3.right;
rot = Quaternion.Euler(0f, 0f, 0f);
}
else if (Input.GetKeyDown(KeyCode.LeftArrow))
{
shootDirection = Vector3.left;
rot = Quaternion.Euler(0f, 0f, 180f);
}
if (shootDirection != Vector3.zero)
{
var spawnPos = transform.position + shootDirection * 0.6f;
var projectile = Instantiate(_projectile, spawnPos, rot);
projectile.GetComponent<DecreaseHealth>().TagsAffected.Remove(_playerTag); // Turn off friendly fire
}
}
}
}
}

View File

@ -1,42 +1,43 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityAtoms.BaseAtoms;
using UnityAtoms;
using UnityAtoms.Tags;
public class DecreaseHealth : MonoBehaviour
namespace UnityAtoms.Examples
{
public List<StringConstant> TagsAffected { get => _tags; }
[SerializeField]
private IntReference _decreaseBy;
[SerializeField]
private List<StringConstant> _tags;
[SerializeField]
private VoidBaseEventReference _didCollide;
void Start()
public class DecreaseHealth : MonoBehaviour
{
Assert.IsNotNull(_decreaseBy);
Assert.IsNotNull(_tags);
}
public List<StringConstant> TagsAffected { get => _tags; }
public void Do(Collider2D collider)
{
if (collider == null) return;
[SerializeField]
private IntReference _decreaseBy;
if (collider.gameObject.HasAnyTag(_tags))
[SerializeField]
private List<StringConstant> _tags;
[SerializeField]
private VoidBaseEventReference _didCollide;
void Start()
{
collider.GetComponent<UnitHealth>().Health -= _decreaseBy;
Assert.IsNotNull(_decreaseBy);
Assert.IsNotNull(_tags);
}
if (_didCollide != null && _didCollide.Event != null)
public void Do(Collider2D collider)
{
_didCollide.Event.Raise();
if (collider == null) return;
if (collider.gameObject.HasAnyTag(_tags))
{
collider.GetComponent<UnitHealth>().Health -= _decreaseBy;
}
if (_didCollide != null && _didCollide.Event != null)
{
_didCollide.Event.Raise();
}
}
}
}
}

View File

@ -1,26 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine;
using UnityAtoms.BaseAtoms;
[RequireComponent(typeof(Rigidbody2D))]
public class MoveInDirection : MonoBehaviour
namespace UnityAtoms.Examples
{
public float Speed { set => _speed.Value = value; }
[SerializeField]
private FloatReference _speed;
private Rigidbody2D rb;
void Start()
[RequireComponent(typeof(Rigidbody2D))]
public class MoveInDirection : MonoBehaviour
{
rb = GetComponent<Rigidbody2D>();
rb.isKinematic = true;
}
public float Speed { set => _speed.Value = value; }
void Update()
{
rb.velocity = transform.right * _speed.Value;
[SerializeField]
private FloatReference _speed;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.isKinematic = true;
}
void Update()
{
rb.velocity = transform.right * _speed.Value;
}
}
}

View File

@ -1,7 +1,10 @@
using UnityEngine;
using UnityEngine.SceneManagement;
public class RestartCurrentScene : MonoBehaviour
namespace UnityAtoms.Examples
{
public void Do() => SceneManager.LoadScene(SceneManager.GetActiveScene().name);
public class RestartCurrentScene : MonoBehaviour
{
public void Do() => SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}