Skip to content

Commit

Permalink
Thn: Huge progress on object movement
Browse files Browse the repository at this point in the history
  • Loading branch information
CallumDev committed Dec 28, 2018
1 parent 30f45f0 commit 8a98e47
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 282 deletions.
37 changes: 34 additions & 3 deletions src/LibreLancer/Thn/Cutscene.cs
Expand Up @@ -17,6 +17,31 @@ public class ThnObject
public DynamicLight Light;
public ThnEntity Entity;
public ThnCameraTransform Camera;
public Vector3 LightDir;
public Hardpoint HpMount;
public void Update()
{
if (Object != null)
{
if(HpMount == null)
Object.Transform = Rotate * Matrix4.CreateTranslation(Translate);
else {
var tr = HpMount.Transform;
tr.Invert();
Object.Transform = tr * (Rotate * Matrix4.CreateTranslation(Translate));
}
}
if(Camera != null)
{
Camera.Orientation = Rotate;
Camera.Position = Translate;
}
if(Light != null)
{
Light.Light.Position = Translate;
Light.Light.Direction = (new Vector4(LightDir.Normalized(), 0) * Rotate).Xyz.Normalized();
}
}
}

public interface IThnRoutine
Expand Down Expand Up @@ -86,14 +111,15 @@ public Cutscene(IEnumerable<ThnScript> scripts, FreelancerGame game, GameObject
obj.Entity = kv.Value;
Vector3 transform = kv.Value.Position ?? Vector3.Zero;
obj.Object.Transform = (kv.Value.RotationMatrix ?? Matrix4.Identity) * Matrix4.CreateTranslation(transform);
obj.HpMount = playerShip.GetHardpoint("HpMount");
World.Objects.Add(obj.Object);
Objects.Add(kv.Key, obj);
continue;
}
//Create objects
if (kv.Value.Type == EntityTypes.Compound)
{
bool getHpMount = false;
//Fetch model
IDrawable drawable;
switch (kv.Value.MeshCategory.ToLowerInvariant())
Expand All @@ -103,6 +129,7 @@ public Cutscene(IEnumerable<ThnScript> scripts, FreelancerGame game, GameObject
break;
case "ship":
case "spaceship":
getHpMount = true;
var sh = game.GameData.GetShip(kv.Value.Template);
drawable = sh.Drawable;
break;
Expand Down Expand Up @@ -136,7 +163,9 @@ public Cutscene(IEnumerable<ThnScript> scripts, FreelancerGame game, GameObject
obj.Object = new GameObject(drawable, game.ResourceManager, false);
obj.Object.Name = kv.Value.Name;
obj.Object.PhysicsComponent = null; //Jitter seems to interfere with directly setting orientation
var r = (ModelRenderer)obj.Object.RenderComponent;
if(getHpMount)
obj.HpMount = obj.Object.GetHardpoint("HpMount");
var r = (ModelRenderer)obj.Object.RenderComponent;
r.LightGroup = kv.Value.LightGroup;
r.LitDynamic = (kv.Value.ObjectFlags & ThnObjectFlags.LitDynamic) == ThnObjectFlags.LitDynamic;
r.LitAmbient = (kv.Value.ObjectFlags & ThnObjectFlags.LitAmbient) == ThnObjectFlags.LitAmbient;
Expand Down Expand Up @@ -171,6 +200,7 @@ public Cutscene(IEnumerable<ThnScript> scripts, FreelancerGame game, GameObject
lt.Active = kv.Value.LightProps.On;
lt.Light = kv.Value.LightProps.Render;
obj.Light = lt;
obj.LightDir = lt.Light.Direction;
if (kv.Value.RotationMatrix.HasValue)
{
var m = kv.Value.RotationMatrix.Value;
Expand Down Expand Up @@ -199,7 +229,7 @@ public Cutscene(IEnumerable<ThnScript> scripts, FreelancerGame game, GameObject
World.Objects.Add(obj.Object);
}
obj.Entity = kv.Value;
Objects.Add(kv.Key, obj);
Objects[kv.Key] = obj;
}
}
evs.Sort((x, y) => x.Time.CompareTo(y.Time));
Expand Down Expand Up @@ -258,6 +288,7 @@ public void _Update(TimeSpan delta)
var ev = events.Dequeue();
ProcessEvent(ev);
}
foreach (var obj in Objects.Values) obj.Update();
camera.Update();
World.Update(delta);
}
Expand Down
134 changes: 52 additions & 82 deletions src/LibreLancer/Thn/Events/AttachEntityRunner.cs
Expand Up @@ -8,50 +8,51 @@ namespace LibreLancer
[ThnEventRunner(EventTypes.AttachEntity)]
public class AttachEntityRunner : IThnEventRunner
{
class AttachCameraToObject : IThnRoutine
class AttachRoutine : IThnRoutine
{
public float Duration;
public ThnCameraTransform Camera;
public ThnObject Child;
public Vector3 Offset;
public GameObject Object;
public ThnObject Parent;
public GameObject Part;
public bool Position;
public bool Orientation;
public bool LookAt;
Func<Vector3> lookFunc;
double t = 0;
public bool Run(Cutscene cs, double delta)
{
Matrix4 transform;
if (Part != null)
transform = Part.GetTransform();
else
transform = Object.GetTransform();
Vector3 translate = Parent.Translate;
Matrix4 rotate = Parent.Rotate;
if (Part != null && (Position || Orientation))
{
var tr = Part.GetTransform();
if (Position) translate = tr.ExtractTranslation();
if (Orientation) rotate = Matrix4.CreateFromQuaternion(tr.ExtractRotation());
}
t += delta;
if (t > Duration)
if (LookAt)
{
if (LookAt)
Camera.LookAt = null;
return false;
if (lookFunc == null)
{
if (Part != null) lookFunc = () => Part.Transform.Transform(Vector3.Zero);
else lookFunc = () => Parent.Translate;
}
}
else
if (Child.Camera != null) Child.Camera.LookAt = null;

if (t > Duration)
if (LookAt) Child.Camera.LookAt = null;
if(Offset != Vector3.Zero) { //TODO: This can be optimised
var tr = rotate * Matrix4.CreateTranslation(translate) * Matrix4.CreateTranslation(Offset);
translate = tr.ExtractTranslation();
rotate = Matrix4.CreateFromQuaternion(tr.ExtractRotation());
}
if (Position)
Camera.Position = transform.ExtractTranslation();
Child.Translate = translate;
if (Orientation)
Camera.Orientation = Matrix4.CreateFromQuaternion(transform.ExtractRotation());
return true;
}
}

class DetachObject : IThnRoutine
{
public float Duration;
public GameObject Object;
double t = 0;
public bool Run(Cutscene cs, double delta)
{
t += delta;
if (t > Duration)
return false;

Child.Rotate = rotate;
return true;
}
}
Expand All @@ -78,64 +79,33 @@ public void Process(ThnEvent ev, Cutscene cs)
flags = ThnEnum.Check<AttachFlags>(tmp);
ev.Properties.TryGetVector3("offset", out offset);
//Attach GameObjects to eachother
if (objA.Object != null && objB.Object != null)
GameObject part = null;
string tgt_part;
ev.Properties.TryGetValue("target_part", out tmp);
tgt_part = (tmp as string);
if (targetType == TargetTypes.Hardpoint && !string.IsNullOrEmpty(tgt_part))
{
if (targetType == TargetTypes.Hardpoint)
{
var targetHp = ev.Properties["target_part"].ToString();
if (!objB.Object.HardpointExists(targetHp))
{
FLLog.Error("Thn", "object " + objB.Name + " does not have hardpoint " + targetHp);
return;
}
var hp = objB.Object.GetHardpoint(targetHp);
objA.Object.Attachment = hp;
objA.Object.Parent = objB.Object;
objA.Object.Transform = Matrix4.CreateTranslation(offset);
}
else if (targetType == TargetTypes.Root)
{
objA.Object.Transform = Matrix4.CreateTranslation(offset);
objA.Object.Parent = objB.Object;
}

part = new GameObject();
part.Parent = objB.Object;
part.Attachment = objB.Object.GetHardpoint(ev.Properties["target_part"].ToString());
}
//Attach GameObjects and Cameras to eachother
if (objA.Object != null && objB.Camera != null)
if (targetType == TargetTypes.Part && !string.IsNullOrEmpty(tgt_part))
{

var hp = new Hardpoint(null, objB.Object.CmpConstructs.Find(ev.Properties["target_part"].ToString())); //Create a dummy hardpoint to attach to
part = new GameObject();
part.Parent = objB.Object;
part.Attachment = hp;
}
if (objA.Camera != null && objB.Object != null)
cs.Coroutines.Add(new AttachRoutine()
{
if ((flags & AttachFlags.LookAt) == AttachFlags.LookAt)
{
objA.Camera.LookAt = objB.Object;
}
GameObject part = null;
if (targetType == TargetTypes.Hardpoint)
{
part = new GameObject();
part.Parent = objB.Object;
part.Attachment = objB.Object.GetHardpoint(ev.Properties["target_part"].ToString());
}
if (targetType == TargetTypes.Part)
{
var hp = new Hardpoint(null, objB.Object.CmpConstructs.Find(ev.Properties["target_part"].ToString())); //Create a dummy hardpoint to attach to
part = new GameObject();
part.Parent = objB.Object;
part.Attachment = hp;
}
cs.Coroutines.Add(new AttachCameraToObject()
{
Duration = ev.Duration,
Camera = objA.Camera,
Object = objB.Object,
Part = part,
Position = ((flags & AttachFlags.Position) == AttachFlags.Position),
Orientation = ((flags & AttachFlags.Orientation) == AttachFlags.Orientation),
LookAt = ((flags & AttachFlags.LookAt) == AttachFlags.LookAt)
});
}
Duration = ev.Duration,
Child = objA,
Parent = objB,
Part = part,
Position = ((flags & AttachFlags.Position) == AttachFlags.Position),
Orientation = ((flags & AttachFlags.Orientation) == AttachFlags.Orientation),
LookAt = ((flags & AttachFlags.LookAt) == AttachFlags.LookAt)
});
}
}
}
63 changes: 14 additions & 49 deletions src/LibreLancer/Thn/Events/StartPathAnimationRunner.cs
Expand Up @@ -90,42 +90,23 @@ void Process(float t)

