Example #1
0
 public override void HandleCollisions(CollisionBody collider, CollisionType colliderType)
 {
     if (collider is Projectile)
     {
         Projectile collidingProjectile = collider as Projectile;
         health -= 1;
         Explosion.NewExplosion(collidingProjectile.position, Vector2.Zero);
         collidingProjectile.DestroyInstance(collidingProjectile);
     }
 }
Example #2
0
    Vector2 GetExtrudedIndex(CollisionBody body, int index)
    {
        if (index < 0 || index >= body.vertices.Length)
        {
            return(Vector2.zero);
        }
        var pos   = body.vertices[index];
        var posP1 = index == body.vertices.Length - 1 ? body.vertices[0] : body.vertices[index + 1];
        var posM1 = index == 0 ? body.vertices[body.vertices.Length - 1] : body.vertices[index - 1];

        var dir = posP1 + posM1 - 2 * pos;

        return(pos - dir.normalized * m_outDistance);
    }
Example #3
0
        /// <summary>
        /// Returns the node with the smallest distance to goal.
        /// </summary>
        private SearchNode FindBestNode(CollisionBody collisionBody)
        {
            SearchNode currentTile = openList[0];

            float smallestDistanceToGoal = float.MaxValue;

            // Find the closest node to the goal.
            for (int i = 0; i < openList.Count; i++)
            {
                if (openList[i].DistanceToGoal < smallestDistanceToGoal && !collisionBody.Collides(openList[i].CollisionArea))
                {
                    currentTile            = openList[i];
                    smallestDistanceToGoal = currentTile.DistanceToGoal;
                }
            }
            return(currentTile);
        }
Example #4
0
        protected override void OnComponentDetached(EntityComponent component)
        {
            var body = component as CollisionBody;

            if (body != null)
            {
                if (_bodies.Contains(body))
                {
                    _bodies.Remove(body);
                }
                else
                {
                    _playerBody = null;
                }
            }

            base.OnComponentDetached(component);
        }
Example #5
0
        public Player(PlayerModel descriptor, PlayerConnection connection)
        {
            _descriptor        = descriptor;
            _connection        = connection;
            _connection.Player = this;
            this.State         = ActorStates.Idle;

            this.Descriptor.CollisionBounds = new Rect(16, 52, 16, 20);

            this.CollisionBody = new CollisionBody(this);

            _inventory        = new Inventory(this);
            _equipment        = new Equipment(this);
            _networkComponent = new PlayerNetworkComponent(this, connection);
            _packetHandler    = new PlayerPacketHandler(this);
            _actionProcessor  = new ActionProcessor <Player>(this);

            _eventHandlers = new Dictionary <string, List <Action <EventArgs> > >();

            Script script = Engine.Services.Get <ScriptManager>().CreateScript(Constants.FILEPATH_SCRIPTS + "player.py");

            _script = script;

            try
            {
                this.Behavior = script.GetVariable <ActorBehaviorDefinition>("BehaviorDefinition");
            }
            catch { }

            if (this.Behavior == null)
            {
                Engine.Services.Get <Logger>().LogEvent("Error hooking player behavior definition.", LogTypes.ERROR, new Exception("Error hooking player behavior definition."));
            }
            else
            {
                this.Behavior.OnCreated(this);
                this.Behavior.EventOccured += this.BehaviorDescriptor_EventOccured;
            }

            this.Descriptor.Stats.Changed += (o, args) =>
            {
                this.NetworkComponent.SendPlayerStats();
            };
        }
Example #6
0
        protected override void OnComponentAttached(EntityComponent component)
        {
            var body = component as CollisionBody;

            if (body != null)
            {
                if (body.Entity.Name == "Player")
                {
                    _playerBody = body;
                }
                else
                {
                    _bodies.Add(body);
                    body.BoundingBox = new BoundingBox(new Vector3(body.Entity.Position, 0), new Vector3(body.Entity.Position.X + body.Size.Width, body.Entity.Position.Y + body.Size.Height, 0));
                }
            }

            base.OnComponentAttached(component);
        }
