private void addXpBar(Screen screen) {

    int xpSinceLastLevelUp = (int) (player.xpSinceLastLevelUp());
    int xpNeededForNextLevel = (int) (player.nettoXpNeededForLevel(player.plevel + 1));

    int maxIndex = Art.panel_xpBar[0].length - 1;
    int index = maxIndex - Math.round(xpSinceLastLevelUp * maxIndex / xpNeededForNextLevel);
    if (index < 0) index = 0;
    else if (index > maxIndex) index = maxIndex;

    screen.blit(Art.panel_xpBar[0][index], 311, screen.h - 32);
    screen.blit(Art.panel_star, 314, screen.h - 40);
    Font font = Font.defaultFont();
    font.draw(screen, texts.playerLevel(player.plevel + 1), 335, screen.h - 36);
  }
 private synchronized void initLevel() {
   if (level == null) return;
   // level.init();
   players[0] =
       new Player(
           synchedKeys[0],
           synchedMouseButtons[0],
           level.width * Tile.WIDTH / 2 - 16,
           (level.height - 5 - 1) * Tile.HEIGHT - 16,
           Team.Team1,
           localTeam);
   players[0].setFacing(4);
   level.addEntity(players[0]);
   level.addEntity(new Base(34 * Tile.WIDTH, 7 * Tile.WIDTH, Team.Team1, localTeam));
   if (isMultiplayer) {
     players[1] =
         new Player(
             synchedKeys[1],
             synchedMouseButtons[1],
             level.width * Tile.WIDTH / 2 - 16,
             7 * Tile.HEIGHT - 16,
             Team.Team2,
             localTeam);
     players[1].setLocalTeam(localTeam);
     level.addEntity(players[1]);
     level.addEntity(new Base(32 * Tile.WIDTH - 20, 32 * Tile.WIDTH - 20, Team.Team2, localTeam));
   }
   player = players[localId];
   player.setCanSee(true);
 }
  private synchronized void createLevel(String levelFile) {
    try {
      level = Level.fromFile(levelFile);
    } catch (Exception ex) {
      throw new RuntimeException("Unable to load level", ex);
    }

    level.init();

    players[0] =
        new Player(
            synchedKeys[0],
            mouseButtons,
            level.width * Tile.WIDTH / 2 - 16,
            (level.height - 5 - 1) * Tile.HEIGHT - 16,
            Team.Team1);
    players[0].setFacing(4);
    level.addEntity(players[0]);
    level.addEntity(new Base(34 * Tile.WIDTH, 7 * Tile.WIDTH, Team.Team1));
    if (isMultiplayer) {
      players[1] =
          new Player(
              synchedKeys[1],
              mouseButtons,
              level.width * Tile.WIDTH / 2 - 16,
              7 * Tile.HEIGHT - 16,
              Team.Team2);
      // players[1] = new Player(synchedKeys[1], 10, 10);
      level.addEntity(players[1]);
      level.addEntity(new Base(32 * Tile.WIDTH - 20, 32 * Tile.WIDTH - 20, Team.Team2));
    }
    player = players[localId];
    player.setCanSee(true);
  }
