Exemple #1
0
 public void DestroyDetail(DetailObject detail)
 {
     this.sender.Send(NetworkPhrase.DestroyDetail, (s) =>
     {
         s.Write(detail.ID);
     });
 }
Exemple #2
0
        public void CreateDetail(DetailObject detail)
        {
            this.sender.Send(NetworkPhrase.CreateDetail, (s) =>
            {
                bool hasShape = detail.Shape != null;

                s.Write(detail.ID);
                if (detail.Parent != null)
                {
                    s.Write(detail.Parent.ID);
                }
                else
                {
                    s.Write(-1);
                }
                s.Write(detail.Model ?? "");
                s.Write(detail.Position);
                s.Write(detail.Rotation);
                s.Write(hasShape);
                if (hasShape == true)
                {
                    var shape = (BoxShape)detail.Shape;
                    s.Write(shape.Size.TK());
                }
            });
        }
Exemple #3
0
        private void CreateDetail(BinaryReader reader)
        {
            int id       = reader.ReadInt32();
            int parentID = reader.ReadInt32();

            string model = reader.ReadString();
            var    pos   = reader.ReadVector3();
            var    rot   = reader.ReadQuaternion();

            Shape shape    = null;
            bool  hasShape = reader.ReadBoolean();

            if (hasShape == true)
            {
                var shapeSize = reader.ReadVector3();
                shape = DetailHelper.CreateShape(shapeSize);
            }

            var parent = this.world.GetDetail(parentID);

            DetailObject obj = new DetailObject(parent, id, shape);

            obj.Position = pos;
            obj.Rotation = rot;
            obj.Model    = model.Length > 0 ? model : null;

            this.world.RegisterDetail(obj);
        }
Exemple #4
0
        public void RegisterDetail(DetailObject obj)
        {
            if (this.HasDetail(obj.ID))
            {
                throw new InvalidOperationException();
            }

            obj.Changed += Obj_Changed;
            obj.InteractionTriggered += Obj_InterationTriggered;
            this.details.Add(obj);

            if (obj.Shape != null)
            {
                var body = new RigidBody(obj.Shape);
                body.Position        = obj.WorldPosition.Jitter();
                body.Orientation     = JMatrix.CreateFromQuaternion(obj.WorldRotation.Jitter());
                body.IsStatic        = true;
                body.Tag             = obj;
                body.EnableDebugDraw = true;
                body.Material        = this.blockMaterial;
                this.detailBodies.Add(obj.ID, body);

                this.AddBody(body);

                obj.PositionChanged += (s, e) =>
                {
                    body.Position    = obj.WorldPosition.Jitter();
                    body.Orientation = JMatrix.CreateFromQuaternion(obj.WorldRotation.Jitter());
                };
            }

            this.OnDetailCreated(obj);
        }
 public DetailObject(DetailObject parent, int id, Shape shape)
 {
     this.parent = parent;
     this.shape  = shape;
     this.ID     = id;
     this.interactions.CollectionChanged += (s, e) =>
     {
         if (e.NewItems != null)
         {
             foreach (Interaction item in e.NewItems)
             {
                 item.Triggered += Item_Triggered;
             }
         }
         if (e.OldItems != null)
         {
             foreach (Interaction item in e.OldItems)
             {
                 item.Triggered -= Item_Triggered;
             }
         }
         this.OnInteractonsChanged();
     };
     if (this.parent != null)
     {
         this.parent.PositionChanged += (s, e) => this.OnPositionChanged();
     }
 }
Exemple #6
0
 private void OnDetailChanged(DetailObject obj)
 {
     if (this.DetailChanged != null)
     {
         this.DetailChanged(this, new DetailEventArgs(obj));
     }
 }
Exemple #7
0
        private void PaintDetailUI(Graphics g, Size screen, DetailObject detail, Brush background, int selection)
        {
            if (this.player == null)
            {
                return;
            }

            if (detail.Interactions.Count == 0)
            {
                return;
            }

            var screenSpace = this.player.Camera.WorldToScreen(detail.WorldPosition, this.Aspect);

            if ((screenSpace.Z < 0.0f) || (screenSpace.Z > 1.0f))
            {
                return;
            }

            int x = (int)(screen.Width * (0.5f + 0.5f * screenSpace.X));
            int y = (int)(screen.Height * (0.5f - 0.5f * screenSpace.Y));

            var state = g.Transform;

            g.TranslateTransform(x, y);

            var        font         = this.smallFont;
            RectangleF area         = new RectangleF();
            var        interactions = detail.Interactions.ToArray();

            for (int i = 0; i < interactions.Length; i++)
            {
                var size = g.MeasureString(interactions[i].Name, font);

                area.Width   = Math.Max(size.Width, area.Width);
                area.Height += size.Height + 1;
            }

            g.FillRectangle(background, area);
            float pointer = 0.0f;

            for (int i = 0; i < interactions.Length; i++)
            {
                var text = interactions[i].Name;
                var size = g.MeasureString(text, font);

                if (i == selection)
                {
                    var sel = new RectangleF(0, pointer, area.Width, size.Height);
                    g.FillRectangle(Brushes.LightGreen, sel);
                }

                g.DrawString(text, font, Brushes.Black, 0, pointer);
                pointer += size.Height;
            }
            g.DrawRectangle(Pens.Black, area.X, area.Y, area.Width, area.Height);

            g.Transform = state;
        }
Exemple #8
0
 public void TriggerInteraction(DetailObject detail, Interaction interaction)
 {
     this.sender.Send(NetworkPhrase.TriggerInteraction, (s) =>
     {
         s.Write(detail.ID);
         s.Write(interaction.ID);
     });
 }
Exemple #9
0
 public void UpdateDetail(DetailObject detail)
 {
     this.sender.Send(NetworkPhrase.UpdateDetail, (s) =>
     {
         s.Write(detail.ID);
         s.Write(detail.Position);
         s.Write(detail.Rotation);
     });
 }
Exemple #10
0
        public DetailObject CreateDetail(string model, Vector3 position, DetailObject parent = null, Shape shape = null)
        {
            var obj = new DetailObject(parent, ++this.detailCounter, shape)
            {
                Model    = model,
                Position = position,
            };

            this.RegisterDetail(obj);

            return(obj);
        }
Exemple #11
0
        private List <DetailObject> GetChildren(DetailObject obj)
        {
            List <DetailObject> children = new List <DetailObject>();

            foreach (var detail in this.details)
            {
                if (detail.Parent == obj)
                {
                    children.Add(detail);
                }
            }
            return(children);
        }
Exemple #12
0
        private void OnDetailRemoved(DetailObject obj)
        {
            if (this.DetailRemoved != null)
            {
                this.DetailRemoved(this, new DetailEventArgs(obj));
            }

            // Destroy all child objects in the object tree as well
            foreach (var child in GetChildren(obj))
            {
                this.RemoveDetail(child.ID);
            }
        }
Exemple #13
0
 public void SetInteractions(DetailObject detail)
 {
     this.sender.Send(NetworkPhrase.SetInteractions, (s) =>
     {
         s.Write(detail.ID);
         s.Write(detail.Interactions.Count);
         foreach (var interaction in detail.Interactions)
         {
             s.Write(interaction.ID);
             s.Write(interaction.Name);
         }
     });
 }
Exemple #14
0
        private DetailObject CreateDetailInstance(
            Dictionary <DetailTemplate, DetailObject> mapping,
            Dictionary <BehaviourTemplate, Behaviour> behaviours,
            DetailTemplate template,
            DetailObject parent)
        {
            Vector3 pos = Vector3.Zero;

            if (template.Position != null)
            {
                pos = template.GetPosition();
            }
            Shape shape = null;

            if (template.Shape != null)
            {
                shape = DetailHelper.CreateShape(template.Shape.GetSize());
            }

            DetailObject root = this.CreateDetail(template.Model, pos, parent, shape);

            if (template.Rotation != null)
            {
                root.Rotation = template.GetRotation();
            }

            if (template.Children != null)
            {
                foreach (var child in template.Children)
                {
                    CreateDetailInstance(mapping, behaviours, child, root);
                }
            }
            mapping.Add(template, root);

            if (template.Behaviours != null)
            {
                foreach (var behav in template.Behaviours)
                {
                    var type = Type.GetType(behav.Class);
                    if (type.IsSubclassOf(typeof(Behaviour)) == false)
                    {
                        throw new InvalidOperationException("Invalid behaviour type: " + behav.Class);
                    }
                    Behaviour behaviour = root.CreateBehaviour(type, true);
                    behaviours.Add(behav, behaviour);
                }
            }

            return(root);
        }
