Esempio n. 1
0
        /// <summary>
        /// Decides what to do next (move or interact) and tries to execute it.
        /// </summary>
        /// <param name="map"></param>
        public void Advance(Map map)
        {
            movementAI.TryDoingSomething(this, map);
            Dir oldMoving = moving;

            if (moving == Dir.None) {
                moving = nextMove;
                if (moving != Dir.None) facing = moving;
            }

            switch (moving) {
                case Dir.Left:
                    if (corrX > 0) {
                        corrX -= speed;
                    }
                    else if (corrX <= 0) {
                        if (oldMoving == moving && OnStep(map)) {
                            corrX = 0;
                            moving = Dir.None;
                            graphic.SetState("still");
                        }
                        else {
                            if (nextMove != Dir.Left) {
                                corrX = 0;
                                moving = Dir.None;
                            }
                            else {
                                corrX += size;
                                map.Move(this, x, y, layer, x - 1, y, layer);
                            }
                        }
                    }
                    break;
                case Dir.Right:
                    if (corrX < 0) {
                        corrX += speed;
                    }
                    else if (corrX >= 0) {
                        if (oldMoving == moving && OnStep(map)) {
                            corrX = 0;
                            moving = Dir.None;
                            graphic.SetState("still");
                        }
                        else {
                            if (nextMove != Dir.Right) {
                                corrX = 0;
                                moving = Dir.None;
                            }
                            else {
                                corrX -= size;
                                map.Move(this, x, y, layer, x + 1, y, layer);
                            }
                        }
                    }
                    break;
                case Dir.Up:
                    if (corrY > 0) {
                        corrY -= speed;
                    }
                    else if (corrY <= 0) {
                        if (oldMoving == moving && OnStep(map)) {
                            corrY = 0;
                            moving = Dir.None;
                            graphic.SetState("still");
                        }
                        else {
                            if (nextMove != Dir.Up) {
                                corrX = 0;
                                moving = Dir.None;
                            }
                            else {
                                corrY += size;
                                map.Move(this, x, y, layer, x, y - 1, layer);
                            }
                        }
                    }
                    break;
                case Dir.Down:
                    if (corrY < 0) {
                        corrY += speed;
                    }
                    else if (corrY >= 0) {
                        if (oldMoving == moving && OnStep(map)) {
                            corrY = 0;
                            moving = Dir.None;
                            graphic.SetState("still");
                        }
                        else {
                            if (nextMove != Dir.Down) {
                                corrX = 0;
                                moving = Dir.None;
                            }
                            else {
                                corrY -= size;
                                map.Move(this, x, y, layer, x, y + 1, layer);
                            }
                        }
                    }
                    break;
            }

            if (moving == Dir.None) {
                moving = nextMove;
                if (moving != Dir.None) facing = moving;
            }

            if (moving != oldMoving) {
                if (moving == Dir.None) {
                    graphic.SetState("still");
                }
                else {
                    graphic.SetState("move");
                    graphic.SetDirection(moving);
                }
            }
            nextMove = Dir.None;
            graphic.Advance();
        }