Skip to content

Instantly share code, notes, and snippets.

@c0ffeeartc
Created August 6, 2018 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c0ffeeartc/bfe63a98d0f2920a1a38423345c4c6ae to your computer and use it in GitHub Desktop.
Save c0ffeeartc/bfe63a98d0f2920a1a38423345c4c6ae to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
public interface ITransformController
{
Vector3 PosWorld { get; set; }
Vector3 PosLocal { get; set; }
Quaternion RotWorld { get; set; }
Quaternion RotLocal { get; set; }
Vector3 ScaleLocal { get; set; }
Boolean HasChanged { get; set; }
Boolean IsActiveSelf { get; set; }
void Init ( Contexts contexts, GameCEntity entity );
void SyncFromTransformToEnt ( );
void SyncFromEntToTransform ( );
void SetParent ( GameCEntity otherEnt, Boolean worldPositionStays );
void CopyLocalFrom ( ITransformController other );
void RotateAround ( Vector3 point, Vector3 axis, Single angle );
}
using System.Collections.Generic;
using Entitas;
using Ent = GameCEntity;
namespace Custom.Sources
{
public class SyncTransformToEnt_IfHasChanged_System : IExecuteSystem, ICleanupSystem
{
public SyncTransformToEnt_IfHasChanged_System ( Contexts contexts )
{
_contexts = contexts;
_group = _contexts.gameC.GetGroup( GameCMatcher.Tr );
}
private Contexts _contexts;
private IGroup<Ent> _group;
private List<Ent> _buff = new List<Ent>( );
public void Execute ( )
{
var ents = _group.GetEntities( _buff );
for (var i = 0; i < ents.Count; i++)
{
var ent = ents[i];
var controller = ent.tr.Value;
if ( controller.HasChanged )
{
controller.SyncFromTransformToEnt( );
}
}
}
public void Cleanup ( )
{
var ents = _group.GetEntities( _buff );
foreach ( var ent in ents )
{
ent.tr.Value.HasChanged = false;
}
}
}
}
using System;
using UnityEngine;
using Ent = GameCEntity;
namespace Custom.Scripts
{
public sealed class UnityTransformController : MonoBehaviour, ITransformController
{
private Contexts _contexts;
private Ent _ent;
private Transform _tr;
public Transform Transform => _tr;
public Vector3 PosWorld
{
get
{
return _tr.position;
}
set
{
if ( _tr.position == value )
{
return;
}
_tr.position = value;
_ent.ReplaceTr_PosLocal( _tr.localPosition );
}
}
public Vector3 PosLocal
{
get
{
return _tr.localPosition;
}
set
{
if ( _tr.localPosition == value )
{
return;
}
_tr.localPosition = value;
_ent.ReplaceTr_PosLocal( value );
}
}
public Quaternion RotWorld
{
get
{
return _tr.rotation;
}
set
{
if ( _tr.rotation == value )
{
return;
}
_tr.rotation = value;
_ent.ReplaceTr_RotLocal( _tr.localRotation );
}
}
public Quaternion RotLocal
{
get
{
return _tr.localRotation;
}
set
{
if ( _tr.localRotation == value )
{
return;
}
_tr.localRotation = value;
_ent.ReplaceTr_RotLocal( value );
}
}
public Vector3 ScaleLocal
{
get
{
return _tr.localScale;
}
set
{
if ( _tr.localScale == value )
{
return;
}
_tr.localScale = value;
_ent.ReplaceTr_ScaleLocal( value );
}
}
public Boolean HasChanged
{
get
{
return _tr.hasChanged;
}
set
{
_tr.hasChanged = value;
}
}
public Boolean IsActiveSelf
{
get
{
return gameObject.activeSelf;
}
set
{
gameObject.SetActive(value);
}
}
public void Init ( Contexts contexts, Ent entity )
{
_tr = transform;
_contexts = contexts;
_ent = entity;
if ( _ent.hasTr_PosLocal )
{
_tr.localPosition = _ent.tr_PosLocal.Value;
}
else
{
_ent.AddTr_PosLocal( _tr.localPosition );
}
if ( _ent.hasTr_RotLocal )
{
_tr.localRotation = _ent.tr_RotLocal.Value;
}
else
{
_ent.AddTr_RotLocal( _tr.localRotation );
}
if ( _ent.hasTr_ScaleLocal )
{
_tr.localScale = _ent.tr_ScaleLocal.Value;
}
else
{
_ent.AddTr_ScaleLocal( _tr.localScale );
}
_tr.hasChanged = false;
}
public void SyncFromTransformToEnt ( )
{
var posLocal = _tr.localPosition;
if ( posLocal != _ent.tr_PosLocal.Value )
{
_ent.ReplaceTr_PosLocal( posLocal );
}
var rotLocal = _tr.localRotation;
if ( rotLocal != _ent.tr_RotLocal.Value )
{
_ent.ReplaceTr_RotLocal( rotLocal );
}
var scaleLocal = _tr.localScale;
if ( scaleLocal != _ent.tr_ScaleLocal.Value )
{
_ent.ReplaceTr_ScaleLocal( scaleLocal );
}
_tr.hasChanged = false;
}
public void SyncFromEntToTransform ( )
{
_tr.localPosition = _ent.tr_PosLocal.Value;
_tr.localRotation = _ent.tr_RotLocal.Value;
_tr.localScale = _ent.tr_ScaleLocal.Value;
_tr.hasChanged = false;
}
public void SetParent ( GameCEntity otherEnt, bool worldPositionStays )
{
var otherTr = otherEnt.viewComp.GameObj.transform;
_tr.SetParent( otherTr, worldPositionStays );
_ent.ReplaceTr_ParentGameEntId( otherEnt.id.Value );
if( otherEnt.hasViewParentKey )
{
_ent.ReplaceSetViewParentComp( otherEnt.viewParentKey.Value, worldPositionStays );
}
else if ( _ent.hasSetViewParentComp )
{
_ent.RemoveSetViewParentComp( );
}
SyncFromTransformToEnt( );
}
public void CopyLocalFrom ( ITransformController other )
{
_tr.localPosition = other.PosLocal;
_tr.localRotation = other.RotLocal;
_tr.localScale = other.ScaleLocal;
SyncFromTransformToEnt( );
}
public void RotateAround ( Vector3 point, Vector3 axis, Single angle )
{
_tr.RotateAround( point, axis, angle );
_ent.ReplaceTr_PosLocal( _tr.localPosition );
_ent.ReplaceTr_RotLocal( _tr.localRotation );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment