Skip to content

Instantly share code, notes, and snippets.

@danielmahon
Created November 8, 2021 15:53
Show Gist options
  • Save danielmahon/375041e09c997bfaf3b537706872baed to your computer and use it in GitHub Desktop.
Save danielmahon/375041e09c997bfaf3b537706872baed to your computer and use it in GitHub Desktop.
// system example
public class SystemManager : MonoBehaviour {
[SerializeField] List<CoreSystem> _systems;
// Start is called before the first frame update
void Start() {
foreach (var system in _systems) {
system.Init();
}
}
void FixedUpdate() {
foreach (var system in _systems) {
system.OnFixedUpdate();
}
}
// public T? GetSystem<T>() where T : CoreSystem {
// return _systems.Find(system => system is T) as T;
// }
}
[CreateAssetMenu(fileName = "PlayerSystem", menuName = "Systems/PlayerSystem", order = 1)]
public class PlayerSystem : CoreSystem {
// ...
List<PlayerController> _players = new List<PlayerController>();
public override void Init() {
SpawnPlayer();
}
public override void OnFixedUpdate() {
foreach (var player in _players.ToArray()) {
player.OnFixedUpdate();
}
}
void SpawnPlayer() {
// spawn player
var player = Instantiate(_playerPrefab, _spawnPosition, Quaternion.identity);
player.Init(_colorSystem.PaddleColor);
_players.Add(player);
}
}
public class PlayerController : MonoBehaviour {
[SerializeField] List<Ability> _abilities;
// ...
public void Init(Color paddleColor) {
_rigidbody = GetComponent<Rigidbody>();
_renderer.material.SetColor("_Color", paddleColor);
}
public void OnFixedUpdate() {
var movement = _moveSpeed * Time.fixedDeltaTime * _movementInput;
foreach (var modifier in _abilities.OfType<IMovementModifier>()) {
movement = modifier.ModifyMovement(movement);
}
var nextPosition = transform.position + movement;
nextPosition.x = Mathf.Clamp(nextPosition.x, -_xMovementRange, _xMovementRange);
// update movement
_rigidbody.MovePosition(nextPosition);
}
public void OnMove(InputAction.CallbackContext context) {
var moveInput = context.ReadValue<Vector2>().normalized;
_movementInput = new Vector3(moveInput.x, 0, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment