Skip to content

Commit

Permalink
Refactored "Position" out of clientside entities and into a Transform…
Browse files Browse the repository at this point in the history
…Component
  • Loading branch information
spoogemonster committed Jul 28, 2013
1 parent ed5eec4 commit d104478
Show file tree
Hide file tree
Showing 56 changed files with 270 additions and 147 deletions.
1 change: 1 addition & 0 deletions CGO/ClientGameObject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="Component\Renderable\MobSpriteComponent.cs" />
<Compile Include="Component\Renderable\SpriteComponent.cs" />
<Compile Include="Component\StatusEffects\StatusEffectComp.cs" />
<Compile Include="Component\Transform\TransformComponent.cs" />
<Compile Include="ContextMenuEntry.cs" />
<Compile Include="Entity.cs" />
<Compile Include="EntityFactory.cs" />
Expand Down
8 changes: 2 additions & 6 deletions CGO/Component/Collidable/CollidableComponent.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using ClientInterfaces;
using ClientInterfaces.Collision;
using ClientInterfaces.GOC;
using GorgonLibrary;
using SS13.IoC;
using SS13_Shared;
using SS13_Shared.GO;
using SS13_Shared.GO.Component.Collidable;

Expand Down Expand Up @@ -41,8 +37,8 @@ private RectangleF OffsetAABB
get
{// Return tweaked AABB
if (currentAABB != null)
return new RectangleF(currentAABB.Left + Owner.Position.X - (currentAABB.Width / 2) + tweakAABB.W,
currentAABB.Top + Owner.Position.Y - (currentAABB.Height / 2) + tweakAABB.X,
return new RectangleF(currentAABB.Left + Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.X - (currentAABB.Width / 2) + tweakAABB.W,
currentAABB.Top + Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y - (currentAABB.Height / 2) + tweakAABB.X,
currentAABB.Width - (tweakAABB.W - tweakAABB.Y),
currentAABB.Height - (tweakAABB.X - tweakAABB.Z));
else
Expand Down
9 changes: 3 additions & 6 deletions CGO/Component/Collider/ColliderComponent.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq;
using System.Drawing;
using ClientInterfaces.Collision;
using ClientInterfaces.GOC;
using GorgonLibrary;
using SS13.IoC;
using SS13_Shared;
using SS13_Shared.GO;

namespace CGO
Expand Down Expand Up @@ -33,8 +30,8 @@ public RectangleF OffsetAABB
get
{ // Return tweaked AABB
if (currentAABB != null)
return new RectangleF(currentAABB.Left + Owner.Position.X - (currentAABB.Width / 2) + tweakAABB.W,
currentAABB.Top + Owner.Position.Y - (currentAABB.Height / 2) + tweakAABB.X,
return new RectangleF(currentAABB.Left + Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.X - (currentAABB.Width / 2) + tweakAABB.W,
currentAABB.Top + Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y - (currentAABB.Height / 2) + tweakAABB.X,
currentAABB.Width - (tweakAABB.W - tweakAABB.Y),
currentAABB.Height - (tweakAABB.X - tweakAABB.Z));
else
Expand Down
11 changes: 4 additions & 7 deletions CGO/Component/Light/PointLightComponent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Drawing;
using ClientInterfaces.GOC;
using ClientInterfaces.Lighting;
using ClientInterfaces.Map;
using GorgonLibrary;
using SS13.IoC;
using SS13_Shared;
Expand Down Expand Up @@ -40,9 +37,9 @@ public override void OnAdd(GameObject.IEntity owner)

_light.SetRadius(_lightRadius);
_light.SetColor(255, (int)_lightColor.X, (int)_lightColor.Y, (int)_lightColor.Z);
_light.Move(Owner.Position + _lightOffset);
_light.Move(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position + _lightOffset);
_light.SetMask(_mask);
Owner.OnMove += OnMove;
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).OnMove += OnMove;
}

public override void HandleNetworkMessage(IncomingEntityComponentMessage message)
Expand Down Expand Up @@ -97,14 +94,14 @@ protected void SetMode(LightModeClass mode)

public override void OnRemove()
{
Owner.OnMove -= OnMove;
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).OnMove -= OnMove;
IoCManager.Resolve<ILightManager>().RemoveLight(_light);
base.OnRemove();
}

private void OnMove(object sender, VectorEventArgs args)
{
_light.Move(Owner.Position + _lightOffset);
_light.Move(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position + _lightOffset);
}

public override void Update(float frameTime)
Expand Down
24 changes: 11 additions & 13 deletions CGO/Component/Mover/KeyBindingMoverComponent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using SS13_Shared;
using SS13_Shared;
using SS13_Shared.GO;
using GorgonLibrary;

Expand Down Expand Up @@ -153,14 +151,14 @@ private void SetMoveDir(Constants.MoveDirs movedir)

public virtual void SendPositionUpdate()
{
Owner.SendComponentNetworkMessage(this, Lidgren.Network.NetDeliveryMethod.ReliableUnordered, Owner.Position.X, Owner.Position.Y, Owner.Velocity.X, Owner.Velocity.Y);
Owner.SendComponentNetworkMessage(this, Lidgren.Network.NetDeliveryMethod.ReliableUnordered, Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.X, Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y, Owner.Velocity.X, Owner.Velocity.Y);
}

public void PlainTranslate(float x, float y)
{
var delta = new Vector2D(x, y) - Owner.Position;
var delta = new Vector2D(x, y) - Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position;

Owner.Position = new Vector2D(x, y);
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position = new Vector2D(x, y);

if (delta.X > 0 && delta.Y > 0)
SetMoveDir(Constants.MoveDirs.southeast);
Expand All @@ -179,7 +177,7 @@ public void PlainTranslate(float x, float y)
if (delta.Y < 0 && delta.X == 0)
SetMoveDir(Constants.MoveDirs.north);

Owner.Moved();
//Owner.Moved();
}

/// <summary>
Expand All @@ -188,7 +186,7 @@ public void PlainTranslate(float x, float y)
/// <param name="translationVector"></param>
public virtual void Translate(Vector2D translationVector)
{
var oldPos = Owner.Position;
var oldPos = Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position;

var translated = TryTranslate(translationVector, false); //Only bump once...
if (!translated)
Expand All @@ -197,7 +195,7 @@ public virtual void Translate(Vector2D translationVector)
translated = TryTranslate(new Vector2D(0, translationVector.Y), true);
if (translated)
{
var delta = Owner.Position - oldPos;
var delta = Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position - oldPos;
if (delta.X > 0 && delta.Y > 0)
SetMoveDir(Constants.MoveDirs.southeast);
if (delta.X > 0 && delta.Y < 0)
Expand All @@ -223,7 +221,7 @@ public virtual void Translate(Vector2D translationVector)
_moveTimeCache = 0;
}
}
Owner.Moved();
//Owner.Moved();
}

