Beispiel #1
0
  public void update(float delta) {
    // Check player side
    if (game.player.getCenterX() < enemyRec.x) {
      isPlayerLeft = true;
    } else if (game.player.getCenterX() > enemyRec.x + enemyRec.width) {
      isPlayerLeft = false;
    }

    if (!isFreeze) {
      // No target
      if (!isTarget) {
        // Horizontal
        if (isHorizontal) {
          if (isLeft) {
            enemyRec.x -= pol.getVertices()[2] / speed * delta;

            // Change Direction
            if (enemyRec.x - startPos.x <= 0) {
              isLeft = false;
            }

            // Player range
            if (isPlayerLeft) {
              if (game.player.isFlashLight()) {
                if (enemyRec.x - game.player.getCenterX() < normalRange * 1.5f) {
                  isTarget = true;
                  isLeft = true;
                }
              } else {
                if (enemyRec.x - game.player.getCenterX() < normalRange) {
                  isTarget = true;
                  isLeft = true;
                }
              }
            } else if (game.player.isFlashLight() || type == enemyBIG) {
              if (game.player.getCenterX() - (enemyRec.x + enemyRec.width) < normalRange / 2) {
                isTarget = true;
                isLeft = false;
              }
            }
          } else {
            enemyRec.x += pol.getVertices()[2] / speed * delta;

            // Change Direction
            if (enemyRec.x - startPos.x >= pol.getVertices()[2] - 32) {
              isLeft = true;
            }

            // Player range
            if (!isPlayerLeft) {
              if (game.player.isFlashLight()) {
                if (game.player.getCenterX() - (enemyRec.x + enemyRec.width) < normalRange * 1.2f) {
                  isTarget = true;
                  isLeft = false;
                }
              } else {
                if (game.player.getCenterX() - (enemyRec.x + enemyRec.width) < normalRange) {
                  isTarget = true;
                  isLeft = false;
                }
              }
            } else if (game.player.isFlashLight() || type == enemyBIG) {
              if (enemyRec.x - game.player.getCenterX() < normalRange / 2) {
                isTarget = true;
                isLeft = true;
              }
            }
          }
        }
        // Veritical
        /*else
        {
        	if (isUp)
        	{
        		enemyRec.y += pol.getVertices()[3]/speed*delta;
        		if (enemyRec.y-startPos.y >= pol.getVertices()[3]-64)
        		{
        			isUp = false;
        		}
        	}
        	else
        	{
        		enemyRec.y -= pol.getVertices()[3]/speed*delta;

        		if (enemyRec.y-startPos.y <= 0)
        		{
        			isUp = true;
        		}
        	}
        }*/
      } else // isTargeting
      {
        if (type == enemyBIG) {
          if (isPlayerLeft) isLeft = true;
          else isLeft = false;
        }
        if (isPlayerLeft) {
          if (game.player.isFlashLight()) {
            if (enemyRec.x - game.player.getCenterX() < targetRange * 1.5f) // Left
            {
              if (enemyRec.x <= game.player.getXWidth() - 1)
                enemyRec.x = game.player.getXWidth() - 1;
              else enemyRec.x -= pol.getVertices()[2] / speed * 2f * delta;
            } else {
              isTarget = false;
            }
          } else {
            if (enemyRec.x - game.player.getCenterX() < targetRange) // Left
            {
              if (enemyRec.x <= game.player.getXWidth() - 1)
                enemyRec.x = game.player.getXWidth() - 1;
              else enemyRec.x -= pol.getVertices()[2] / speed * 2f * delta;
            } else {
              isTarget = false;
            }
          }
        } else {
          if (game.player.isFlashLight()) {
            if (game.player.getCenterX() - (enemyRec.x + enemyRec.width) < targetRange * 1.5f) {
              if (enemyRec.x + enemyRec.width >= game.player.getX() + 1)
                enemyRec.x = game.player.getX() + 1 - enemyRec.width;
              else enemyRec.x += pol.getVertices()[2] / speed * 2f * delta;
            } else {
              isTarget = false;
            }
          } else {
            if (game.player.getCenterX() - (enemyRec.x + enemyRec.width) < targetRange) {
              if (enemyRec.x + enemyRec.width >= game.player.getX() + 1)
                enemyRec.x = game.player.getX() + 1 - enemyRec.width;
              else enemyRec.x += pol.getVertices()[2] / speed * 2f * delta;
            } else {
              isTarget = false;
            }
          }
        }
      }

      if (stateTime > 1000) stateTime = 0;

      stateTime += delta;
      soundController += delta;

      if (type == enemyNORMAL) {
        if (isLeft) {
          enemySprite.setRegion(assets.animationNormalEnemyLeft.getKeyFrame(stateTime, true));
        } else {
          enemySprite.setRegion(assets.animationNormalEnemyRight.getKeyFrame(stateTime, true));
        }

        enemyRec.setSize(enemySprite.getWidth() - 5, enemySprite.getHeight());
      } else {
        enemySprite.setRegion(assets.animationBigEnemy.getKeyFrame(stateTime, true));
        if (0.5f - soundController < 0) {
          game.game.audioManager.playSound(sfxWALKBIGENEMY);
          soundController = 0;
        }
        enemyRec.setSize(enemySprite.getWidth() - 10, enemySprite.getHeight());
        if (isLeft) {
          if (!enemySprite.isFlipX()) enemySprite.setFlip(true, false);
        } else {
          if (enemySprite.isFlipX()) enemySprite.setFlip(false, false);
        }
      }
    } else {
      freezeController += delta;
      if (freezeTime - freezeController <= 0) {
        freezeController = 0;
        isFreeze = false;
        game.game.audioManager.playSound(sfxICEBROKEN);
        enemySprite.setColor(Color.WHITE);
      }
    }
  }
 public void setSize(float width, float height) {
   bounds.setSize(width, height);
 }