示例#1
0
        /// <summary>
        ///		Updates the following entity associated with this process.
        /// </summary>
        /// <param name="deltaTime">Elapsed time since last frame.</param>
        public override void Run(float deltaTime)
        {
            //if (_entity.IsEnabled == false) return;

            Transformation entityTransform = _entity.CalculateTransformation();

            if (_entity is CameraNode)
            {
                entityTransform.X = -entityTransform.X;
                entityTransform.Y = -entityTransform.Y;
            }
            entityTransform.X = (int)entityTransform.X;
            entityTransform.Y = (int)entityTransform.Y;

            // Are we using a rectangle or entity as the boundry?
            Rectangle boundry = new Rectangle((int)_boundry.X, (int)_boundry.Y, (int)_boundry.Width, (int)_boundry.Height);

            if (_entityBoundry != null)
            {
                Transformation boundryTransform = _entityBoundry.CalculateTransformation();
                if (_entityBoundry is CameraNode)
                {
                    boundryTransform.X = -boundryTransform.X;
                    boundryTransform.Y = -boundryTransform.Y;
                }
                boundryTransform.X = (int)boundryTransform.X;
                boundryTransform.Y = (int)boundryTransform.Y;
                boundry            = new Rectangle((int)boundryTransform.X, (int)boundryTransform.Y, (int)(_entityBoundry.Width * boundryTransform.ScaleX), (int)(_entityBoundry.Height * boundryTransform.ScaleY));
            }

            float x = entityTransform.X; // Cast to int to fix jittering bugs.
            float y = entityTransform.Y; // Cast to int to fix jittering bugs.

            if (x < boundry.Left)
            {
                x = boundry.Left;
            }
            if (y < boundry.Top)
            {
                y = boundry.Top;
            }
            if (x > boundry.Right - (_entity.BoundingRectangle.Width * entityTransform.ScaleX))
            {
                x = (int)(boundry.Right - (_entity.BoundingRectangle.Width * entityTransform.ScaleX));
            }
            if (y > boundry.Bottom - (_entity.BoundingRectangle.Height * entityTransform.ScaleY))
            {
                y = (int)(boundry.Bottom - (_entity.BoundingRectangle.Height * entityTransform.ScaleY));
            }

            if (!float.IsNaN(x) && !float.IsNaN(y))
            {
                _entity.Position(x, y, _entity.Transformation.Z);
            }
        }