Example #4
0
 @Override
 public void use(Entity user) {
   if (user instanceof Player && ((Player) user).getTeam() == team) {
     Player player = (Player) user;
     if (player.carrying == null && player.getScore() >= COST[type]) {
       player.payCost(COST[type]);
       Building item = null;
       switch (type) {
         case SHOP_TURRET:
           item = new Turret(pos.x, pos.y, team);
           break;
         case SHOP_HARVESTER:
           item = new Harvester(pos.x, pos.y, team);
           break;
         case SHOP_BOMB:
           item = new Bomb(pos.x, pos.y);
           break;
       }
       level.addEntity(item);
       player.pickup(item);
     }
   }
 }
  private synchronized void render(Graphics g) {
    if (level != null) {
      int xScroll = (int) (player.pos.x - screen.w / 2);
      int yScroll = (int) (player.pos.y - (screen.h - 24) / 2);
      soundPlayer.setListenerPosition((float) player.pos.x, (float) player.pos.y);
      level.render(screen, xScroll, yScroll);
    }
    if (!menuStack.isEmpty()) {
      menuStack.peek().render(screen);
    }

    if (Options.getAsBoolean(Options.DRAW_FPS, Options.VALUE_FALSE)) {
      Font.draw(screen, texts.FPS(fps), 10, 10);
    }

    if (player != null && menuStack.size() == 0) {
      if (isMultiplayer) {
        Font.draw(screen, texts.latency(latencyCacheReady() ? "" + avgLatency() : "-"), 10, 20);
      }

      Font.draw(screen, texts.health(player.health, player.maxHealth), 340, screen.h - 16);
      Font.draw(screen, texts.money(player.score), 340, screen.h - 27);
      Font.draw(screen, texts.nextLevel((int) player.getNextLevel()), 340, screen.h - 38);
      Font.draw(screen, texts.playerExp((int) player.pexp), 340, screen.h - 49);
      Font.draw(screen, texts.playerLevel(player.plevel), 340, screen.h - 60);
    }

    if (isMultiplayer && menuStack.isEmpty()) {
      chat.render(screen);
    }

    g.setColor(Color.BLACK);

    g.fillRect(0, 0, getWidth(), getHeight());
    g.translate((getWidth() - GAME_WIDTH * SCALE) / 2, (getHeight() - GAME_HEIGHT * SCALE) / 2);
    g.clipRect(0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE);

    if (!menuStack.isEmpty() || level != null) {

      // render mouse
      renderMouse(screen, mouseButtons);

      g.drawImage(screen.image, 0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE, null);
    }
  }