class ObjectPathAnimation : PathAnimationBase
{
public GameObject Object;
public ThnObject Object;

protected override void SetPosition(Vector3 pos)
{
var rot = Object.Transform.ExtractRotation();
Object.Transform = Matrix4.CreateFromQuaternion(rot) * Matrix4.CreateTranslation(pos);
Object.Translate = pos;
}
protected override void SetPositionOrientation(Vector3 pos, Matrix4 orient)
{
Object.Transform = orient * Matrix4.CreateTranslation(pos);
Object.Translate = pos;
Object.Rotate = orient;
}
protected override void SetOrientation(Matrix4 orient)
{
var translation = Object.Transform.ExtractTranslation();
Object.Transform = orient * Matrix4.CreateTranslation(translation);
Object.Rotate = orient;
}
}

class CameraPathAnimation : PathAnimationBase
{
public ThnCameraTransform Camera;

protected override void SetPosition(Vector3 pos)
{
Camera.Position = pos;
}
protected override void SetPositionOrientation(Vector3 pos, Matrix4 orient)
{
Camera.Position = pos;
Camera.Orientation = orient;
}
protected override void SetOrientation(Matrix4 orient)
{
Camera.Orientation = orient;
}
}

public void Process(ThnEvent ev, Cutscene cs)
{
Expand All @@ -134,32 +115,16 @@ public void Process(ThnEvent ev, Cutscene cs)
var start = (float)ev.Properties["start_percent"];
var stop = (float)ev.Properties["stop_percent"];
var flags = ThnEnum.Check<AttachFlags>(ev.Properties["flags"]);
if (obj.Object != null)
{
cs.Coroutines.Add(new ObjectPathAnimation()
{
Duration = ev.Duration,
StartPercent = start,
StopPercent = stop,
Flags = flags,
Curve = ev.ParamCurve,
Path = path,
Object = obj.Object
});
}
if (obj.Camera != null)
cs.Coroutines.Add(new ObjectPathAnimation()
{
cs.Coroutines.Add(new CameraPathAnimation()
{
Duration = ev.Duration,
StartPercent = start,
StopPercent = stop,
Flags = flags,
Curve = ev.ParamCurve,
Path = path,
Camera = obj.Camera
});
}
Duration = ev.Duration,
StartPercent = start,
StopPercent = stop,
Flags = flags,
Curve = ev.ParamCurve,
Path = path,
Object = obj
});
}
}
}

0 comments on commit 8a98e47

Please sign in to comment.