Example #7
0
 public override void HandleCollisions(CollisionBody collider, CollisionType colliderType)
 {
     if (collider is Projectile)
     {
         Projectile collidingProjectile = collider as Projectile;
         health -= 1;
         if (deathTimer > 0)
         {
             deathTimer = 80;
         }
         Explosion.NewExplosion(collidingProjectile.position, Vector2.Zero);
         collidingProjectile.DestroyInstance(collidingProjectile);
     }
     if (collider is Asteroid)
     {
         Asteroid collidingAsteroid = collider as Asteroid;
         collidingAsteroid.health -= 2;
         Explosion.NewExplosion(collidingAsteroid.position, Vector2.Zero);
         health = 0;
     }
 }
 public override void HandleCollisions(CollisionBody collider, CollisionType colliderType)
 {
     if (collider is Projectile)
     {
         Projectile collidingProjectile = collider as Projectile;
         health -= 1;
         if (health <= 0)
         {
             DropPowerUp(1, new Vector2(Width / 2f, Height / 2f));
             SpawnGore(Main.random.Next(8, 10 + 1), Width, Height, Main.random.Next(1, 2 + 1));
             GenerateSmoke(Color.Orange, Color.Gray, Width, Height, 16);
             if (beam != null)
             {
                 beam.DestroyInstance(beam);
                 beam = null;
             }
             DestroyInstance(this);
         }
         Explosion.NewExplosion(collidingProjectile.position, Vector2.Zero);
         collidingProjectile.DestroyInstance(collidingProjectile);
     }
     if (collider is Asteroid)
     {
         Asteroid collidingAsteroid = collider as Asteroid;
         collidingAsteroid.health -= 2;
         Explosion.NewExplosion(collidingAsteroid.position, Vector2.Zero);
         DropPowerUp(1, new Vector2(Width / 2f, Height / 2f));
         SpawnGore(Main.random.Next(8, 10 + 1), Width, Height, Main.random.Next(1, 2 + 1));
         GenerateSmoke(Color.Orange, Color.Gray, Width, Height, 16);
         if (beam != null)
         {
             beam.DestroyInstance(beam);
             beam = null;
         }
         DestroyInstance(this);
     }
 }
Example #9
0
    void GetFarestVertex(CollisionBody body, Vector2 startPos, Vector2 endPos, out int leftIndex, out int rightIndex, out bool farestLeft)
    {
        leftIndex  = -1;
        rightIndex = -1;

        var dir = endPos - startPos;

        float leftAngle  = 0;
        float rightAngle = 0;

        float epsilonSqrMax = m_outDistance * m_outDistance * 2;

        for (int i = 0; i < body.vertices.Length; i++)
        {
            var dPoint = body.vertices[i] - startPos;
            if (dPoint.sqrMagnitude < epsilonSqrMax)
            {
                continue;
            }

            var angle = Vector2.SignedAngle(dir, dPoint);

            if (angle > leftAngle)
            {
                leftIndex = i;
                leftAngle = angle;
            }
            if (angle < rightAngle)
            {
                rightIndex = i;
                rightAngle = angle;
            }
        }

        farestLeft = Mathf.Abs(leftAngle) > Mathf.Abs(rightAngle);
    }
