public GameWorld(ControlManager controlManagerRef)
        {
            ControlManagerRef = controlManagerRef;
            Map = new TileMap(200, 400);
            MainPlayer = new GamePlayer(this);
            MainPlayer.Name = "Osama Abulail";
            int cloudsCount = (Map.MapWidth*Map.MapHeight)/(20*20);
            CloudManager = new CloudManager(Map, cloudsCount, CloudDirection.West, 0.5f);
            MovementManager = new UnitMovementManager(this);
            SelectionManager = new UnitSelectionManager(this);
            DockManager = new DockManager(ControlManagerRef);
            UpperStatusBar = new UpperStatusBar(new Vector2(10, 0), MainPlayer, ControlManagerRef);

            _trees = new List<Tree>();

            // generate random trees.
            Random r = new Random();
            for (int i = 0; i < 4000; i++)
            {
                Point treeLoc = new Point(r.Next(0,Map.MapWidth), r.Next(0,Map.MapHeight));
                if (Map.MapCellAt(treeLoc).Walkable)
                {
                    Tree t = new Tree("tree" + r.Next(1, 26), this, treeLoc);
                    _trees.Add(t);
                }
            }

            // border
            _border = new PictureBox(GameGraphics.GetTexture("gameplay_border").SourceTexture,
               new Rectangle(0, 0, 1366, 768));
            ControlManagerRef.Add(_border);
        }
        public CloudManager(TileMap mapRef, int cloudCount, CloudDirection cloudDirection, float speed)
        {
            _mapRef = mapRef;
            _cloudCount = cloudCount;
            _cloudDirection = cloudDirection;

            const double epsilon = 0.2f;
            if (Math.Abs(speed - 0.0f) < epsilon) throw new Exception("cloud speed too slow");

            _startingXWest = mapRef.MapWidth * TileInfo.TileStepX;
            _endingXWest = -1 * MaxCloudWidth;

            _startingXEast = _endingXWest;
            _endingXEast = _startingXWest;

            _xpositions = new float[cloudCount];
            _ypositions = new float[cloudCount];
            _speeds = new float[cloudCount];
            _drawDepths = new float[cloudCount];

            int x, y;
            for (int i = 0; i < cloudCount; i++)
            {
                x = _random.Next(0, mapRef.MapWidth * TileInfo.TileStepX - MaxCloudWidth);
                y = _random.Next(-1 * MaxCloudHeight, mapRef.MapHeight * TileInfo.TileStepY - MaxCloudHeight);
                _xpositions[i] = x;
                _ypositions[i] = y;
                _speeds[i] = _random.Next(1, ((int)(speed * 10.0f))) / 10.0f;
                _drawDepths[i] = StartDrawDepth + i*DrawDepthIncrement;
            }

            _shadowOffset = new Point(CloudInfo.CloudShadowOffsetX, CloudInfo.CloudShadowOffsetY);

            _cloudVisuals = new List<CloudVisual>();
            CloudVisual cv1, cv2, cv3;

            cv1 = new CloudVisual();
            cv1.CloudTexture = GameGraphics.GetTexture("cloud1");
            cv1.CloudShadowTexture = GameGraphics.GetTexture("cloud1_shadow");

            cv2 = new CloudVisual();
            cv2.CloudTexture = GameGraphics.GetTexture("cloud2");
            cv2.CloudShadowTexture = GameGraphics.GetTexture("cloud2_shadow");

            cv3 = new CloudVisual();
            cv3.CloudTexture = GameGraphics.GetTexture("cloud3");
            cv3.CloudShadowTexture = GameGraphics.GetTexture("cloud3_shadow");

            _cloudVisuals.Add(cv1);
            _cloudVisuals.Add(cv2);
            _cloudVisuals.Add(cv3);

            _cloudVisualIndex = new List<int>();
            for (var i = 0; i < cloudCount; i++)
            {
                _cloudVisualIndex.Add(_random.Next(0, _cloudVisuals.Count));
            }
        }
 public static List<Vector2> FindPath(Vector2 startTile, Vector2 endTile, TileMap mapRef)
 {
     if (!mapRef.MapCellAt(startTile).Walkable || !mapRef.MapCellAt(endTile).Walkable)
     {
         return null;
     }
     OpenList.Clear();
     NodeCosts.Clear();
     nodeStatus.Clear();
     var endNode = new PathNode(null, null, endTile, mapRef.MapCellAt(endTile).CenterPosition, 0);
     var startNode = new PathNode(null, endNode, startTile, mapRef.MapCellAt(startTile).CenterPosition, 0);
     AddNodeToOpenList(startNode);
     while (OpenList.Count > 0)
     {
         var currentNode = OpenList[OpenList.Count - 1];
         if (currentNode.IsEqualToNode(endNode))
         {
             var bestPath = new List<Vector2>();
             while (currentNode != null)
             {
                 bestPath.Insert(0, currentNode.GridLocation);
                 currentNode = currentNode.ParentNode;
             }
             return bestPath;
         }
         OpenList.Remove(currentNode);
         NodeCosts.Remove(currentNode.GridLocation);
         foreach (
         var possibleNode in
         FindAdjacentNodes(currentNode, endNode, mapRef))
         {
             if (nodeStatus.ContainsKey(possibleNode.GridLocation))
             {
                 if (nodeStatus[possibleNode.GridLocation] ==
                 NodeStatus.Closed)
                 {
                     continue;
                 }
                 if (
                 nodeStatus[possibleNode.GridLocation] ==
                 NodeStatus.Open)
                 {
                     if (possibleNode.TotalCost >=
                     NodeCosts[possibleNode.GridLocation])
                     {
                         continue;
                     }
                 }
             }
             AddNodeToOpenList(possibleNode);
         }
         nodeStatus[currentNode.GridLocation] = NodeStatus.Closed;
     }
     return null;
 }
        public FarmBase(string identifier, GameWorld gameWorldRef, GamePlayer playerRef, Point location)
        {
            Texture = GameGraphics.GetTexture(identifier);
            Location = location;
            _gameWorldRef = gameWorldRef;
            PlayerRef = playerRef;
            MapRef = _gameWorldRef.Map;

            _drawDepth = 1.0f - TileEngineInfo.HeightRowDepthMod;

            _rowOffset = (MapRef.MapCellAt(location).OnOddRow) ? TileInfo.OddRowXOffset : 0;

            FarmOrigin =
               new Vector2((Location.X * TileInfo.TileStepX) + _rowOffset + Texture.AlignmentOffset.X,
                   Location.Y * TileInfo.TileStepY - Texture.AlignmentOffset.Y);
        }
        protected CreatureBase(string animationIdentifier, 
            int frameWidth, int frameHeight, int frames,
            GameWorld gameWorldRef, GamePlayer playerRef, Vector2 position, float animationSpeed)
        {
            PlayerRef = playerRef;
            GameWorldRef = gameWorldRef;
            _mapRef = gameWorldRef.Map ;

            // walking animation is unified in all creatures
            #region setting appropriate walking animation
            AnimationSet = GameGraphics.GetTexture(animationIdentifier);
            Animation = new SpriteAnimation(AnimationSet.SourceTexture);

            Animation.AddAnimation("Walk_E", 0, frameHeight * 0, frameWidth, frameHeight, frames, animationSpeed);
            Animation.AddAnimation("Walk_N", 0, frameHeight * 1, frameWidth, frameHeight, frames, animationSpeed);
            Animation.AddAnimation("Walk_NE", 0, frameHeight * 2, frameWidth, frameHeight, frames, animationSpeed);
            Animation.AddAnimation("Walk_NW", 0, frameHeight * 3, frameWidth, frameHeight, frames, animationSpeed);
            Animation.AddAnimation("Walk_S", 0, frameHeight * 4, frameWidth, frameHeight, frames, animationSpeed);
            Animation.AddAnimation("Walk_SE", 0, frameHeight * 5, frameWidth, frameHeight, frames, animationSpeed);
            Animation.AddAnimation("Walk_SW", 0, frameHeight * 6, frameWidth, frameHeight, frames, animationSpeed);
            Animation.AddAnimation("Walk_W", 0, frameHeight * 7, frameWidth, frameHeight, frames, animationSpeed);

            Animation.AddAnimation("Idle_E", 3 * frameWidth, frameHeight * 0, frameWidth, frameHeight, 1, 0.2f);
            Animation.AddAnimation("Idle_N", 3 * frameWidth, frameHeight * 1, frameWidth, frameHeight, 1, 0.2f);
            Animation.AddAnimation("Idle_NE", 3 * frameWidth, frameHeight * 2, frameWidth, frameHeight, 1, 0.2f);
            Animation.AddAnimation("Idle_NW", 3 * frameWidth, frameHeight * 3, frameWidth, frameHeight, 1, 0.2f);
            Animation.AddAnimation("Idle_S", 3 * frameWidth, frameHeight * 4, frameWidth, frameHeight, 1, 0.2f);
            Animation.AddAnimation("Idle_SE", 3 * frameWidth, frameHeight * 5, frameWidth, frameHeight, 1, 0.2f);
            Animation.AddAnimation("Idle_SW", 3 * frameWidth, frameHeight * 6, frameWidth, frameHeight, 1, 0.2f);
            Animation.AddAnimation("Idle_W", 3 * frameWidth, frameHeight * 7, frameWidth, frameHeight, 1, 0.2f);

            CurrentAnimation = "Idle_SE";

            Animation.IsAnimating = true;

            #endregion

            Position = position;
            _currentMoveDir = Vector2.Zero;
            _previousMovDir = Vector2.Zero;
            Distination = position;

            HealthStatusLine = new StatusLine(StatusLineType.Green);
            _healthStatusLineOffset = new Vector2(-13, -50);

            Actions = new ActionManager();
        }
