Post by Admin on Jul 16, 2016 3:07:23 GMT
Andreas Wilcox @svdvorak Aug 05 2015 22:22 Currently I'm stuck on a small dilemma on how to handle math operations and position components. As the Match-One example I've made a position component that has x, y and z fields but any time I need to do math operations I need to convert the component into a vector type, do the operations and then convert back again. And sometimes I need to convert the position into a Unity-vector too which creates yet another step. It works but the code gets quite a bit messier, any suggestions on how to handle this effectively?
|
Post by Admin on Jul 16, 2016 3:08:06 GMT
Brandon Catcho @bcatcho Aug 05 2015 23:35 Why don't you just use a Vector3 in your position component? I'm doing this right now for an RTS game. Unfortunately it looks funny: Entity.position.Position or Entity.position.WorldPosition You could give the entity a getter/setter that will automatically create a Vector3 for you. Since it is a value type it is very inexepensive:
public class PositionComponent : IComponent { public float x; public float y; public float z;
public Vector3 AsVector3 { get { return new Vector3(x,y,z);} set { x = value.x; y = value.y; z = value.z; } } }
|
Post by Admin on Jul 16, 2016 3:10:18 GMT
Andreas Wilcox @svdvorak Aug 05 2015 23:39 Well, as you said, Entity.position.position looks pretty terrible. I've already done as you've mentioned in the second point, I was just hoping that I could somehow get an automatic conversion so that the code won't be sprinkled with AsVector3. I guess I'll have to manage =) Though thinking about the code above, that would not trigger that the component has changed right?
|
Post by Admin on Jul 16, 2016 3:10:39 GMT
Brandon Catcho @bcatcho Aug 05 2015 23:41 Correct. One of the issues with the setter is that you will be modifying the component, which is not suggested in order to use the framework to it's fullest potential. But I do this anyway because it makes it easy to replace:
Entity.position.AsVector3 = Vector3.zero; var pos = Entity.position; Entity.ReplacePosition(pos.x, pos.y, pos.z);
|
Post by Admin on Jul 16, 2016 3:11:12 GMT
Andreas Wilcox @svdvorak Aug 05 2015 23:46 Ok, thanks. I did add an extension so that ReplacePosition takes a Vector3 which in turn just calls the normal ReplacePosition so the conversion back to Component is nicely hidden now.
|