protected override bool OnStep(Map map) { if (X == lastX && Y == lastY && Layer == lastLayer && lastResult) { return true; } else { lastResult = map.OnStep(X, Y, Layer); lastX = X; lastY = Y; lastLayer = Layer; return lastResult; } }
public override void TryDoingSomething(Sprite me, Map map) { if (me.IsMoving) return; script.Owner = me; object result = script.Run(); if (result != null && result.GetType() == typeof(string)) { string res = (string)result; switch (res.ToLower()) { case "up": me.PlanMove(Sprite.Dir.Up, map); break; case "down": me.PlanMove(Sprite.Dir.Down, map); break; case "left": me.PlanMove(Sprite.Dir.Left, map); break; case "right": me.PlanMove(Sprite.Dir.Right, map); break; default: me.PlanMove(Sprite.Dir.None, map); break; } } }
public override void TryDoingSomething(Sprite me, Map map) { if (game.Input.IsPressed(UserInput.Buttons.Up)) { me.PlanMove(Sprite.Dir.Up, map); } else if (game.Input.IsPressed(UserInput.Buttons.Down)) { me.PlanMove(Sprite.Dir.Down, map); } else if (game.Input.IsPressed(UserInput.Buttons.Left)) { me.PlanMove(Sprite.Dir.Left, map); } else if (game.Input.IsPressed(UserInput.Buttons.Right)) { me.PlanMove(Sprite.Dir.Right, map); } if (game.Input.IsPressed(UserInput.Buttons.Action)) { me.PlanAction(map); } }
public override void TryDoingSomething(Sprite me, Map map) { }
protected virtual bool OnStep(Map map) { return false; }
/// <summary> /// Sets the sprite's next move. /// Used only by MovementAIs. /// </summary> /// <param name="direction"></param> /// <param name="map"></param> /// <seealso cref="PlanAction"/> public void PlanMove(Dir direction, Map map) { nextMove = Dir.None; if (moving == Dir.None && direction != Dir.None) { facing = direction; graphic.SetDirection(direction); graphic.SetState("still"); } switch (direction) { case Dir.Up: if (map.IsPassable(x, y - 1, layer)) { nextMove = Dir.Up; } else if (moving == Dir.None) { map.OnCollide(x, y - 1, layer, this); } break; case Dir.Down: if (map.IsPassable(x, y + 1, layer)) { nextMove = Dir.Down; } else if (moving == Dir.None) { map.OnCollide(x, y + 1, layer, this); } break; case Dir.Left: if (map.IsPassable(x - 1, y, layer)) { nextMove = Dir.Left; } else if (moving == Dir.None) { map.OnCollide(x - 1, y, layer, this); } break; case Dir.Right: if (map.IsPassable(x + 1, y, layer)) { nextMove = Dir.Right; } else if (moving == Dir.None) { map.OnCollide(x + 1, y, layer, this); } break; } }
/// <summary> /// Plans the next movement to interact with another sprite. /// Used only by MovementAIs. /// </summary> /// <param name="map"></param> /// <seealso cref="PlanMove"/> public void PlanAction(Map map) { if (moving != Dir.None) return; switch (facing) { case Dir.Up: map.OnAction(x, y - 1, layer); break; case Dir.Down: map.OnAction(x, y + 1, layer); break; case Dir.Left: map.OnAction(x - 1, y, layer); break; case Dir.Right: map.OnAction(x + 1, y, layer); break; } map.OnAction(x, y, layer); }
/// <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(); }
/// <summary> /// Decides the next action of the sprite. /// </summary> /// <param name="me">The sprite whose action is to be decided.</param> /// <param name="map">The map the sprite is on.</param> public abstract void TryDoingSomething(Sprite me, Map map);