Example #10
0
        /// <summary>
        /// Finds the optimal path from one point to another.
        /// </summary>
        public List <Vector> FindPath(Vector startPoint, Vector endPoint, CollisionBody collisionBody)
        {
            Vector normStartPoint = new Vector((int)(startPoint.X / Settings.TileSize), (int)(startPoint.Y / Settings.TileSize));
            Vector normEndPoint   = new Vector((int)(endPoint.X / Settings.TileSize), (int)(endPoint.Y / Settings.TileSize));

            if (normStartPoint.X < _mapBounds.X || normStartPoint.Y < _mapBounds.Y ||
                normEndPoint.X < _mapBounds.X || normEndPoint.Y < _mapBounds.Y)
            {
                return(new List <Vector>());
            }

            if (normStartPoint.X >= _mapBounds.Width || normStartPoint.Y >= _mapBounds.Height ||
                normEndPoint.X >= _mapBounds.Width || normEndPoint.Y >= _mapBounds.Height)
            {
                return(new List <Vector>());
            }

            // Only try to find a path if the start and end points are different.
            if (normStartPoint.X == normEndPoint.X && normStartPoint.Y == normEndPoint.Y)
            {
                return(new List <Vector>());
            }

            /////////////////////////////////////////////////////////////////////
            // Step 1 : Clear the Open and Closed Lists and reset each node’s F
            //          and G values in case they are still set from the last
            //          time we tried to find a path.
            /////////////////////////////////////////////////////////////////////
            ResetSearchNodes();

            // Store references to the start and end nodes for convenience.
            SearchNode startNode = _searchNodes[(int)normStartPoint.X, (int)normStartPoint.Y];
            SearchNode endNode   = _searchNodes[(int)normEndPoint.X, (int)normEndPoint.Y];

            /////////////////////////////////////////////////////////////////////
            // Step 2 : Set the start node’s G value to 0 and its F value to the
            //          estimated distance between the start node and goal node
            //          (this is where our H function comes in) and add it to the
            //          Open List.
            /////////////////////////////////////////////////////////////////////
            startNode.InOpenList = true;

            startNode.DistanceToGoal   = Heuristic(normStartPoint, normEndPoint);
            startNode.DistanceTraveled = 0;

            openList.Add(startNode);

            /////////////////////////////////////////////////////////////////////
            // Setp 3 : While there are still nodes to look at in the Open list :
            /////////////////////////////////////////////////////////////////////
            while (openList.Count > 0)
            {
                /////////////////////////////////////////////////////////////////
                // a) : Loop through the Open List and find the node that
                //      has the smallest F value.
                /////////////////////////////////////////////////////////////////
                SearchNode currentNode = FindBestNode(collisionBody);

                /////////////////////////////////////////////////////////////////
                // b) : If the Open List empty or no node can be found,
                //      no path can be found so the algorithm terminates.
                /////////////////////////////////////////////////////////////////
                if (currentNode == null)
                {
                    break;
                }

                /////////////////////////////////////////////////////////////////
                // c) : If the Active Node is the goal node, we will
                //      find and return the final path.
                /////////////////////////////////////////////////////////////////

                if (currentNode.Position.X == endNode.Position.X && currentNode.Position.Y == endNode.Position.Y)
                {
                    // Trace our path back to the start.
                    return(FindFinalPath(startNode, endNode));
                }

                /////////////////////////////////////////////////////////////////
                // d) : Else, for each of the Active Node’s neighbours :
                /////////////////////////////////////////////////////////////////
                for (int i = 0; i < currentNode.GetNeighbors().Length; i++)
                {
                    SearchNode neighbor = currentNode.GetNeighbors()[i];

                    //////////////////////////////////////////////////
                    // i) : Make sure that the neighbouring node can
                    //      be walked across.
                    //////////////////////////////////////////////////
                    if (neighbor == null || neighbor.Walkable == false)
                    {
                        continue;
                    }

                    //////////////////////////////////////////////////
                    // ii) Calculate a new G value for the neighbouring node.
                    //////////////////////////////////////////////////
                    float distanceTraveled = currentNode.DistanceTraveled + 1;

                    // An estimate of the distance from this node to the end node.
                    float heuristic = Heuristic(neighbor.Position, normEndPoint);

                    //////////////////////////////////////////////////
                    // iii) If the neighbouring node is not in either the Open
                    //      List or the Closed List :
                    //////////////////////////////////////////////////
                    if (neighbor.InOpenList == false && neighbor.InClosedList == false)
                    {
                        // (1) Set the neighbouring node’s G value to the G value
                        //     we just calculated.
                        neighbor.DistanceTraveled = distanceTraveled;
                        // (2) Set the neighbouring node’s F value to the new G value +
                        //     the estimated distance between the neighbouring node and
                        //     goal node.
                        neighbor.DistanceToGoal = distanceTraveled + heuristic;
                        // (3) Set the neighbouring node’s Parent property to point at the Active
                        //     Node.
                        neighbor.Parent = currentNode;
                        // (4) Add the neighbouring node to the Open List.
                        neighbor.InOpenList = true;
                        openList.Add(neighbor);
                    }
                    //////////////////////////////////////////////////
                    // iv) Else if the neighbouring node is in either the Open
                    //     List or the Closed List :
                    //////////////////////////////////////////////////
                    else if (neighbor.InOpenList || neighbor.InClosedList)
                    {
                        // (1) If our new G value is less than the neighbouring
                        //     node’s G value, we basically do exactly the same
                        //     steps as if the nodes are not in the Open and
                        //     Closed Lists except we do not need to add this node
                        //     the Open List again.
                        if (neighbor.DistanceTraveled > distanceTraveled)
                        {
                            neighbor.DistanceTraveled = distanceTraveled;
                            neighbor.DistanceToGoal   = distanceTraveled + heuristic;

                            neighbor.Parent = currentNode;
                        }
                    }
                }

                /////////////////////////////////////////////////////////////////
                // e) Remove the Active Node from the Open List and add it to the
                //    Closed List
                /////////////////////////////////////////////////////////////////
                openList.Remove(currentNode);
                currentNode.InClosedList = true;
            }

            // No path could be found.
            return(new List <Vector>());
        }
Example #11
0
 public void Connect(string strName, out CollisionBody item)
 {
     ExAddProperty(this.m_pSelf, strName, 4, 0x2ee0);
     item = null;
 }
Example #12
0
 public void Connect(string strName, out CollisionBody item)
 {
     item = RuntimeObject.FromPtr(ExGetProperty(this.m_pSelf, strName, 4, 0x2ee0)) as CollisionBody;
 }
Example #13
0
 public abstract void removeBody(CollisionBody body);
Example #14
0
 public abstract void addBody(CollisionBody body);
Example #15
0
 public AbstractCollisionComponent(IEntity owner, CollisionBody body)
     : base(owner)
 {
     this.bodyList = new List<CollisionBody>();
     this.bodyList.Add(body);
 }