Esempio n. 1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    dbhelper = new DatabaseHelper(getApplicationContext());
    setContentView(R.layout.activity_main);

    beaconManager = new BeaconManager(getApplicationContext());
    beaconManager.connect(
        new BeaconManager.ServiceReadyCallback() {
          @Override
          public void onServiceReady() {
            beaconManager.startMonitoring(new Region("monitored region", null, null, null));
          }
        });

    dbhelper.addUser(new UserBean(1, "Player", 0));

    user = dbhelper.getUserByID(1);

    Log.i(APP_NAME, user.getUsername() + ", " + user.getLevel() + ", " + user.getId());

    Button run = (Button) findViewById(R.id.myRunButton);
    run.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            runaway();
          }
        });

    // set up listeners
    beaconManager.setMonitoringListener(
        new BeaconManager.MonitoringListener() {
          @Override
          public void onEnteredRegion(Region region, List<Beacon> list) {
            try {
              new HttpGetJsonTask()
                  .execute(
                      new URL("http://192.168.224.130:5000/monsters?userlevel=" + user.getLevel()));

            } catch (Exception e) {

            }
            showNotification(
                "You have entered a battle",
                "You have entered a battle with a monster click to fight and check it out.");
          }

          @Override
          public void onExitedRegion(Region region) {
            Toast.makeText(getApplicationContext(), "Exited a beacon", Toast.LENGTH_SHORT).show();
          }
        });
    // end of listeners
  }
Esempio n. 2
0
  public void runBattle() {

    if (fightingMonster != null) {

      // load monster on screen
      int counter = 0;
      while (fightingMonster.getImage() == null && counter != 1000) {}

      final ProgressBar progBar = (ProgressBar) findViewById(R.id.progressBar);
      progBar.setMax(fightingMonster.getHealth());
      progBar.setProgress(fightingMonster.getHealth());
      ImageView imgV = (ImageView) findViewById(R.id.myImageView);

      final ProgressBar userHealthBar = (ProgressBar) findViewById(R.id.userHealthBar);
      user.setHealth(12 * user.getLevel());
      userHealthBar.setMax(user.getHealth());
      userHealthBar.setProgress(user.getHealth());
      Random rndNumGen = new Random();
      // Generate 2 numbers (one for each combatant) who ever gets higher wins
      int playerScore = rndNumGen.nextInt(10);
      int monsterScore = rndNumGen.nextInt(9);
      imgV.setImageBitmap(fightingMonster.getImage());
      imgV.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              Random rndNumGen = new Random();
              // Generate 2 numbers (one for each combatant) who ever gets higher wins
              int playerScore = rndNumGen.nextInt(10);
              int monsterScore = rndNumGen.nextInt(9);
              if (playerScore >= monsterScore) {
                fightingMonster.hit();
                userHealthBar.setProgress(user.getHealth());
              } else if (monsterScore >= playerScore) {
                user.hit();
                progBar.setProgress(fightingMonster.getHealth());
              }
              if (fightingMonster.getHealth() == 0) {
                Toast.makeText(MainActivity.this, "Health 0", Toast.LENGTH_LONG).show();
                user.increaseLevel();
                dbhelper.updateUser(user);

                loadMainPage(
                    "Congratulations you defeated "
                        + fightingMonster.getName()
                        + " your new level is "
                        + user.getLevel());
              }
            }
          });
    }
  }