Пример #6
0
        public Tree(string Identifier, GameWorld gameWorldRef, Point Location)
        {
            this.texture = GameGraphics.GetTexture(Identifier);
            _gameWorldRef = gameWorldRef;
            this.Location = Location;
            this.MapRef = _gameWorldRef.Map;

             rowOffset = (MapRef.MapCellAt(Location).OnOddRow) ? TileInfo.OddRowXOffset : 0;
             depth = MapRef.MapCellAt(Location).DrawDepth;

            // make tree place unwalkable..
            MapRef.MapCellAt(Location).Walkable = false;
            // add tree shadow to base tiles of the occupied cell..
            MapRef.MapCellAt(Location).AddTopperTile("tree_shadow");
            Position = new Vector2((Location.X * TileInfo.TileStepX) + rowOffset + texture.AlignmentOffset.X,
                Location.Y * TileInfo.TileStepY - texture.AlignmentOffset.Y);
            SelectionBox = new Rectangle((int)Position.X, (int)Position.Y, texture.SourceRectangle.Width, texture.SourceRectangle.Height);
        }
        /// <summary>
        /// static method to find a path for a movable object from cell to cell using a tile map
        /// </summary>
        /// <param name="movable"></param>
        /// <param name="fromCell"></param>
        /// <param name="toCell"></param>
        /// <param name="mapRef"></param>
        public static void SetMovableToMovePath(IMovable movable, Vector2 fromCell, Vector2 toCell, TileMap mapRef)
        {
            var path = PathFinder.FindPath(fromCell, toCell, mapRef);
            movable.MovingPath = path;

            if (path == null || path.Count <= 1) return;

            movable.CurrentPathSegment = 1;

            // initially move to first segment..
            Vector2 cellCenter = mapRef.MapCellAt(movable.MovingPath[movable.CurrentPathSegment]).CenterPosition;
            movable.Distination = cellCenter;

            Vector2 difference = movable.Distination - movable.Position;

            if (difference != Vector2.Zero)
            {
                movable.MoveDirection = Vector2.Normalize(difference);
            }
        }
        protected BuildingBase(string identifier, GameWorld gameWorldRef, GamePlayer playerRef, Point location)
        {
            Texture = GameGraphics.GetTexture(identifier);
            Location = location;
            GameWorldRef = gameWorldRef;
            MapRef = gameWorldRef.Map;
            PlayerRef = playerRef;

            var rowOffset = (MapRef.MapCellAt(location).OnOddRow) ? TileInfo.OddRowXOffset : 0;

            BuildingOrigin =
                new Vector2((location.X * TileInfo.TileStepX) + rowOffset + Texture.AlignmentOffset.X,
                    location.Y * TileInfo.TileStepY - Texture.AlignmentOffset.Y);

            #region Build Segments of the texture..
            var imgSegmentsF = new List<Rectangle>();
            var imgSegmentsB = new List<Rectangle>();

            var segWidth = TileInfo.TileWidth / 2; // seg_width = 32

            var doubleDivision = (Texture.SourceRectangle.Width / 2.0) / segWidth;
            var intDivision = (double)((int)doubleDivision);
            // check if image width not devided by half of tile width (32)
            const double epsilon = 0.01f;
            var isOverloaded = (!(Math.Abs(doubleDivision - intDivision) < epsilon));

            int height = Texture.SourceRectangle.Height;
            int mid = Texture.SourceRectangle.Width / 2;

            int forwardIndexer = mid;
            int backwardIndexer = mid - segWidth;

            for (int i = 0; i < intDivision; i++)
            {
                var rectForward = new Rectangle(forwardIndexer, Texture.SourceRectangle.Y, segWidth, height);
                var rectBackward = new Rectangle(backwardIndexer, Texture.SourceRectangle.Y, segWidth, height);

                imgSegmentsB.Add(rectBackward);
                imgSegmentsF.Add(rectForward);

                forwardIndexer += segWidth;
                backwardIndexer -= segWidth;
            }

            // next, using latest value of forward_indexer
            if (isOverloaded)
            {
                var leftSegWidth = mid - (int)intDivision * segWidth;
                var lastRectRight = new Rectangle(/**/forwardIndexer/**/, Texture.SourceRectangle.Y, leftSegWidth, height);
                var lastRectLeft = new Rectangle(Texture.SourceRectangle.X, Texture.SourceRectangle.Y, leftSegWidth, height);

                imgSegmentsB.Add(lastRectLeft);
                imgSegmentsF.Add(lastRectRight);
            }

            // setting depths of segments..
            var indexer = location;
            _imgSegmentsDepths = new List<SegDepth>();

            // first forward and backward segments have same depth as cell at this building's Location
            var depth = MapRef.MapCellAt(location).DrawDepth;
            _imgSegmentsDepths.Add(new SegDepth(imgSegmentsF[0], depth));
            _imgSegmentsDepths.Add(new SegDepth(imgSegmentsB[0], depth));

            // moving to the tiles on the right..
            for (var i = 1; i < imgSegmentsF.Count; i++)
            {
                indexer = indexer.WalkTo(Direction.NE);
                depth = MapRef.MapCellAt(indexer).DrawDepth;
                _imgSegmentsDepths.Add(new SegDepth(imgSegmentsF[i], depth));
            }

            //resetting
            indexer = location;

            // moving to the tiles on the left..
            for (int i = 1; i < imgSegmentsB.Count; i++)
            {
                indexer = indexer.WalkTo(Direction.NW);
                depth = MapRef.MapCellAt(indexer).DrawDepth;
                _imgSegmentsDepths.Add(new SegDepth(imgSegmentsB[i], depth));
            }

            // constructing an ordered version of the img_segments_depths list..
            ImgSegmentsDepthsOrdered = new List<SegDepth>();
            for (int i = _imgSegmentsDepths.Count - 1; i >= _imgSegmentsDepths.Count - (imgSegmentsB.Count - 1); i--)
            {
                ImgSegmentsDepthsOrdered.Add(_imgSegmentsDepths[i]);
            }
            ImgSegmentsDepthsOrdered.Add(_imgSegmentsDepths[1]);
            ImgSegmentsDepthsOrdered.Add(_imgSegmentsDepths[0]);

            for (int i = 2; i < (imgSegmentsF.Count - 1) + 2; i++)
            {
                ImgSegmentsDepthsOrdered.Add(_imgSegmentsDepths[i]);
            }

            ///
            /// now, img_segments_depths_ordered is constructed properly
            /// so that it can be used to access and / or modify the segments of
            /// the building in the child classes of this class
            ///

            #endregion

            #region Generating Built Units Locations and idle animations
            BuiltUnitsLocations = new List<Vector2>();
            var cells = new List<Point>();
            var index = Location;
            index = index.WalkTo(Direction.S).WalkTo(Direction.NE)
                .WalkTo(Direction.NE).WalkTo(Direction.NE);
            cells.Add(index);
            index = index.WalkTo(Direction.SW);
            cells.Add(index);
            index = index.WalkTo(Direction.SW);
            cells.Add(index);
            index = index.WalkTo(Direction.SW);
            cells.Add(index);
            index = index.WalkTo(Direction.NW);
            cells.Add(index);
            index = index.WalkTo(Direction.NW);
            cells.Add(index);
            index = index.WalkTo(Direction.NW);
            cells.Add(index);
            _builtUnitIndex = 0;
            foreach (var point in cells)
            {
                BuiltUnitsLocations.Add(MapRef.MapCellAt(point).CenterPosition);
            }

            // setting initial idle animations to look away from building
            InitialIdleAnimation = new List<string>();
            InitialIdleAnimation.Add("Idle_SE");
            InitialIdleAnimation.Add("Idle_SE");
            InitialIdleAnimation.Add("Idle_SE");
            InitialIdleAnimation.Add("Idle_S");
            InitialIdleAnimation.Add("Idle_SW");
            InitialIdleAnimation.Add("Idle_SW");
            InitialIdleAnimation.Add("Idle_S");
            InitialIdleAnimation.Add("Idle_SW");
            InitialIdleAnimation.Add("Idle_SW");
            #endregion
        }
        private static IEnumerable<PathNode> FindAdjacentNodes( PathNode currentNode, PathNode endNode, TileMap mapRef)
        {
            var adjacentNodes = new List<PathNode>();
            var current = currentNode.GridLocation;

            var v2Pointer = current.WalkTo(Direction.N);
            if(mapRef.MapCellAt(v2Pointer).Walkable)
                adjacentNodes.Add(new PathNode(currentNode, endNode, v2Pointer, mapRef.MapCellAt(v2Pointer).CenterPosition, currentNode.DirectCost + PathFinder.DirectCostNs));

            v2Pointer = current.WalkTo(Direction.NE);
            if (mapRef.MapCellAt(v2Pointer).Walkable)
                adjacentNodes.Add(new PathNode(currentNode, endNode, v2Pointer, mapRef.MapCellAt(v2Pointer).CenterPosition, currentNode.DirectCost + PathFinder.DirectCostDiagonaly));

            v2Pointer = current.WalkTo(Direction.E);
            if (mapRef.MapCellAt(v2Pointer).Walkable)
                adjacentNodes.Add(new PathNode(currentNode, endNode, v2Pointer, mapRef.MapCellAt(v2Pointer).CenterPosition, currentNode.DirectCost + PathFinder.DirectCostEw));

            v2Pointer = current.WalkTo(Direction.SE);
            if (mapRef.MapCellAt(v2Pointer).Walkable)
                adjacentNodes.Add(new PathNode(currentNode, endNode, v2Pointer, mapRef.MapCellAt(v2Pointer).CenterPosition, currentNode.DirectCost + PathFinder.DirectCostDiagonaly));

            v2Pointer = current.WalkTo(Direction.S);
            if (mapRef.MapCellAt(v2Pointer).Walkable)
                adjacentNodes.Add(new PathNode(currentNode, endNode, v2Pointer, mapRef.MapCellAt(v2Pointer).CenterPosition, currentNode.DirectCost + PathFinder.DirectCostNs));

            v2Pointer = current.WalkTo(Direction.SW);
            if (mapRef.MapCellAt(v2Pointer).Walkable)
                adjacentNodes.Add(new PathNode(currentNode, endNode, v2Pointer, mapRef.MapCellAt(v2Pointer).CenterPosition, currentNode.DirectCost + PathFinder.DirectCostDiagonaly));

            v2Pointer = current.WalkTo(Direction.W);
            if (mapRef.MapCellAt(v2Pointer).Walkable)
                adjacentNodes.Add(new PathNode(currentNode, endNode, v2Pointer, mapRef.MapCellAt(v2Pointer).CenterPosition, currentNode.DirectCost + PathFinder.DirectCostEw));

            v2Pointer = current.WalkTo(Direction.NW);
            if (mapRef.MapCellAt(v2Pointer).Walkable)
                adjacentNodes.Add(new PathNode(currentNode, endNode, v2Pointer, mapRef.MapCellAt(v2Pointer).CenterPosition, currentNode.DirectCost + PathFinder.DirectCostDiagonaly));

            return adjacentNodes;
        }