예제 #1
0
  int obtainSource(boolean isMusic) {
    if (noDevice) return 0;
    for (int i = 0, n = idleSources.size; i < n; i++) {
      int sourceId = idleSources.get(i);
      int state = alGetSourcei(sourceId, AL_SOURCE_STATE);
      if (state != AL_PLAYING && state != AL_PAUSED) {
        if (isMusic) {
          idleSources.removeIndex(i);
        } else {
          if (sourceToSoundId.containsKey(sourceId)) {
            long soundId = sourceToSoundId.get(sourceId);
            sourceToSoundId.remove(sourceId);
            soundIdToSource.remove(soundId);
          }

          long soundId = nextSoundId++;
          sourceToSoundId.put(sourceId, soundId);
          soundIdToSource.put(soundId, sourceId);
        }
        alSourceStop(sourceId);
        alSourcei(sourceId, AL_BUFFER, 0);
        AL10.alSourcef(sourceId, AL10.AL_GAIN, 1);
        AL10.alSourcef(sourceId, AL10.AL_PITCH, 1);
        AL10.alSource3f(sourceId, AL10.AL_POSITION, 0, 0, 1f);
        return sourceId;
      }
    }
    return -1;
  }
예제 #2
0
  @Test
  public void testVisibility() {
    reset();

    ModelEntity entity = new ModelEntity();
    Visibility visibility = new Visibility();
    visibility.setCondition("(eq $" + variableDef + " i1)");
    entity.getComponents().add(visibility);

    entitiesLoader.toEngineEntity(entity);
    IntMap<Entity> entityIntMap =
        gameLoop.getEntitiesFor(Family.getFamilyFor(VisibilityComponent.class));
    EngineEntity engineEntity = (EngineEntity) entityIntMap.entries().next().value;
    assertTrue(engineEntity.getGroup().isVisible());

    gameLoop.update(1);
    assertFalse(engineEntity.getGroup().isVisible());

    variablesManager.setVarToExpression(variableDef, "i1");
    gameLoop.update(1);
    assertTrue(engineEntity.getGroup().isVisible());

    variablesManager.setVarToExpression(variableDef, "i0");
    gameLoop.update(1);
    assertFalse(engineEntity.getGroup().isVisible());
  }
예제 #3
0
 void freeSource(int sourceID) {
   if (noDevice) return;
   alSourceStop(sourceID);
   alSourcei(sourceID, AL_BUFFER, 0);
   if (sourceToSoundId.containsKey(sourceID)) {
     long soundId = sourceToSoundId.remove(sourceID);
     soundIdToSource.remove(soundId);
   }
   idleSources.add(sourceID);
 }
  @Override
  public IntMap read(Json json, JsonValue jsonData, Class type) {
    IntMap intMap = new IntMap(json.readValue(VALUE_SIZE, int.class, jsonData));

    for (JsonValue entry = jsonData.getChild(VALUE_ENTRIES); entry != null; entry = entry.next) {
      intMap.put(Integer.parseInt(entry.name), json.readValue(entry.name, null, jsonData));
    }

    return intMap;
  }
예제 #5
0
 void stopSourcesWithBuffer(int bufferID) {
   if (noDevice) return;
   for (int i = 0, n = idleSources.size; i < n; i++) {
     int sourceID = idleSources.get(i);
     if (alGetSourcei(sourceID, AL_BUFFER) == bufferID) {
       if (sourceToSoundId.containsKey(sourceID)) {
         long soundId = sourceToSoundId.remove(sourceID);
         soundIdToSource.remove(soundId);
       }
       alSourceStop(sourceID);
     }
   }
 }
예제 #6
0
  public static void initialize(JSONArray shotDefs) throws JSONException {
    for (int i = 0; i < shotDefs.length(); i++) {
      JSONObject shotDef = shotDefs.getJSONObject(i);
      int type = shotDef.getInt("type");
      types.add(type);

      widths.put(type, (float) shotDef.getDouble("width"));
      heights.put(type, (float) shotDef.getDouble("height"));
      speeds.put(type, (float) shotDef.getDouble("speed"));
      damages.put(type, shotDef.getInt("damage"));
      textures.put(type, shotDef.getString("texture"));
      upgradeables.put(type, shotDef.getBoolean("upgradeable"));
    }
  }