Exemple #15
0
        private void UpdateDetail(BinaryReader reader)
        {
            int id  = reader.ReadInt32();
            var pos = reader.ReadVector3();
            var rot = reader.ReadQuaternion();

            DetailObject obj = this.world.GetDetail(id);

            if (obj == null)
            {
                return;
            }
            obj.Position = pos;
            obj.Rotation = rot;
        }
Exemple #16
0
        private void RenderDetail(Camera cam, DetailObject detail, double time)
        {
            Matrix4 world = detail.Transform;

            Matrix4 worldViewProjection =
                world *
                cam.CreateViewMatrix() *
                cam.CreateProjectionMatrix(this.Aspect);

            GL.UniformMatrix4(this.objectShader["uWorld"], false, ref world);
            GL.UniformMatrix4(this.objectShader["uWorldViewProjection"], false, ref worldViewProjection);

            var model = this.GetModelFromName(detail.Model);

            if (model != null)
            {
                model.Render(cam, time);
            }
        }
Exemple #17
0
        public override void UpdateFrame(IGameInputDriver input, double time)
        {
            this.fps        = (int)(1.0f / time);
            this.totalTime += time;

            this.network.Dispatch();

            foreach (var proxy in this.proxies)
            {
                proxy.Value.UpdateFrame(input, time);
            }

            if (input.GetButtonDown(Key.Q))
            {
                this.currentTool = (this.currentTool + 1) % this.tools.Count;
            }

            if (this.player != null)
            {
                this.player.Tool = this.tools[this.currentTool].Item2;
                this.player.UpdateFrame(input, time);

                var previous = this.selectedDetail;
                this.selectedDetail = null;
                this.visibleDetails.Clear();

                var from = this.player.Eye;
                foreach (var detail in this.world.Details)
                {
                    // Ignore all details without interactions.
                    // How stupid was I when i forgot this?
                    if (detail.Interactions.Count == 0)
                    {
                        continue;
                    }

                    // Check for visibility with trace
                    var to  = detail.WorldPosition;
                    var dir = (to - from).Normalized();

                    var hit = this.world.Trace(from.Jitter(), dir.Jitter(), TraceOptions.IgnoreDynamic);

                    if ((hit != null) && ((hit.Body?.Tag as DetailObject) != detail))
                    {
                        continue;
                    }

                    var dist = (detail.WorldPosition - from).Length;
                    if (dist > 3.5f)                     // TODO: Implement settings value
                    {
                        continue;
                    }

                    var center = this.player.Camera.WorldToScreen(detail.WorldPosition, this.Aspect);
                    if ((center.X < -0.6) || (center.X > 0.6))
                    {
                        continue;
                    }

                    /*
                     * Maybe opt in this later, but it should decrease usability of doors.
                     * if ((center.Y < -1.0) || (center.Y > 1.0))
                     * continue;
                     */

                    this.visibleDetails.Add(detail);


                    if (this.selectedDetail != null)
                    {
                        var currentCenter = this.player.Camera.WorldToScreen(this.selectedDetail.WorldPosition, this.Aspect).Xy.Length;

                        if (currentCenter < center.Xy.Length)
                        {
                            continue;
                        }
                    }
                    this.selectedDetail = detail;
                }
                this.visibleDetails.Remove(this.selectedDetail);

                // Reset interaction on change
                if (previous != this.selectedDetail)
                {
                    this.selectedDetailInteraction = 0;
                }

                if (this.selectedDetail != null)
                {
                    // Detail interaction selection with moues wheel.
                    int c = this.selectedDetail.Interactions.Count;
                    if (c > 1)
                    {
                        if (input.MouseWheel > 0)
                        {
                            this.selectedDetailInteraction -= 1;
                        }
                        if (input.MouseWheel < 0)
                        {
                            this.selectedDetailInteraction += 1;
                        }

                        while (this.selectedDetailInteraction < 0)
                        {
                            this.selectedDetailInteraction += c;
                        }
                        while (this.selectedDetailInteraction >= c)
                        {
                            this.selectedDetailInteraction -= c;
                        }
                    }

                    // Detail interaction with Key.E
                    if (input.GetButtonDown(Key.E))
                    {
                        if (this.selectedDetail.Interactions.Count > 0)
                        {
                            this.Server.TriggerInteraction(
                                this.selectedDetail,
                                this.selectedDetail.Interactions[this.selectedDetailInteraction]);
                        }
                    }
                }
            }
            lock (typeof(World))
            {
                this.world.Step((float)time, true);
            }

            this.networkUpdateCounter++;
            if (this.networkUpdateCounter > 5)
            {
                this.sender.SetPlayer(this.player.FeetPosition, this.player.BodyRotation);
                this.networkUpdateCounter = 0;
            }
        }
 public DetailInteractionEventArgs(DetailObject detail, Interaction interaction, IActor actor) :
     base(actor)
 {
     this.Detail      = detail;
     this.Interaction = interaction;
 }
Exemple #19
0
 private void CreateDetail(DetailObject detail)
 {
     this.client.CreateDetail(detail);
     this.client.SetInteractions(detail);
     detail.InteractionsChanged += Detail_InteractionsChanged;
 }
 public DetailEventArgs(DetailObject obj)
 {
     this.Detail = obj;
 }
Exemple #21
0
        public void Load(Stream fs, bool leaveOpen)
        {
            // Reset whole world
            foreach (var block in blocks.ToArray())
            {
                this[block.X, block.Y, block.Z] = null;
            }

            Action <bool> assert = (b) => { if (!b)
                                            {
                                                throw new InvalidDataException();
                                            }
            };

            using (var br = new BinaryReader(fs, Encoding.UTF8, leaveOpen))
            {
                assert(br.ReadString() == "BLOCKWORLD");
                int major = br.ReadByte();
                int minor = br.ReadByte();
                assert(major == 1);


                int    typeCount = br.ReadInt32();
                Type[] types     = new Type[typeCount];
                for (int i = 0; i < typeCount; i++)
                {
                    string typeName = br.ReadString();
                    types[i] = Type.GetType(typeName);
                    if (types[i] == null)
                    {
                        throw new TypeLoadException("Could not find type " + typeName);
                    }
                }

                long blockCount = br.ReadInt64();
                for (long i = 0; i < blockCount; i++)
                {
                    int  x    = br.ReadInt32();
                    int  y    = br.ReadInt32();
                    int  z    = br.ReadInt32();
                    Type type = types[br.ReadInt32()];

                    var block = Activator.CreateInstance(type) as Block;

                    block.Deserialize(br);

                    this[x, y, z] = block;
                }

                // Details are stored here as well
                if (minor == 1)
                {
                    int detailCount = br.ReadInt32();
                    for (int i = 0; i < detailCount; i++)
                    {
                        int    id       = br.ReadInt32();
                        string model    = br.ReadString();
                        int    parentID = br.ReadInt32();

                        var position = br.ReadVector3();
                        var rotation = br.ReadQuaternion();

                        Shape shape = null;

                        bool hasShape = br.ReadBoolean();
                        if (hasShape)
                        {
                            var size = br.ReadVector3();
                            shape = new BoxShape(size.Jitter());
                        }

                        DetailObject detail = new DetailObject(this.GetDetail(parentID), id, shape);

                        if (string.IsNullOrWhiteSpace(model) == false)
                        {
                            detail.Model = model;
                        }

                        detail.Position = position;
                        detail.Rotation = rotation;

                        this.RegisterDetail(detail);

                        int behaviourCount = br.ReadInt32();
                        for (int j = 0; j < behaviourCount; j++)
                        {
                            var typeName = br.ReadString();
                            var type     = Type.GetType(typeName, true);

                            int  behaviourID = br.ReadInt32();
                            bool isEnabled   = br.ReadBoolean();

                            var behaviour = (Behaviour)Activator.CreateInstance(type);

                            detail.CreateBehaviour(behaviour, behaviourID, isEnabled);
                        }
                    }



                    int signalCount = br.ReadInt32();
                    for (int j = 0; j < signalCount; j++)
                    {
                        var signalDetailID    = br.ReadInt32();
                        var signalBehaviourID = br.ReadInt32();
                        var signalName        = br.ReadString();

                        var signal = this.GetDetail(signalDetailID).Behaviours[signalBehaviourID].Signals[signalName];

                        var slotCount = br.ReadInt32();
                        for (int k = 0; k < slotCount; k++)
                        {
                            var slotDetailID    = br.ReadInt32();
                            var slotBehaviourID = br.ReadInt32();
                            var slotName        = br.ReadString();

                            var slot = this.GetDetail(slotDetailID).Behaviours[slotBehaviourID].Slots[slotName];

                            signal.Connect(slot);
                        }
                    }
                }
            }
        }