CS代考 package dungeonmania;

package dungeonmania;

import java.util.List;
import java.util.PriorityQueue;

Copyright By PowCoder代写 加微信 powcoder

import java.util.UUID;

import dungeonmania.battles.BattleFacade;
import dungeonmania.entities.Entity;
import dungeonmania.entities.EntityFactory;
import dungeonmania.entities.Interactable;
import dungeonmania.entities.Player;
import dungeonmania.entities.collectables.Bomb;
import dungeonmania.entities.collectables.Treasure;
import dungeonmania.entities.collectables.potions.Potion;
import dungeonmania.entities.enemies.Enemy;
import dungeonmania.exceptions.InvalidActionException;
import dungeonmania.goals.Goal;
import dungeonmania.map.GameMap;
import dungeonmania.util.Direction;

public class Game {
private String id;
private String name;
private Goal goals;
private GameMap map;
private Player player;
private BattleFacade battleFacade;
private int initialTreasureCount;
private EntityFactory entityFactory;
private boolean isInTick = false;
public static final int PLAYER_MOVEMENT = 0;
public static final int PLAYER_MOVEMENT_CALLBACK = 1;
public static final int AI_MOVEMENT = 2;
public static final int AI_MOVEMENT_CALLBACK = 3;

private int tickCount = 0;
private PriorityQueue sub = new PriorityQueue<>();
private PriorityQueue addingSub = new PriorityQueue<>();

public Game(String dungeonName) {
this.name = dungeonName;
this.map = new GameMap();
this.battleFacade = new BattleFacade();

public void init() {
this.id = UUID.randomUUID().toString();
map.init();
this.tickCount = 0;
player = map.getPlayer();
register(() -> player.onTick(tickCount), PLAYER_MOVEMENT, “potionQueue”);
initialTreasureCount = map.getEntities(Treasure.class).size();

public Game tick(Direction movementDirection) {
registerOnce(
() -> player.move(this.getMap(), movementDirection), PLAYER_MOVEMENT, “playerMoves”);
return this;

public Game tick(String itemUsedId) throws InvalidActionException {
Entity item = player.getEntity(itemUsedId);
if (item == null)
throw new InvalidActionException(String.format(“Item with id %s doesn’t exist”, itemUsedId));
if (!(item instanceof Bomb) && !(item instanceof Potion))
throw new IllegalArgumentException(String.format(“%s cannot be used”, item.getClass()));

registerOnce(() -> {
if (item instanceof Bomb)
player.use((Bomb) item, map);
if (item instanceof Potion)
player.use((Potion) item, tickCount);
}, PLAYER_MOVEMENT, “playerUsesItem”);
return this;

* Battle between player and enemy
* @param player
* @param enemy
public void battle(Player player, Enemy enemy) {
battleFacade.battle(this, player, enemy);
if (player.getBattleStatistics().getHealth() <= 0) { map.destroyEntity(player); if (enemy.getBattleStatistics().getHealth() <= 0) { map.destroyEntity(enemy); public Game build(String buildable) throws InvalidActionException { List buildables = player.getBuildables();
if (!buildables.contains(buildable)) {
throw new InvalidActionException(String.format(“%s cannot be built”, buildable));
registerOnce(() -> player.build(buildable, entityFactory), PLAYER_MOVEMENT, “playerBuildsItem”);
return this;

public Game interact(String entityId) throws IllegalArgumentException, InvalidActionException {
Entity e = map.getEntity(entityId);
if (e == null || !(e instanceof Interactable))
throw new IllegalArgumentException(“Entity cannot be interacted”);
if (!((Interactable) e).isInteractable(player)) {
throw new InvalidActionException(“Entity cannot be interacted”);
registerOnce(
() -> ((Interactable) e).interact(player, this), PLAYER_MOVEMENT, “playerBuildsItem”);
return this;

public long countEntities(Class type) {
return map.countEntities(type);

public void register(Runnable r, int priority, String id) {
if (isInTick)
addingSub.add(new ComparableCallback(r, priority, id));
sub.add(new ComparableCallback(r, priority, id));

public void registerOnce(Runnable r, int priority, String id) {
if (isInTick)
addingSub.add(new ComparableCallback(r, priority, id, true));
sub.add(new ComparableCallback(r, priority, id, true));

public void unsubscribe(String id) {
for (ComparableCallback c : sub) {
if (id.equals(c.getId())) {
c.invalidate();
for (ComparableCallback c : addingSub) {
if (id.equals(c.getId())) {
c.invalidate();

public int tick() {
isInTick = true;
sub.forEach(s -> s.run());
isInTick = false;
sub.addAll(addingSub);
addingSub = new PriorityQueue<>();
sub.removeIf(s -> !s.isValid());
tickCount++;
// update the weapons/potions duration
return tickCount;

public int getTick() {
return this.tickCount;

public String getId() {
return id;

public void setId(String id) {
this.id = id;

public String getName() {
return name;

public void setName(String name) {
this.name = name;

public Goal getGoals() {
return goals;

public void setGoals(Goal goals) {
this.goals = goals;

public GameMap getMap() {
return map;

public void setMap(GameMap map) {
this.map = map;

public EntityFactory getEntityFactory() {
return entityFactory;

public void setEntityFactory(EntityFactory factory) {
entityFactory = factory;

public Player getPlayer() {
return player;

public void setPlayer(Player player) {
this.player = player;

public BattleFacade getBattleFacade() {
return battleFacade;

public void setBattleFacade(BattleFacade battleFacade) {
this.battleFacade = battleFacade;

public int getInitialTreasureCount() {
return initialTreasureCount;

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com