Example #6
0
  @Override
  public void tick() {
    super.tick();
    if (--freezeTime > 0) {
      return;
    }

    if (isEmptying && (time % 3 == 0)) {
      if (money <= 0) {
        isEmptying = false;
      } else {
        Loot l = new Loot(pos.x, pos.y, 0, 0, 1);
        l.fake = true;
        l.life = 20;
        l.forceTake(emptyingPlayer);
        int toAdd = Math.min(emptyingSpeed, money);
        money -= toAdd;
        emptyingPlayer.addScore(toAdd);
        level.addEntity(l);
      }
    }

    ++time;
    --harvestingTicks;
    isHarvesting = (harvestingTicks >= 0);

    Random random = TurnSynchronizer.synchedRandom;
    if (isHarvesting) {
      if (random.nextDouble() < 0.050f) {
        level.addEntity(
            new SmokeAnimation(
                pos.x - 6 + random.nextInt(8) - random.nextInt(8), pos.y - 16, Art.fxSteam12, 30));
      }
    } else {
      if (random.nextDouble() < 0.002f) {
        level.addEntity(
            new SmokeAnimation(
                pos.x - 6 + random.nextInt(8) - random.nextInt(8), pos.y - 16, Art.fxSteam12, 30));
      }
    }
    if (health == 0) {
      dropAllMoney();
    }
  }
  private synchronized void render(Graphics g) {
    if (level != null) {
      int xScroll = (int) (player.pos.x - screen.w / 2);
      int yScroll = (int) (player.pos.y - (screen.h - 24) / 2);
      soundPlayer.setListenerPosition((float) player.pos.x, (float) player.pos.y);
      level.render(screen, xScroll, yScroll);
    }
    if (!menuStack.isEmpty()) {
      menuStack.peek().render(screen);
    }

    boolean drawFPS = Options.get("drawFPS") != null && Options.get("drawFPS").equals("true");
    if (drawFPS) {
      Font.draw(screen, texts.FPS(fps), 10, 10);
    }

    if (player != null && menuStack.size() == 0) {
      Font.draw(screen, texts.health(player.health, player.maxHealth), 340, screen.h - 16);
      Font.draw(screen, texts.money(player.score), 340, screen.h - 27);
      Font.draw(screen, texts.nextLevel((int) player.getNextLevel()), 340, screen.h - 38);
      Font.draw(screen, texts.playerExp((int) player.pexp), 340, screen.h - 49);
      Font.draw(screen, texts.playerLevel(player.plevel), 340, screen.h - 60);
    }

    g.setColor(Color.BLACK);

    g.fillRect(0, 0, getWidth(), getHeight());
    g.translate((getWidth() - GAME_WIDTH * SCALE) / 2, (getHeight() - GAME_HEIGHT * SCALE) / 2);
    g.clipRect(0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE);

    if (!menuStack.isEmpty() || level != null) {

      // render mouse
      renderMouse(screen, mouseButtons);

      g.drawImage(screen.image, 0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE, null);
    }
  }
 public boolean fallDownHole() {
   int x = (int) (pos.x / Tile.WIDTH);
   int y = (int) (pos.y / Tile.HEIGHT);
   if (level.getTile(x, y) instanceof HoleTile) {
     if (!(this instanceof Player)) {
       ItemFallAnimation animation =
           new ItemFallAnimation(x * Tile.WIDTH, y * Tile.HEIGHT, this.getSprite());
       if (this instanceof Harvester) {
         animation.setHarvester();
       }
       level.addEntity(animation);
       remove();
     } else {
       GameCharacter character = ((Player) this).getCharacter();
       level.addEntity(new PlayerFallingAnimation(x * Tile.WIDTH, y * Tile.HEIGHT, character));
       if (((Player) this).isCarrying()) {
         ItemFallAnimation animation =
             new ItemFallAnimation(
                 x * Tile.WIDTH, (y - 1) * Tile.HEIGHT, ((Player) this).carrying.getSprite());
         if (((Player) this).carrying instanceof Harvester) {
           animation.setHarvester();
         }
         level.addEntity(animation);
         ((Player) this).carrying.remove();
       }
       // TODO add a sex attribute to Characters
       if (character.ordinal() < 2)
         MojamComponent.soundPlayer.playSound(
             "/sound/falling_male.wav", (float) pos.x, (float) pos.y);
       else
         MojamComponent.soundPlayer.playSound(
             "/sound/falling_female.wav", (float) pos.x, (float) pos.y);
     }
     return true;
   }
   return false;
 }
  private void tick() {

    if (packetLink != null) {
      packetLink.tick();
    }

    mouseButtons.setPosition(getMousePosition());
    if (!menuStack.isEmpty()) {
      menuStack.peek().tick(mouseButtons);
    }
    if (mouseMoved) {
      mouseMoved = false;
      mouseHideTime = 0;
      if (mouseHidden) {
        mouseHidden = false;
      }
    }
    if (mouseHideTime < 60) {
      mouseHideTime++;
      if (mouseHideTime == 60) {
        mouseHidden = true;
      }
    }
    mouseButtons.tick();

    if (level != null) {
      if (synchronizer.preTurn()) {
        synchronizer.postTurn();

        if (!paused) {
          for (int index = 0; index < keys.getAll().size(); index++) {
            Keys.Key key = keys.getAll().get(index);
            boolean nextState = key.nextState;
            if (key.isDown != nextState) {
              synchronizer.addCommand(new ChangeKeyCommand(index, nextState));
            }
          }

          keys.tick();
          for (Keys skeys : synchedKeys) {
            skeys.tick();
          }

          if (keys.pause.wasPressed()) {
            keys.release();
            synchronizer.addCommand(new PauseCommand(true));
          }

          if (keys.fullscreen.wasPressed()) {
            setFullscreen(!fullscreen);
          }

          // if mouse is in use, update player orientation before level tick
          if (!mouseHidden) {

            // update player mouse, in world pixels relative to
            // player
            player.setAimByMouse(
                ((mouseButtons.getX() / SCALE) - (screen.w / 2)),
                (((mouseButtons.getY() / SCALE) + 24) - (screen.h / 2)));
          } else {
            player.setAimByKeyboard();
          }

          level.tick();
        }

        // every 4 minutes, start new background music :)
        if (System.currentTimeMillis() / 1000 > nextMusicInterval) {
          nextMusicInterval = (System.currentTimeMillis() / 1000) + 4 * 60;
          soundPlayer.startBackgroundMusic();
        }

        if (keys.screenShot.isDown) {
          takeScreenShot();
        }
      }
    }

    if (createServerState == 1) {
      createServerState = 2;

      synchronizer = new TurnSynchronizer(MojamComponent.this, packetLink, localId, 2);

      clearMenus();
      createLevel(TitleMenu.level);

      synchronizer.setStarted(true);
      if (TitleMenu.level.vanilla) {
        packetLink.sendPacket(
            new StartGamePacket(
                TurnSynchronizer.synchedSeed,
                TitleMenu.level.getUniversalPath(),
                DifficultyList.getDifficultyID(TitleMenu.difficulty)));
      } else {
        packetLink.sendPacket(
            new StartGamePacketCustom(
                TurnSynchronizer.synchedSeed,
                level,
                DifficultyList.getDifficultyID(TitleMenu.difficulty)));
      }
      packetLink.setPacketListener(MojamComponent.this);
    }
  }