/// <summary>
Expand All @@ -234,16 +232,16 @@ public virtual void Translate(Vector2D translationVector)
/// <returns></returns>
public bool TryTranslate(Vector2D translationVector, bool suppressBump)
{
var oldPosition = Owner.Position;
Owner.Position += translationVector; // We move the sprite here rather than the position, as we can then use its updated AABB values.
var oldPosition = Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position;
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position += translationVector; // We move the sprite here rather than the position, as we can then use its updated AABB values.
//Check collision.
var reply = Owner.SendMessage(this, ComponentFamily.Collider, ComponentMessageType.CheckCollision, false);
if (reply.MessageType == ComponentMessageType.CollisionStatus)
{
var colliding = (bool)reply.ParamsList[0];
if (colliding) //Collided, reset position and return false.
{
Owner.Position = oldPosition;
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position = oldPosition;
return false;
}
}
Expand Down
15 changes: 6 additions & 9 deletions CGO/Component/Mover/NetworkMoverComponent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GorgonLibrary;
using SS13_Shared;
using SS13_Shared.GO;
Expand Down Expand Up @@ -63,11 +60,11 @@ public override void Update(float frameTime)
if (interpolating)
{
movedtime = movedtime + frameTime;
Vector2D delta = targetPosition - Owner.Position;
Vector2D delta = targetPosition - Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position;
if (movedtime >= movetime)
{
//targetPosition = Owner.Position;
Owner.Position = targetPosition;
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position = targetPosition;
//startPosition = Owner.Position;
startPosition = targetPosition;
interpolating = false;
Expand All @@ -77,22 +74,22 @@ public override void Update(float frameTime)
{
float X = Ease(movedtime, startPosition.X, targetPosition.X, movetime);
float Y = Ease(movedtime, startPosition.Y, targetPosition.Y, movetime);
Owner.Position = new Vector2D(X, Y);
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position = new Vector2D(X, Y);
}

Owner.Moved();
//Owner.Moved();
}
}

private void Translate(float x, float y, float velx, float vely)
{
Vector2D delta = new Vector2D(x, y) - Owner.Position;
Vector2D delta = new Vector2D(x, y) - Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position;
interpolating = true;
movedtime = 0;

//Owner.Position = new Vector2D(x, y);
targetPosition = new Vector2D(x, y);
startPosition = Owner.Position;
startPosition = Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position;

if (delta.X > 0 && delta.Y > 0)
SetMoveDir(Constants.MoveDirs.southeast);
Expand Down
16 changes: 7 additions & 9 deletions CGO/Component/Mover/SlaveMoverComponent.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using ClientInterfaces.GOC;
using ClientInterfaces.GOC;
using GorgonLibrary;
using SS13_Shared;
using SS13_Shared.GO;
Expand Down Expand Up @@ -46,8 +44,8 @@ public override void OnRemove()
private void Attach(int uid)
{
_master = EntityManager.Singleton.GetEntity(uid);
_master.OnMove += HandleOnMove;
Translate(_master.Position);
_master.GetComponent<TransformComponent>(ComponentFamily.Transform).OnMove += HandleOnMove;
Translate(_master.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
GetMasterMoveDirection();
}

Expand All @@ -66,7 +64,7 @@ private void Detach()
{
if (_master == null) return;

_master.OnMove -= HandleOnMove;
_master.GetComponent<TransformComponent>(ComponentFamily.Transform).OnMove -= HandleOnMove;
_master = null;
}

Expand All @@ -78,9 +76,9 @@ private void HandleOnMove(object sender, VectorEventArgs args)

private void Translate(Vector2D toPosition)
{
Vector2D delta = toPosition - Owner.Position;
Vector2D delta = toPosition - Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position;

Owner.Position = toPosition;
Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position = toPosition;
/*
if (delta.X > 0 && delta.Y > 0)
SetMoveDir(Constants.MoveDirs.southeast);
Expand All @@ -100,7 +98,7 @@ private void Translate(Vector2D toPosition)
SetMoveDir(Constants.MoveDirs.north);
*/

Owner.Moved();
//Owner.Moved();
}

private void SetMoveDir(Constants.MoveDirs movedir)
Expand Down
11 changes: 5 additions & 6 deletions CGO/Component/Renderable/MobSpriteComponent.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using GorgonLibrary;
using SS13_Shared;
using SS13_Shared.GO;
Expand Down Expand Up @@ -137,16 +136,16 @@ public void LoadSprites()
public override void Render(Vector2D topLeft, Vector2D bottomRight)
{
if (!visible) return;
if (Owner.Position.X < topLeft.X
|| Owner.Position.X > bottomRight.X
|| Owner.Position.Y < topLeft.Y
|| Owner.Position.Y > bottomRight.Y)
if (Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.X < topLeft.X
|| Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.X > bottomRight.X
|| Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y < topLeft.Y
|| Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y > bottomRight.Y)
return;

base.Render(topLeft, bottomRight);

if (_speechBubble != null)
_speechBubble.Draw(Owner.Position, ClientWindowData.Singleton.ScreenOrigin, currentBaseSprite);
_speechBubble.Draw(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position, ClientWindowData.Singleton.ScreenOrigin, currentBaseSprite);
}

public override void HandleComponentState(dynamic state)
Expand Down
15 changes: 7 additions & 8 deletions CGO/Component/Renderable/SpriteComponent.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ClientInterfaces;
using ClientInterfaces.GOC;
using ClientInterfaces.Resource;
using GorgonLibrary.Graphics;
Expand Down Expand Up @@ -43,7 +42,7 @@ public RectangleF AABB

public override float Bottom
{
get { return Owner.Position.Y + (GetActiveDirectionalSprite().AABB.Height / 2); }
get { return Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y + (GetActiveDirectionalSprite().AABB.Height / 2); }
}

private void BuildDirectionalSprites()
Expand Down Expand Up @@ -181,7 +180,7 @@ protected virtual bool WasClicked(PointF worldPos)

Sprite spriteToCheck = GetActiveDirectionalSprite();

System.Drawing.RectangleF AABB = new System.Drawing.RectangleF(Owner.Position.X - (spriteToCheck.Width / 2), Owner.Position.Y - (spriteToCheck.Height / 2), spriteToCheck.Width, spriteToCheck.Height);
System.Drawing.RectangleF AABB = new System.Drawing.RectangleF(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.X - (spriteToCheck.Width / 2), Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y - (spriteToCheck.Height / 2), spriteToCheck.Width, spriteToCheck.Height);
if (!AABB.Contains(worldPos)) return false;

System.Drawing.Point spritePosition = new System.Drawing.Point((int)(worldPos.X - AABB.X + spriteToCheck.ImageOffset.X), (int)(worldPos.Y - AABB.Y + spriteToCheck.ImageOffset.Y));
Expand Down Expand Up @@ -271,13 +270,13 @@ where c.DrawDepth < DrawDepth

Sprite spriteToRender = GetActiveDirectionalSprite();

var renderPos = ClientWindowData.WorldToScreen(Owner.Position);
var renderPos = ClientWindowData.WorldToScreen(Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position);
SetSpriteCenter(spriteToRender, renderPos);

if (Owner.Position.X + spriteToRender.AABB.Right < topLeft.X
|| Owner.Position.X > bottomRight.X
|| Owner.Position.Y + spriteToRender.AABB.Bottom < topLeft.Y
|| Owner.Position.Y > bottomRight.Y)
if (Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.X + spriteToRender.AABB.Right < topLeft.X
|| Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.X > bottomRight.X
|| Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y + spriteToRender.AABB.Bottom < topLeft.Y
|| Owner.GetComponent<TransformComponent>(ComponentFamily.Transform).Position.Y > bottomRight.Y)
return;

spriteToRender.HorizontalFlip = flip;
Expand Down
47 changes: 47 additions & 0 deletions CGO/Component/Transform/TransformComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using GorgonLibrary;
using SS13_Shared;
using SS13_Shared.GO;

namespace CGO
{
public class TransformComponent : GameObjectComponent
{
private Vector2D _position = Vector2D.Zero;

public Vector2D Position
{
get { return _position; }
set
{
_position = value;

if (OnMove != null) OnMove(this, new VectorEventArgs(_position));
}
}

public event EventHandler<VectorEventArgs> OnMove;

public TransformComponent() :base()
{
Family = ComponentFamily.Transform;
}

public float X
{
get { return Position.X; }
set { Position = new Vector2D(value, Position.Y); }
}

public float Y
{
get { return Position.Y; }
set { Position = new Vector2D(Position.X, value);}
}

public override void Shutdown()
{
Position = Vector2D.Zero;
}
}
}
Loading

0 comments on commit d104478

Please sign in to comment.