示例#2
0
        /// <summary>
        ///		Updates the following entity associated with this process.
        /// </summary>
        /// <param name="deltaTime">Elapsed time since last frame.</param>
        public override void Run(float deltaTime)
        {
            //if (_entity.IsEnabled == false) return;

            CameraNode camera = _entity as CameraNode;

            // Work out the central points.
            Transformation entityTransform = _entity.CalculateTransformation();
            Transformation targetTransform = _targetEntity.CalculateTransformation();

            // If its a camera then we need to invert the coordinates as it uses
            // slightly different ones from normal entities.
            if (camera != null)
            {
                entityTransform.X = -entityTransform.X;
                entityTransform.Y = -entityTransform.Y;
            }

            float entityTransformCenterX       = entityTransform.X + ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX);
            float entityTransformCenterY       = entityTransform.Y + ((_entity.BoundingRectangle.Height / 2) * entityTransform.ScaleY);
            float targetEntityTransformCenterX = targetTransform.X + ((_targetEntity.BoundingRectangle.Width / 2) * targetTransform.ScaleX);
            float targetEntityTransformCenterY = targetTransform.Y + ((_targetEntity.BoundingRectangle.Height / 2) * targetTransform.ScaleY);
            float vectorX  = entityTransformCenterX - targetEntityTransformCenterX;
            float vectorY  = entityTransformCenterY - targetEntityTransformCenterY;
            float distance = (float)Math.Sqrt(vectorX * vectorX + vectorY * vectorY);

            vectorX /= distance;
            vectorY /= distance;

            // Are we set to instantly snap to the target?
            if (_bufferZoneRadius == 0.0f || _followSpeed == 0.0f)
            {
                _entity.Position((float)Math.Floor(targetEntityTransformCenterX - ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX)), (float)Math.Floor(targetEntityTransformCenterY - ((_entity.BoundingRectangle.Width / 2) * entityTransform.ScaleX)), targetTransform.Z);
                return;
            }

            // Are we at the correct place? If so why bother with the below code :P.
            if (entityTransformCenterX == targetEntityTransformCenterX && entityTransformCenterY == targetEntityTransformCenterY)
            {
                return;
            }

            // Are we in the stationary buffer zone?
            if (distance <= _bufferZoneRadius)
            {
                return; // Yep.
            }
            distance = (int)distance;

            // Work out vector towards entity.
            float scaleAmount = (distance / _bufferZoneRadius);
            //if (distance > _bufferZoneRadius * 2) scaleAmount *= 2;

            float movementX = (float)Math.Round(vectorX * (_followSpeed * scaleAmount), 1); // *deltaTime;
            float movementY = (float)Math.Round(vectorY * (_followSpeed * scaleAmount), 1); // *deltaTime;

            movementX = (int)movementX;
            movementY = (int)movementY;

            if (!float.IsNaN(movementX) && !float.IsNaN(movementY))
            {
                _entity.Move(-movementX, -movementY, 0.0f);
            }
        }
        /// <summary>
        ///     Called when the property list view wishs to set the value of an item.
        /// </summary>
        /// <param name="item">Item to set value of.</param>
        /// <param name="value">Value to set item as.</param>
        private void SetValue(PropertyListViewItem item, object value)
        {
            // If the value is a file editor we need to grab its file URL.
            if (value is FileEditorValue)
            {
                value = ((FileEditorValue)value).FileUrl;
            }

            // If its an enumeration get its enumeration value instead.
            if (item.EnumerationValues != null)
            {
                value = item.EnumerationValue;
            }

            // Go through non-dynamic items.
            switch (item.Name.ToLower())
            {
            // General
            case "name":
                _entity.Name = value as string;
                Engine.Engine.GlobalInstance.Map.SceneGraph.SyncronizeNodes();
                return;

            case "event":
                _entity.Event      = value as string;
                _entity.EventNodes = Engine.Engine.GlobalInstance.Map.SceneGraph.GetNodesByName(_entity.Event);
                return;

            case "enabled":     _entity.IsEnabled = (bool)value; return;

            // Rendering
            case "visible":     _entity.IsVisible = (bool)value; return;

            case "color":
            {
                Color color = (Color)value;
                int   a, r, g, b;
                ColorMethods.SplitColor(ColorFormats.A8R8G8B8, _entity.Color, out r, out g, out b, out a);
                _entity.Color = ColorMethods.CombineColor(ColorFormats.A8R8G8B8, color.R, color.G, color.B, a);
                return;
            }

            case "alpha":
            {
                int a, r, g, b;
                ColorMethods.SplitColor(ColorFormats.A8R8G8B8, _entity.Color, out r, out g, out b, out a);
                _entity.Color = ColorMethods.CombineColor(ColorFormats.A8R8G8B8, r, g, b, (byte)value);
                return;
            }

            case "blend mode":  _entity.BlendMode = (BlendMode)value; return;

            case "render mode": _entity.RenderMode = (EntityRenderMode)value; return;

            case "image":
            {
                _entity.Image = value as Graphics.Image;

                if (_entity.Image != null)
                {
                    _entity.BoundingRectangle  = new Rectangle(0, 0, _entity.Image.Width, _entity.Image.Height);
                    _entity.CollisionRectangle = new Rectangle(0, 0, _entity.Image.Width, _entity.Image.Height);
                }

                return;
            }

            case "mesh": _entity.Mesh = (string)value == "" ? null : new Mesh(Engine.Engine.GlobalInstance.MediaPath + "\\" + value, 0); return;

            case "frame":      _entity.Frame = (int)value; return;

            // Collision
            case "solid":               _entity.IsSolid = (bool)value; return;

            case "collision rectangle": _entity.CollisionRectangle = (Rectangle)value; return;

            case "collision layers":    _entity.CollisionPolygon.Layers = value as int[]; return;

            // Text
            case "text":                _entity.Text = (string)value; return;

            case "bitmap font":         _entity.Font = value as BitmapFont; return;

            case "shader":              _entity.Shader = (string)value == "" ? null : new Shader(Engine.Engine.GlobalInstance.ShaderPath + "\\" + value); return;

            // Transformation
            case "location":            _entity.Position(((Point)value).X, ((Point)value).Y, _entity.Transformation.Z); return;

            case "scale":               _entity.Scale(((Vector)value).X, ((Vector)value).Y, ((Vector)value).Z); return;

            case "angle":               _entity.Rotate(((Vector)value).X, ((Vector)value).Y, ((Vector)value).Z); return;

            case "z-offset":            _entity.Position(_entity.Transformation.X, _entity.Transformation.Y, (float)value); return;

            case "z-layer":             _entity.DepthLayer = (int)value; return;

            case "depth mode":          _entity.DepthMode = (EntityDepthMode)value; return;

            case "bounding rectangle":  _entity.BoundingRectangle = (Rectangle)value; return;
            }

            // Go through dynamic items.
            if (_entity is ScriptedEntityNode)
            {
                ScriptedEntityNode scriptedEntity = (ScriptedEntityNode)_entity;
                foreach (Symbol symbol in scriptedEntity.ScriptProcess.GlobalScope.Symbols)
                {
                    if (!(symbol is VariableSymbol))
                    {
                        continue;
                    }

                    string name       = ((VariableSymbol)symbol).Identifier;
                    string editMethod = "";
                    foreach (Symbol subSymbol in symbol.Symbols)
                    {
                        if (subSymbol is MetaDataSymbol)
                        {
                            if (((MetaDataSymbol)subSymbol).Identifier.ToLower() == "name")
                            {
                                name = ((MetaDataSymbol)subSymbol).Value;
                            }
                            else if (((MetaDataSymbol)subSymbol).Identifier.ToLower() == "editmethod")
                            {
                                editMethod = ((MetaDataSymbol)subSymbol).Value;
                            }
                        }
                    }


                    if (((VariableSymbol)symbol).IsProperty == true && name.ToLower() == item.Name.ToLower())
                    {
                        VariableSymbol variableSymbol = ((VariableSymbol)symbol);
                        if (variableSymbol.DataType.IsArray == false)
                        {
                            switch (variableSymbol.DataType.DataType)
                            {
                            case DataType.Bool: scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, (bool)value); break;

                            case DataType.Byte: scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, (byte)value); break;

                            case DataType.Double: scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, (double)value); break;

                            case DataType.Float: scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, (float)value); break;

                            case DataType.Int: scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, (int)value); break;

                            case DataType.Long: scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, (long)value); break;

                            case DataType.Short: scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, (short)value); break;

                            case DataType.String: scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, (string)value); break;

                            case DataType.Object:
                                if (editMethod.ToLower() == "image")
                                {
                                    scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, value == null ? (RuntimeObject)null : new Engine.ScriptingFunctions.ImageScriptObject(value as Graphics.Image)); break;
                                }
                                else if (editMethod.ToLower() == "sound")
                                {
                                    scriptedEntity.ScriptProcess[0].SetGlobalVariable(variableSymbol.Identifier, value == null ? (RuntimeObject)null : new Engine.ScriptingFunctions.SoundScriptObject(value as Audio.Sound)); break;
                                }
                                break;

                            default: continue;
                            }
                        }
                        else
                        {
                            object[] array      = (object[])value;
                            int      arrayIndex = scriptedEntity.ScriptProcess[0].AllocateArray(variableSymbol.DataType.DataType, array.Length);
                            for (int i = 0; i < array.Length; i++)
                            {
                                switch (variableSymbol.DataType.DataType)
                                {
                                case DataType.Bool: scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, (bool)array[i]); break;

                                case DataType.Byte: scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, (byte)array[i]); break;

                                case DataType.Double: scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, (double)array[i]); break;

                                case DataType.Float: scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, (float)array[i]); break;

                                case DataType.Int: scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, (int)array[i]); break;

                                case DataType.Long: scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, (long)array[i]); break;

                                case DataType.Short: scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, (short)array[i]); break;

                                case DataType.String: scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, (string)array[i]); break;

                                // Needs fixing - What if we have other items apart from images stored in this variable?
                                case DataType.Object:
                                    if (editMethod.ToLower() == "image")
                                    {
                                        scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, array[i] == null ? (RuntimeObject)null : new Engine.ScriptingFunctions.ImageScriptObject(array[i] as Graphics.Image)); break;
                                    }
                                    else if (editMethod.ToLower() == "image")
                                    {
                                        scriptedEntity.ScriptProcess[0].SetArrayElement(arrayIndex, i, array[i] == null ? (RuntimeObject)null : new Engine.ScriptingFunctions.SoundScriptObject(array[i] as Audio.Sound)); break;
                                    }
                                    break;

                                default: continue;
                                }
                            }
                        }
                        scriptedEntity.OnPropertyChange(symbol.Identifier);
                    }
                }
            }

            else if (_entity is EmitterNode)
            {
                // Unneccessary, the emitter editor deals with this.
            }

            else if (_entity is TilemapSegmentNode)
            {
                TilemapSegmentNode segmentNode = _entity as TilemapSegmentNode;

                switch (item.Name.ToLower())
                {
                case "grid visible": segmentNode.IsGridVisible = (bool)value; break;

                case "tile size":    segmentNode.TileWidth = ((Size)value).Width; segmentNode.TileHeight = ((Size)value).Height; break;
                }
            }

            else if (_entity is PathMarkerNode)
            {
                PathMarkerNode markerNode = _entity as PathMarkerNode;

                switch (item.Name.ToLower())
                {
                case "speed": markerNode.Speed = (StartFinishF)value; break;

                case "delay": markerNode.Delay = (int)value; break;

                case "next marker name":
                    markerNode.NextNodeName = (string)value;
                    ArrayList nodes = Engine.Engine.GlobalInstance.Map.SceneGraph.GetNodesByName(markerNode.NextNodeName);
                    markerNode.NextNode = (nodes.Count != 0) ? nodes[0] as PathMarkerNode : null;
                    break;
                }
            }

            // Call property changed delegate.
            _propertyChangedDelegate(this, item.Name, value);
        }