Example #10
0
 @Override
 public void handleCollision(Entity entity, double xa, double ya) {
   if (isTakeable && entity instanceof Player) {
     ((Player) entity).take(this);
   }
 }
  private void tick() {
    if (level != null) {
      if (level.player1Score >= Level.TARGET_SCORE) {
        addMenu(new WinMenu(GAME_WIDTH, GAME_HEIGHT, 1));
        level = null;
        return;
      }
      if (level.player2Score >= Level.TARGET_SCORE) {
        addMenu(new WinMenu(GAME_WIDTH, GAME_HEIGHT, 2));
        level = null;
        return;
      }
      if (keys.escape.wasPressed()) {
        clearMenus();
        addMenu(new TitleMenu(GAME_WIDTH, GAME_HEIGHT));
        level = null;
        return;
      }
    }
    if (packetLink != null) {
      packetLink.tick();
    }

    mouseButtons.setPosition(getMousePosition());
    if (!menuStack.isEmpty()) {
      menuStack.peek().tick(mouseButtons);
    }
    if (mouseMoved) {
      mouseMoved = false;
      mouseHideTime = 0;
      if (mouseHidden) {
        mouseHidden = false;
      }
    }
    if (mouseHideTime < 60) {
      mouseHideTime++;
      if (mouseHideTime == 60) {
        mouseHidden = true;
      }
    }
    mouseButtons.tick();

    if (level != null) {
      if (synchronizer.preTurn()) {
        synchronizer.postTurn();
        for (int index = 0; index < keys.getAll().size(); index++) {
          Keys.Key key = keys.getAll().get(index);
          boolean nextState = key.nextState;
          if (key.isDown != nextState) {
            synchronizer.addCommand(new ChangeKeyCommand(index, nextState));
          }
        }
        if (keys.save.isDown) {
          level.save();
        }
        keys.tick();
        for (Keys skeys : synchedKeys) {
          skeys.tick();
        }

        // if mouse is in use, update player orientation before level
        // tick
        if (!mouseHidden) {

          // update player mouse, in world pixels relative to player
          player.setAimByMouse(
              ((mouseButtons.getX() / SCALE) - (screen.w / 2)),
              (((mouseButtons.getY() / SCALE) + 24) - (screen.h / 2)));
        } else {
          player.setAimByKeyboard();
        }

        level.tick();
      }
    }

    if (createServerState == 1) {
      createServerState = 2;

      synchronizer = new TurnSynchronizer(MojamComponent.this, packetLink, localId, 2);

      clearMenus();
      createLevel(TitleMenu.level);

      synchronizer.setStarted(true);
      packetLink.sendPacket(new StartGamePacket(TurnSynchronizer.synchedSeed, TitleMenu.level));
      packetLink.setPacketListener(MojamComponent.this);
    }
  }