예제 #7
0
 @Override
 public boolean isKeyPressed(int key) {
   synchronized (this) {
     if (key == Input.Keys.ANY_KEY) return keys.size > 0;
     else return keys.containsKey(key);
   }
 }
예제 #8
0
 @Override
 public void onGamepadDisconnected(int index) {
   GwtController controller = controllerMap.remove(index);
   if (controller != null) {
     synchronized (eventQueue) {
       GwtControllerEvent event = eventPool.obtain();
       event.type = GwtControllerEvent.DISCONNECTED;
       event.controller = controller;
       eventQueue.add(event);
     }
   }
 }
예제 #9
0
 @Override
 public void onGamepadConnected(int index) {
   Gamepad gamepad = Gamepad.getGamepad(index);
   GwtController controller = new GwtController(gamepad.getIndex(), gamepad.getId());
   controllerMap.put(index, controller);
   synchronized (eventQueue) {
     GwtControllerEvent event = eventPool.obtain();
     event.type = GwtControllerEvent.CONNECTED;
     event.controller = controller;
     eventQueue.add(event);
   }
 }
  @Override
  public void write(Json json, IntMap intMap, Class knownType) {
    json.writeObjectStart();
    json.writeValue(VALUE_SIZE, intMap.size);

    json.writeArrayStart(VALUE_ENTRIES);
    for (IntMap.Entry entry : (IntMap.Entries<?>) intMap.entries()) {
      json.writeValue(String.valueOf(entry.key), entry.value, null);
    }
    json.writeArrayEnd();

    json.writeObjectEnd();
  }
예제 #11
0
  public void init(float posX, float posY, int type, float angle) {
    this.type = type;

    float width = widths.get(type, 0.1f);
    float height = heights.get(type, 0.1f);

    bounds.set(posX - width / 2F, posY, width, height);
    position.set(posX - width / 2F, posY);
    velocity.set(0, speeds.get(type, 4f));
    velocity.setAngle(90f + angle);
    this.damage = damages.get(type, 2);
    alive = true;
    upgradeable = upgradeables.get(type);
  }
예제 #12
0
  @Override
  public void onGamepadUpdated(int index) {
    Gamepad gamepad = Gamepad.getGamepad(index);
    GwtController controller = controllerMap.get(index);
    if (gamepad != null && controller != null) {
      // Determine what changed
      JsArrayNumber axes = gamepad.getAxes();
      JsArrayNumber buttons = gamepad.getButtons();
      synchronized (eventQueue) {
        for (int i = 0, j = axes.length(); i < j; i++) {
          float oldAxis = controller.getAxis(i);
          float newAxis = (float) axes.get(i);
          if (oldAxis != newAxis) {
            GwtControllerEvent event = eventPool.obtain();
            event.type = GwtControllerEvent.AXIS;
            event.controller = controller;
            event.code = i;
            event.amount = newAxis;
            eventQueue.add(event);
          }
        }
        for (int i = 0, j = buttons.length(); i < j; i++) {
          float oldButton = controller.getButtonAmount(i);
          float newButton = (float) buttons.get(i);
          if (oldButton != newButton) {
            if ((oldButton < 0.5f && newButton < 0.5f)
                || (oldButton >= 0.5f && newButton >= 0.5f)) {
              controller.buttons.put(i, newButton);
              continue;
            }

            GwtControllerEvent event = eventPool.obtain();
            event.type =
                newButton >= 0.5f ? GwtControllerEvent.BUTTON_DOWN : GwtControllerEvent.BUTTON_UP;
            event.controller = controller;
            event.code = i;
            event.amount = newButton;
            eventQueue.add(event);
          }
        }
      }
    }
  }
예제 #13
0
  public void dispose() {
    if (noDevice) return;
    for (int i = 0, n = allSources.size; i < n; i++) {
      int sourceID = allSources.get(i);
      int state = alGetSourcei(sourceID, AL_SOURCE_STATE);
      if (state != AL_STOPPED) alSourceStop(sourceID);
      alDeleteSources(sourceID);
    }

    sourceToSoundId.clear();
    soundIdToSource.clear();

    AL.destroy();
    while (AL.isCreated()) {
      try {
        Thread.sleep(10);
      } catch (InterruptedException e) {
      }
    }
  }
