Skip to content

Instantly share code, notes, and snippets.

@FNGgames
Last active March 7, 2019 11:19
Show Gist options
  • Save FNGgames/c7445d5aea6bb092b209dd2c72561f2f to your computer and use it in GitHub Desktop.
Save FNGgames/c7445d5aea6bb092b209dd2c72561f2f to your computer and use it in GitHub Desktop.
Entitias Collisions
using Entitas;
using System.Collections.Generic;
public abstract class CollisionSystem : ReactiveSystem<InputEntity>
{
protected Contexts _contexts;
public CollisionSystem(Contexts contexts) : base(contexts.input)
{
_contexts = contexts;
}
protected override ICollector<InputEntity> GetTrigger(IContext<InputEntity> context)
{
return context.CreateCollector(InputMatcher.Collision);
}
protected override bool Filter(InputEntity entity)
{
if (!entity.hasCollision) return false;
return Filter(entity.collision.self, entity.collision.other);
}
protected abstract bool Filter(GameEntity self, GameEntity other);
protected override void Execute(List<InputEntity> entities)
{
foreach (var e in entities)
Execute(e.collision.self, e.collision.other);
}
protected abstract void Execute(GameEntity self, GameEntity other);
}
[Input, Cleanup(CleanupMode.DestroyEntity)]
public sealed class CollisionComponent : IComponent
{
public GameEntity self;
public GameEntity other;
}
using UnityEngine;
public static class EntitasUnityHelpers
{
public static bool GetEntity(this GameObject go, out GameEntity entity)
{
entity = null;
var view = go.GetComponent<IView>();
if (view == null || view.Entity == null || !view.Entity.isEnabled) return false;
entity = view.Entity;
return true;
}
}
public class PlayerCollisionWithSceneExitSystem : CollisionSystem
{
public PlayerCollisionWithSceneExitSystem(Contexts contexts) : base(contexts)
{
}
protected override bool Filter(GameEntity self, GameEntity other)
{
return self.hasSceneExit && other.isPlayer;
}
protected override void Execute(GameEntity self, GameEntity other)
{
_contexts.state.sceneManagerEntity.ReplaceLoadSceneRequest(self.sceneExit.sceneName);
_contexts.state.sceneManagerEntity.ReplaceSceneEntranceRequest(self.sceneExit.targetEntryPoint);
}
}
// this is the collision-relevant parts of the View monobehaviour
public class View : MonoBehaviour, IView
{
public GameEntity Entity {get; set;} // this gets assigned by view service
protected virtual void OnCollisionEnter2D(Collision2D collision)
{
if (Entity == null) return;
GameEntity otherEntity = null;
if (collision.gameObject.GetEntity(out otherEntity))
_contexts.input.CreateEntity().AddCollision(Entity, otherEntity);
}
// also add OnTriggerEnter2D in the same way
// in my games these events are not distinguished from eachother
// ... irrelevant code removed ...
}
@FNGgames
Copy link
Author

FNGgames commented Mar 7, 2019

Base collision system, collision component, relevant part of View code to propagate collisions from Unity, example collision system that handles player colliding with scene exit triggers, and a helper to get the entity from the gameobject.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment