Android Open Source - SpaceGame Asteroids






From Project

Back to project page SpaceGame.

License

The source code is released under:

Copyright (c) 2012 ??ukasz Patalas Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the So...

If you think the Android project SpaceGame listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.lpatalas.spacegame;
/*from   w  w w.  ja  va  2s . co m*/
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;

import java.util.ArrayList;
import java.util.List;

/**
 * User: Lukasz
 * Date: 11.08.12
 */
class Asteroids {
  private static final int INITIAL_ASTEROID_COUNT = 5;
  private static final float NEW_ASTEROID_INTERVAL = 5.0f;

  private final List<Asteroid> asteroids = new ArrayList<Asteroid>();
  private final Texture texture;
  private float timeToNextAsteroid = NEW_ASTEROID_INTERVAL;

  public Asteroids(Texture texture) {
    this.texture = texture;
  }

  public void reset() {
    asteroids.clear();

    for (int i = 0; i < INITIAL_ASTEROID_COUNT; i++) {
      addNewAsteroid();
    }

    timeToNextAsteroid = NEW_ASTEROID_INTERVAL;
  }

  public boolean collideWith(Rectangle testedBounds) {
    for (Asteroid asteroid : asteroids) {
      if (asteroid.collidesWith(testedBounds))
        return true;
    }

    return false;
  }

  public void update(float deltaTime) {
    updateAsteroidCount(deltaTime);

    for (Asteroid asteroid : asteroids) {
      asteroid.update(deltaTime);
    }
  }

  private void updateAsteroidCount(float deltaTime) {
    timeToNextAsteroid -= deltaTime;
    if (timeToNextAsteroid <= 0) {
      timeToNextAsteroid = Asteroids.NEW_ASTEROID_INTERVAL;
      addNewAsteroid();
    }
  }

  private void addNewAsteroid() {
    Asteroid asteroid = new Asteroid(texture);
    asteroids.add(asteroid);
  }

  public void render(SpriteBatch spriteBatch) {
    for (Asteroid asteroid : asteroids) {
      asteroid.render(spriteBatch);
    }
  }
}




Java Source Code List

com.lpatalas.spacegame.Assets.java
com.lpatalas.spacegame.Asteroid.java
com.lpatalas.spacegame.Asteroids.java
com.lpatalas.spacegame.CloudLayer.java
com.lpatalas.spacegame.Clouds.java
com.lpatalas.spacegame.DesktopStarter.java
com.lpatalas.spacegame.GameplayScreen.java
com.lpatalas.spacegame.MainMenuScreen.java
com.lpatalas.spacegame.Particles.java
com.lpatalas.spacegame.PlayerInputProcessor.java
com.lpatalas.spacegame.Player.java
com.lpatalas.spacegame.SpaceGameActivity.java
com.lpatalas.spacegame.SpaceGame.java
com.lpatalas.spacegame.Stars.java