Beispiel #1
0
        public bool FireEvent(Event _event, Entity entity)
        {
            if (Game1.player.FireEvent(_event))
            {
                return(true);
            }

            if (_event.id != "RayHit")
            {
                if (EntityWithinBounds(entity.GetCenterPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetCenterPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetTopLeftPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetTopLeftPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetTopPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetTopPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetTopRightPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetTopRightPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetRightPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetRightPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetLeftPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetLeftPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetBottomLeftPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetBottomLeftPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetBottomPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetBottomPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetBottomRightPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.staticCells[entity.GetBottomRightPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }


                // DYNAMIC CELLS
                if (EntityWithinBounds(entity.GetCenterPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetCenterPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetTopLeftPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetTopLeftPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetTopPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetTopPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetTopRightPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetTopRightPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetRightPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetRightPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetLeftPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetLeftPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetBottomLeftPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetBottomLeftPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetBottomPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetBottomPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(entity.GetBottomRightPartition(), CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[entity.GetBottomRightPartition()].FireEvent(_event))
                    {
                        return(true);
                    }
                }
            }

            if (_event.id == "RayHit")
            {
                int partitionIndex = CellSpacePartition.PositionToIndex(((Point)_event.parameters["Ray"]).ToVector2());
                if (EntityWithinBounds(partitionIndex, CellSpacePartition))
                {
                    if (CellSpacePartition.dynamicCells[partitionIndex].FireEvent(_event))
                    {
                        return(true);
                    }
                }

                if (EntityWithinBounds(partitionIndex, CellSpacePartition))
                {
                    if (Game1.world.dynamicCellSpacePartition.dynamicCells[partitionIndex].FireEvent(_event))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        public void Update(GameTime gameTime)
        {
            for (int i = 0; i < _destroyQueue.Count; i++)
            {
                ColliderPartition.Remove(_destroyQueue[i]);
            }


            _collidersInArea.Clear();
            //center
            // delta = 150

            if (Camera.Instance == null)
            {
                throw  new Exception("Camera not initialized");
            }

            List <ICollider> tempColliders =
                ColliderPartition.Cells[ColliderPartition.PositionToIndex(Camera.Instance.center)].Members;

            if (tempColliders != null)
            {
                // TODO: SIMULATE EVERYTHING IN CAMERA BOUNDS
                loadedPartitions[0] = ColliderPartition.PositionToIndex(Camera.Instance.center);
                if (CellWithinBounds(loadedPartitions[0]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[0]].Members);
                }


                loadedPartitions[1] = ColliderPartition.PositionToIndex(Camera.Instance.center) + 150;
                if (CellWithinBounds(loadedPartitions[1]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[1]].GetMembers());
                }

                loadedPartitions[2] = ColliderPartition.PositionToIndex(Camera.Instance.center) - 150;
                if (CellWithinBounds(loadedPartitions[2]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[2]].GetMembers());
                }

                loadedPartitions[3] = ColliderPartition.PositionToIndex(Camera.Instance.center) - 151;
                if (CellWithinBounds(loadedPartitions[3]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[3]].GetMembers());
                }

                loadedPartitions[4] = ColliderPartition.PositionToIndex(Camera.Instance.center) - 149;
                if (CellWithinBounds(loadedPartitions[4]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[4]].GetMembers());
                }

                loadedPartitions[5] = ColliderPartition.PositionToIndex(Camera.Instance.center) + 149;
                if (CellWithinBounds(loadedPartitions[5]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[5]].GetMembers());
                }

                loadedPartitions[6] = ColliderPartition.PositionToIndex(Camera.Instance.center) + 151;
                if (CellWithinBounds(loadedPartitions[6]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[6]].GetMembers());
                }

                loadedPartitions[7] = ColliderPartition.PositionToIndex(Camera.Instance.center) + 1;
                if (CellWithinBounds(loadedPartitions[7]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[7]].GetMembers());
                }

                loadedPartitions[8] = ColliderPartition.PositionToIndex(Camera.Instance.center) - 1;
                if (CellWithinBounds(loadedPartitions[8]))
                {
                    _collidersInArea.AddRange(ColliderPartition
                                              .Cells[loadedPartitions[8]].GetMembers());
                }
            }

            for (int i = 0; i < partitionsToUpdate.Count; i++)
            {
                _collidersInArea.AddRange(ColliderPartition
                                          .Cells[partitionsToUpdate[i]].GetMembers());
            }

            UpdateParticles(_collidersInArea, gameTime);
            //CheckCollisions(_collidersInArea);

            partitionsToUpdate.Clear();
        }