예제 #14
0
  public void addSquad(final Entity squad, final int index) {
    final SquadCommandCard squadCard = new SquadCommandCard(squad, index, skin);
    Source source =
        new Source(squadCard) {

          @Override
          public Payload dragStart(InputEvent event, float x, float y, int pointer) {
            Payload payload = new Payload();

            // payload.setDragActor(new Label("Test", skin));
            SquadCommandCard card = new SquadCommandCard(squad, index, skin);
            payload.setDragActor(card);
            // payload.setDragActor(squadTable);
            return payload;
          }
        };

    dragAndDrop.addSource(source);

    cardSlots.get(index).setCard(squadCard);
  }
예제 #15
0
  public CommandCardContainer(final InputSystem inputSystem, final Skin skin, Stage stage) {
    this.skin = skin;
    this.inputSystem = inputSystem;

    dragAndDrop = new DragAndDrop();

    for (int i = 0; i < Constants.maxSquads; i++) {
      final CommandCardSlot slot = new CommandCardSlot(i, skin);
      cardSlots.put(i, slot);
      add(slot).size(slotWidth, slotHeight);

      Target target =
          new Target(slot) {

            @Override
            public void drop(Source source, Payload payload, float x, float y, int pointer) {
              CommandCardSlot slotA = slot;
              CommandCardSlot slotB = cardSlots.get((Integer) source.getActor().getUserObject());

              CommandCard cardA = (CommandCard) source.getActor();
              CommandCard cardB = (CommandCard) slotA.getUserObject();

              slotA.setCard(cardA);
              slotB.setCard(cardB);

              inputSystem.swapSquadSlot(slotA.index, slotB.index);
            }

            @Override
            public boolean drag(Source source, Payload payload, float x, float y, int pointer) {
              return true;
            }
          };

      dragAndDrop.addTarget(target);

      addSquad(null, i);
    }
  }
예제 #16
0
 /** @return iterator to tiles in this tileset */
 @Override
 public Iterator<TiledMapTile> iterator() {
   return tiles.values().iterator();
 }
예제 #17
0
 /**
  * Gets the {@link TiledMapTile} that has the given id.
  *
  * @param id the id of the {@link TiledMapTile} to retrieve.
  * @return tile matching id, null if it doesn't exist
  */
 public TiledMapTile getTile(int id) {
   return tiles.get(id);
 }
예제 #18
0
  @Override
  public void execute(Entity target, SetCamera effect) {
    // Get camera
    EngineEntity cameraEntity = gameView.getLayer(Layer.CAMERA);
    IntMap<Entity> entitiesWithCameras =
        gameLoop.getEntitiesFor(Family.getFamilyFor(CamerasComponent.class));
    EngineEntity sceneEntity;
    if (entitiesWithCameras == null
        || entitiesWithCameras.size == 0
        || (sceneEntity = (EngineEntity) entitiesWithCameras.values().next()) == null) {
      Gdx.app.log(LOG_TAG, "There are no cameras available. Effect will be skipped.");
      return;
    }

    if (!sceneEntity.hasComponent(CamerasComponent.class)) {
      Gdx.app.log(LOG_TAG, "No cameras in scene. Effect will be skipped.");
      return;
    }

    Camera camera =
        sceneEntity.getComponent(CamerasComponent.class).getCamera(effect.getCameraId());
    if (camera == null) {
      Gdx.app.log(
          LOG_TAG, "No camera with id " + effect.getCameraId() + " is av Effect will be skipped.");
      return;
    }

    // Get old values. Used for additional tweaking if the effect is
    // animated.
    float oldOriginX = cameraEntity.getGroup().getOriginX();
    float oldOriginY = cameraEntity.getGroup().getOriginY();
    float oldX = cameraEntity.getGroup().getX();
    float oldY = cameraEntity.getGroup().getY();
    float oldScaleX = cameraEntity.getGroup().getScaleX();
    float oldScaleY = cameraEntity.getGroup().getScaleY();
    float oldRotation = cameraEntity.getGroup().getRotation();

    // ///////////////////////////
    // Calculate new values
    // ///////////////////////////
    Float viewportWidth =
        (float) (Integer) variablesManager.getValue(VarsContext.RESERVED_VIEWPORT_WIDTH_VAR);
    Float viewportHeight =
        (float) (Integer) variablesManager.getValue(VarsContext.RESERVED_VIEWPORT_HEIGHT_VAR);

    // Camera width and height
    float w = camera.getWidth();
    float h = camera.getHeight();

    // Rotation
    float newRotation = camera.getRotation();

    // X and Y
    /*
     * After the matrix transformation is applied, we want that M * (x y 1)
     * = (0 0 1) The goal is to get the left-bottom vertex of the camera
     * aligned with the left-bottom vertex of the screen.
     */
    float x = camera.getX();
    float y = camera.getY();

    // Calculate the origin.
    /*
     * Must be the center of the camera in scene coordinates. There is no
     * need to adjust it as the origin translation is the first step in
     * Group.computeTransform()
     */
    float newOriginX = x + w / 2.0F;
    float newOriginY = y + h / 2.0F;

    // Calculate scale.
    /*
     * If the camera's width is smaller than the viewport, zoom-in will be
     * performed. Otherwise zoom out will apply.
     */
    float newScaleX = viewportWidth / w;
    float newScaleY = viewportHeight / h;

    /*
     * Calculate the x and y coordinates for the camera entity as to get
     * screen's bottom-left corner aligned with camera's bottom left corner.
     * It's a bit tricky since Group.computeTransform() translates to the
     * origin, then scales and "undoes" the origin translation, but since
     * the matrix is at that point scaled, not all the origin is removed.
     *
     * Calculus is described in class javadoc
     */
    float newX = -x * newScaleX - newOriginX * (1 - newScaleX);
    float newY = -y * newScaleY - newOriginY * (1 - newScaleY);

    // Check there's actually a transformation to apply:
    float t = 0.1F;
    if (MathUtils.isEqual(oldX, newX, t)
        && MathUtils.isEqual(oldY, newY, t)
        && MathUtils.isEqual(oldOriginX, newOriginX, t)
        && MathUtils.isEqual(oldOriginY, newOriginY, t)
        && MathUtils.isEqual(oldScaleX, newScaleX, t)
        && MathUtils.isEqual(oldScaleY, newScaleY, t)
        && MathUtils.isEqual(oldRotation, newRotation, t)) {
      Gdx.app.debug(LOG_TAG, "There's no transformation to apply. Effect skipped.");
      return;
    }

    // Set new origin
    cameraEntity.getGroup().setOrigin(newOriginX, newOriginY);

    // Instantaneous effect: just apply transformation
    if (effect.getAnimationTime() == 0F) {
      // Translate
      cameraEntity.getGroup().setX(newX);
      cameraEntity.getGroup().setY(newY);
      // Scale
      cameraEntity.getGroup().setScaleX(newScaleX);
      cameraEntity.getGroup().setScaleY(newScaleY);

      // Rotate
      cameraEntity.getGroup().setRotation(newRotation);
    } else {
      // Animated effect
      /*
       * New origin has been set but values are not actually transformed
       * until tweens start to run a loop later. This causes an
       * unintentional translational effect all of a sudden. To correct
       * that, x and y has to be adjusted so the entity does not actually
       * move
       */
      float cos = MathUtils.cosDeg(oldRotation);
      float sin = MathUtils.sinDeg(oldRotation);
      float adjX =
          oldScaleX * cos * (newOriginX - oldOriginX)
              + oldScaleY * sin * (oldOriginY - newOriginY)
              + oldOriginX
              - newOriginX;
      float adjY =
          oldScaleX * sin * (newOriginX - oldOriginX)
              + oldScaleY * cos * (newOriginY - oldOriginY)
              + oldOriginY
              - newOriginY;
      cameraEntity.getGroup().setX(oldX + adjX);
      cameraEntity.getGroup().setY(oldY + adjY);

      // Animated effect: create and add tweens
      TweensComponent tweensComponent =
          gameLoop.addAndGetComponent(cameraEntity, TweensComponent.class);
      RotateTween rotateTween = new RotateTween();
      rotateTween.setRotation(camera.getRotation());
      rotateTween.setDuration(effect.getAnimationTime());
      rotateTween.setRelative(false);
      ScaleTween scaleTween = new ScaleTween();
      scaleTween.setScaleX(newScaleX);
      scaleTween.setScaleY(newScaleY);
      scaleTween.setDuration(effect.getAnimationTime());
      MoveTween moveTween = new MoveTween();
      moveTween.setX(newX);
      moveTween.setY(newY);
      moveTween.setDuration(effect.getAnimationTime());
      tweensComponent.addTween(moveTween);
      tweensComponent.addTween(scaleTween);
      tweensComponent.addTween(rotateTween);
    }
  }
예제 #19
0
 public void updateSquadTable(int index) {
   SquadCommandCard card = (SquadCommandCard) cardSlots.get(index).getCard();
 }
예제 #20
0
 public long getSoundId(int sourceId) {
   if (!sourceToSoundId.containsKey(sourceId)) return -1;
   return sourceToSoundId.get(sourceId);
 }
예제 #21
0
 public Entity setSelected(int index, boolean selected) {
   SquadCommandCard card = (SquadCommandCard) cardSlots.get(index).getCard();
   card.setSelected(selected);
   return card.getSquad();
 }
예제 #22
0
 /**
  * Adds or replaces tile with that id
  *
  * @param id the id of the {@link TiledMapTile} to add or replace.
  * @param tile the {@link TiledMapTile} to add or replace.
  */
 public void putTile(int id, TiledMapTile tile) {
   tiles.put(id, tile);
 }
예제 #23
0
 /** @param id tile's id to be removed */
 public void removeTile(int id) {
   tiles.remove(id);
 }
예제 #24
0
 public void removeSquad(Entity squad, int index) {
   SquadCommandCard card = (SquadCommandCard) cardSlots.get(index).getCard();
   card.setSquad(null);
   card.setSelected(false);
 }
예제 #25
0
  @Override
  public boolean onKey(View v, int keyCode, android.view.KeyEvent e) {
    for (int i = 0, n = keyListeners.size(); i < n; i++)
      if (keyListeners.get(i).onKey(v, keyCode, e)) return true;

    synchronized (this) {
      KeyEvent event = null;

      if (e.getKeyCode() == android.view.KeyEvent.KEYCODE_UNKNOWN
          && e.getAction() == android.view.KeyEvent.ACTION_MULTIPLE) {
        String chars = e.getCharacters();
        for (int i = 0; i < chars.length(); i++) {
          event = usedKeyEvents.obtain();
          event.keyCode = 0;
          event.keyChar = chars.charAt(i);
          event.type = KeyEvent.KEY_TYPED;
          keyEvents.add(event);
        }
        return false;
      }

      char character = (char) e.getUnicodeChar();
      // Android doesn't report a unicode char for back space. hrm...
      if (keyCode == 67) character = '\b';

      switch (e.getAction()) {
        case android.view.KeyEvent.ACTION_DOWN:
          event = usedKeyEvents.obtain();
          event.keyChar = 0;
          event.keyCode = e.getKeyCode();
          event.type = KeyEvent.KEY_DOWN;

          // Xperia hack for circle key. gah...
          if (keyCode == android.view.KeyEvent.KEYCODE_BACK && e.isAltPressed()) {
            keyCode = Keys.BUTTON_CIRCLE;
            event.keyCode = keyCode;
          }

          keyEvents.add(event);
          keys.put(event.keyCode, null);
          break;
        case android.view.KeyEvent.ACTION_UP:
          event = usedKeyEvents.obtain();
          event.keyChar = 0;
          event.keyCode = e.getKeyCode();
          event.type = KeyEvent.KEY_UP;
          // Xperia hack for circle key. gah...
          if (keyCode == android.view.KeyEvent.KEYCODE_BACK && e.isAltPressed()) {
            keyCode = Keys.BUTTON_CIRCLE;
            event.keyCode = keyCode;
          }
          keyEvents.add(event);

          event = usedKeyEvents.obtain();
          event.keyChar = character;
          event.keyCode = 0;
          event.type = KeyEvent.KEY_TYPED;
          keyEvents.add(event);

          if (keyCode == Keys.BUTTON_CIRCLE) keys.remove(Keys.BUTTON_CIRCLE);
          else keys.remove(e.getKeyCode());
      }
      app.getGraphics().requestRendering();
    }

    // circle button on Xperia Play shouldn't need catchBack == true
    if (keyCode == Keys.BUTTON_CIRCLE) return true;
    if (catchBack && keyCode == android.view.KeyEvent.KEYCODE_BACK) return true;
    if (catchMenu && keyCode == android.view.KeyEvent.KEYCODE_MENU) return true;
    return false;
  }
예제 #26
0
 public void updateFormationPattern(int index, FormationPatternType pattern) {
   SquadCommandCard card = (SquadCommandCard) cardSlots.get(index).getCard();
   card.updateFormationPattern(pattern);
 }