/**
   * creates a GLFont for given awt Font consists of characters in given alphabet
   *
   * @param typeFace the awt font
   * @param alphabet characters of our alphabet
   */
  public GLFont(Paint paint, String alphabet) {
    this.alphabet = eliminateDuplicates(alphabet);
    this.charWidths = new int[alphabet.length()];

    Bitmap.Config config = Bitmap.Config.ARGB_8888;

    paint = new Paint(paint);
    paint.setColor(Color.WHITE);

    FontMetricsInt fontMetrics = paint.getFontMetricsInt();

    this.fontHeight = fontMetrics.leading - fontMetrics.ascent + fontMetrics.descent;
    this.baseline = -fontMetrics.top;
    int height = fontMetrics.bottom - fontMetrics.top;

    for (int i = 0; i < alphabet.length(); i++) {
      String c = alphabet.substring(i, i + 1);
      int width = (int) paint.measureText(c);

      Bitmap charImage = Bitmap.createBitmap(width, height, config);
      Canvas canvas = new Canvas(charImage);

      canvas.drawText(c, 0, baseline, paint);

      pack.addImage(charImage);
    }
    pack.pack(TexturePack.ALPHA_USE);
  }
  @SuppressWarnings("unchecked")
  public void onEnable() {
    super.onEnable(true);

    instance = this;

    this.packDir = new File(this.baseDirPath + File.separator + "pack_cache");

    if (!this.packDir.exists()) {
      this.packDir.mkdirs();
    }

    this.packsFile = new File(this.baseDirPath + File.separator + "texture_packs.bin");

    if (!this.packsFile.exists()) {
      this.packs = new HashMap<String, TexturePack>();
    } else {
      try {
        ObjectInputStream input = new ObjectInputStream(new FileInputStream(packsFile));

        this.packs = (HashMap<String, TexturePack>) input.readObject();

        input.close();
      } catch (Exception e) {
        this.log.warn("Failed to load texture packs from file");
        e.printStackTrace();
      }
    }

    this.selectedPacks =
        new DataStore(new File(this.baseDirPath + File.separator + "selected-packs.txt"), true);
    this.selectedPacks.load();

    for (TexturePack pack : this.packs.values()) {
      pack.fetchImage();
      pack.setMapRenderer();
    }

    if (this.pluginManager.isPluginEnabled("Vault")) {
      RegisteredServiceProvider<Economy> economyProvider =
          this.server.getServicesManager().getRegistration(Economy.class);

      if (economyProvider != null) {
        this.econ = economyProvider.getProvider();
      }
    }

    this.permissionManager.registerPermissions(Permission.class);
    this.commandManager.registerCommandExecutor(new TexturePackExecutor(this));
    this.pluginManager.registerEvents(new TexturePackListener(this), this);
  }
Example #3
0
  /** Constructor: Sets up this panel and loads the images. */
  public Engine() {
    addKeyListener(new Controller());

    setBackground(Color.black);
    setPreferredSize(new Dimension(WIDTH, HEIGHT));
    setFocusable(true);

    // init world
    final int defWidth = 150;
    final int defHeight = 15;
    final Random gen = new Random();
    // 50% random, 25% hills, 25% platform
    if (gen.nextBoolean()) {
      world = RandomLevel.genWorldRandom(defWidth, defHeight, tp);
    } else {
      if (gen.nextBoolean()) {
        world = RandomLevel.genWorldHills(defWidth, defHeight, tp);
      } else {
        world = RandomLevel.genWorldPlatform(defWidth, defHeight, tp);
      }
    }
    //        world = new Level("house.txt", tp);

    // init hero
    hero = new Hero();
    hero.setImage(tp.get(Texture.hero));
    hero.setPos(world.getStart());
    renderer.addDynProp(hero);
    renderer.setWorld(world);

    // init test light
    Point[] bgLights = world.getAll(Texture.bgLightDead);
    Light tempLight;
    final int cso = 15; // Cell size offset.
    final int rRange = 100;
    final int rMin = 75;

    for (Point light : bgLights) {
      tempLight = new Light(light.x + cso, light.y + cso, world);
      tempLight.setRadius((short) (Math.random() * rRange + rMin));
      renderer.addStaticProp(tempLight);
      triggers.add(tempLight);
    }

    // Don't start this loop until setup is complete!
    if (thread == null) {
      running = true;
      thread = new Thread(this);
      thread.start();
    }
  }
  /**
   * blits given string to frame buffer. works very similar to awt.Graphics#drawString(..) that is:
   * x coordinate is left most point in string, y is baseline
   *
   * @param buffer buffer to blit into
   * @param s string to blit
   * @param x leftmost point
   * @param transparency transparency value, make sure >= 0
   * @param color text color
   * @param y baseline
   */
  public void blitString(
      FrameBuffer buffer, String s, int x, int y, int transparency, RGBColor color) {
    y -= baseline;

    for (int i = 0; i < s.length(); i++) {
      char c = s.charAt(i);
      int index = alphabet.indexOf(c);
      if (index == -1) index = alphabet.indexOf('?');
      if (index != -1) {
        Point size = pack.blit(buffer, index, x, y, transparency, false, color);
        x += size.x;
      }
    }
  }
Example #5
0
  /**
   * Calculate the physics in our world for the given object.
   *
   * @param obj The object to move
   * @param timeStep The time since last evaluation in nanoseconds
   */
  public void simulate(Dynamic obj, long timeStep) {
    /* Actions:
     * 1. apply drag
     * 2. add forces to object
     *     - gravity (unless resting on block)
     *     - arrow keys
     *
     * 3. apply forces to object. (done by the object)
     *
     * 4. a) move in x axis
     * 5. a) resolve collisions in x axis
     *
     * 4. b) move in y axis
     * 5. b) resolve collisions in y axis
     */
    double seconds = timeStep / (double) NS_PER_S;

    // 1. apply drag
    obj.applyDrag(seconds);

    // 2. add forces
    obj.addForce(new Vector2D(0, GRAVITY));
    obj.addForce(keyForce);

    // 3. apply forces
    obj.applyForces(seconds);

    // get velocity in preparation for step 4
    Vector2D vel = obj.getVel();
    hero.setImage(tp.get(Texture.hero));

    // 4. a)  move x
    obj.move((int) (vel.x * seconds), 0);

    // 5. a) resolve collisions in x
    Point bad = getWorldCollision(obj, Texture.brick);
    //        bad = null;
    if (bad != null) {
      int resolution = world.escapeX(obj, vel.x * seconds, bad);

      obj.move(resolution, 0);
      obj.getVel().setX(0);
    }

    // 4. b)  move y
    obj.move(0, (int) (vel.y * seconds));

    // 5. b) resolve collisions in y
    bad = getWorldCollision(obj, Texture.brick);
    //        bad = null;
    if (bad != null) {
      int resolution = world.escapeY(obj, vel.y * seconds, bad);
      if (vel.y < 0) {
        hero.setOnGround(true);
        hero.setImage(tp.get(Texture.heroGround));
      }
      obj.move(0, resolution);
      obj.getVel().setY(0);
    } else {
      hero.setOnGround(false);
    }
  }