public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
                UnitType unittype = UnitType.None;

                GameData.Owner owner       = GameData.Owner.None;
                int            hp          = 0;
                Guid           guid        = Guid.Empty;
                bool           gotUnittype = false;
                bool           gotGuid     = false;
                bool           gotOwner    = false;
                bool           gotHp       = false;

                while (reader.Read())
                {
                    if (reader.TokenType != JsonToken.PropertyName)
                    {
                        break;
                    }

                    string propertyName = (string)reader.Value;
                    if (!reader.Read())
                    {
                        continue;
                    }
                    switch (propertyName)
                    {
                    case "UnitType":
                        unittype    = (serializer.Deserialize <string>(reader)).ToEnum <UnitType>();
                        gotUnittype = true;
                        break;

                    case "Owner":
                        owner    = (serializer.Deserialize <string>(reader)).ToEnum <GameData.Owner>();
                        gotOwner = true;
                        break;

                    case "HP":
                        hp    = serializer.Deserialize <int>(reader);
                        gotHp = true;
                        break;

                    case "guid":
                        guid    = serializer.Deserialize <Guid>(reader);
                        gotGuid = true;
                        //CONTENT_MANAGER.ShowMessageBox(guid.ToString());
                        break;

                    default:
                        break;
                    }
                }

                if (!(gotUnittype && gotHp && gotOwner && gotGuid))
                {
                    //throw new InvalidDataException("Not enought data");
                    return(null);
                }

                return(UnitCreationHelper.Create(unittype, owner, hp, AnimationName.idle, guid));
            }
Example #2
0
        float delay            = 25; //ms

        public MovingUnitAnimation(Unit unit, List <Point> movementPath, Point startingPoint)
        {
            movingUnit = UnitCreationHelper.Create(unit.UnitType, unit.Owner);
            movingUnit.Animation.Depth = LayerDepth.Unit + 0.01f;
            this.movementPath          = movementPath;
            movingUnitPosition         = startingPoint;
        }
Example #3
0
        public override bool Init()
        {
            camera = new Camera(_device.Viewport);

            map = new Map(9, 9);
            map.Fill(TerrainType.Plain);

            map[3, 3].terrain = TerrainType.Mountain;
            map[4, 3].terrain = TerrainType.Mountain;
            map[3, 4].terrain = TerrainType.Mountain;
            map[3, 5].terrain = TerrainType.Mountain;
            map[3, 6].terrain = TerrainType.Mountain;

            map[5, 3].terrain = TerrainType.Sea;
            map[6, 3].terrain = TerrainType.Sea;
            map[6, 4].terrain = TerrainType.River;
            map[6, 5].terrain = TerrainType.River;

            map[5, 6].terrain = TerrainType.Road;
            map[6, 6].terrain = TerrainType.Road;
            map[7, 6].terrain = TerrainType.Road;
            map[7, 5].terrain = TerrainType.Road;
            map[7, 4].terrain = TerrainType.Road;
            map[7, 3].terrain = TerrainType.Road;

            map.GenerateNavigationMap();

            map[position].unit = UnitCreationHelper.Create(currentUnit, currentColor);
            map[position].unit.Animation.PlayAnimation(AnimationName.idle.ToString());

            console           = new UIClass.Console(new Point(0, 0), new Vector2(720, 200), CONTENT_MANAGER.hackfont);
            console.IsVisible = false;

            console.SetVariable("map", map);

            return(base.Init());
        }
Example #4
0
        public override void Update(GameTime gameTime)
        {
            mouseInputState        = CONTENT_MANAGER.inputState.mouseState;
            lastMouseInputState    = CONTENT_MANAGER.lastInputState.mouseState;
            keyboardInputState     = CONTENT_MANAGER.inputState.keyboardState;
            lastKeyboardInputState = CONTENT_MANAGER.lastInputState.keyboardState;

            selectedMapCell = Utility.HelperFunction.TranslateMousePosToMapCellPos(mouseInputState.Position, camera, map.Width, map.Height);

            if (HelperFunction.IsKeyPress(Keys.OemTilde))
            {
                console.IsVisible = !console.IsVisible;
            }

            #region change unit and animation
            if (console.IsVisible) //suck all input in to the input box
            {
                console.Update(CONTENT_MANAGER.inputState, CONTENT_MANAGER.lastInputState);
            }
            else //accept input
            {
                //cylce through unit
                if (HelperFunction.IsKeyPress(Keys.Left))
                {
                    if (currentUnit == UnitType.Soldier)
                    {
                        currentUnit = UnitType.Battleship;
                    }
                    else
                    {
                        currentUnit = currentUnit.Previous();
                    }
                }

                if (HelperFunction.IsKeyPress(Keys.Right))
                {
                    if (currentUnit == UnitType.Battleship)
                    {
                        currentUnit = UnitType.Soldier;
                    }
                    else
                    {
                        currentUnit = currentUnit.Next();
                    }
                }

                //cycle through animation
                if (HelperFunction.IsKeyPress(Keys.Up))
                {
                    if (currentAnimation == AnimationName.idle)
                    {
                        currentAnimation = AnimationName.done;
                    }
                    else
                    {
                        currentAnimation = currentAnimation.Previous();
                    }
                }
                if (HelperFunction.IsKeyPress(Keys.Down))
                {
                    if (currentAnimation == AnimationName.done)
                    {
                        currentAnimation = AnimationName.idle;
                    }
                    else
                    {
                        currentAnimation = currentAnimation.Next();
                    }
                }

                //cycle through color
                if (HelperFunction.IsKeyPress(Keys.E))
                {
                    if (currentColor == GameData.Owner.Yellow)
                    {
                        currentColor = GameData.Owner.Red;
                    }
                    else
                    {
                        currentColor = currentColor.Next();
                    }
                }
                if (HelperFunction.IsKeyPress(Keys.Q))
                {
                    if (currentColor == GameData.Owner.Red)
                    {
                        currentColor = GameData.Owner.Yellow;
                    }
                    else
                    {
                        currentColor = currentColor.Previous();
                    }
                }
            }

            Unit           unit          = map[position].unit;
            UnitType       nextUnit      = unit.UnitType;
            GameData.Owner nextOwner     = unit.Owner;
            AnimationName  nextAnimation = unit.Animation.CurntAnimationName.ToEnum <AnimationName>();
            bool           isChanged     = false;

            if (nextUnit != currentUnit)
            {
                isChanged = true;
            }

            if (nextOwner != currentColor)
            {
                isChanged = true;
            }

            if (nextAnimation != currentAnimation)
            {
                isChanged = true;
            }

            if (isChanged)
            {
                map[position].unit = UnitCreationHelper.Create(currentUnit, currentColor, startingAnimation: currentAnimation);
            }
            map[position].unit.Animation.Update(gameTime);
            #endregion

            if (mouseInputState.LeftButton == ButtonState.Released &&
                lastMouseInputState.LeftButton == ButtonState.Pressed)
            {
                SelectUnit();
            }

            //if (isMovingUnitAnimPlaying)
            //{
            //    UpdateMovingUnit(gameTime);
            //}

            //calculate movepath
            if (isMovePathCalculated)
            {
                if (movementRange.Contains(selectedMapCell) && selectedMapCell != lastSelectedMapCell)
                {
                    //update movement path
                    movementPath = DijkstraHelper.FindPath(dijkstarGraph, selectedMapCell);
                    dirarrowRenderer.UpdatePath(movementPath);
                    lastSelectedMapCell = selectedMapCell;
                }
            }

            base.Update(gameTime);
        }