Source Code

This commit is contained in:
Edward
2014-07-06 15:07:28 -07:00
parent 117836dff7
commit 4077f5894d
33 changed files with 2695 additions and 0 deletions
+14
View File
@@ -0,0 +1,14 @@
package com.spaceshooter;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public abstract class Bullet extends Sprite {
public Bullet(String filepath) {
super(ResourceManager.getAssetManager().get(filepath,Texture.class));
}
public Bullet() {
super(ResourceManager.getAssetManager().get(ResourceManager.Bullet,Texture.class));
}
abstract void updateBullet(SpriteBatch batch, EnemyManager enemyManager);
}
+127
View File
@@ -0,0 +1,127 @@
package com.spaceshooter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.MathUtils;
public class BulletManager {
private List<Bullet> bulletList;
private Explosion explosion = null;
float blowX = 0.0f, blowY = 0.0f;
float blowTimer = 0.0f;
private boolean collision = false;
BulletManager() {
bulletList = new LinkedList<Bullet>();
}
public void update(EnemyManager enemyManager, SpriteBatch batch,
ScoreHandler scorehandler, Player player,PickupManager pickupManager) {
// Where 'b' = bullet && 'e' = enemy
if (player != null) {
ListIterator<Bullet> bulletIter = bulletList.listIterator();
while (bulletIter.hasNext()) {
Bullet b = bulletIter.next();
b.updateBullet(batch,enemyManager);
ListIterator<Enemy> enemyIter = enemyManager.getList().listIterator();
while (enemyIter.hasNext()) {
Enemy e = enemyIter.next();
if (Intersector.overlaps(b.getBoundingRectangle(), e.getBoundingRectangle()) == true
&& b instanceof PlayerBullet) {
e.hit(player.getWeaponLevel());
if (e.isDead) {
blowX = b.getX();
blowY = b.getY();
explosion = new Explosion(blowX, blowY);
int rand = MathUtils.random(8);
if (rand == 0) {
/*pickupManager.getList().add(new CoinPickup(e.getX(),
e.getY()));*/
scorehandler.addScore(1000);
}
else if (rand == 1) {
pickupManager.getList().add(new WeaponPickup(e.getX(),
e.getY()));
}
else if (rand == 2) {
pickupManager.getList().add(new MissilePickup(e.getX(),
e.getY()));
}
enemyIter.remove();
}
collision = true; // tells that the bullet has collided w/
// enemy
if (e != null && !e.isInvincible)
scorehandler.addScore(10); // Add 10pts to score.
/*
* In the future, check for enemy types and add a higher or
* lower score Depending on the enemy
*/
}
else if (Intersector.overlaps(b
.getBoundingRectangle(), player.getBoundingRectangle()) == true
&& (!(b instanceof PlayerBullet))) {
if (!(player.isBlinking || collision)) { //Why must I check the collision boolean?
player.killPlayer();
collision = true;
}
/*
* In the future, check for enemy types and add a higher or
* lower score Depending on the enemy
*/
}
}
if (b.getY() > SpaceShooter.getTopBound() || b.getY() < SpaceShooter.getBottomBound()) {
bulletIter.remove();
}
else if (collision) {
bulletIter.remove();
collision = false; // follows up and removes the bullet, then
// resets boolean.
}
}
}
else {
ListIterator<Bullet> bulletIter = bulletList.listIterator();
while (bulletIter.hasNext()) {
Bullet b = bulletIter.next();
b.updateBullet(batch,enemyManager);
}
}
}
public List<Bullet> getList() {
return bulletList;
}
public void draw(SpriteBatch batch) {
// Looping through the Linked List with O(n).
for (Bullet b : bulletList) {
if (b != null && (!(b instanceof PlayerBullet))) //Don't draw the actual hitbox sprite
b.draw(batch);
}
if (explosion != null)
explosion.draw(batch);
}
public void clear() {
bulletList.clear();
}
public void dispose() {
bulletList.clear();
}
}
+46
View File
@@ -0,0 +1,46 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class CoinPickup extends Pickup {
private static final int FRAME_COLS = 6;
private static final int FRAME_ROWS = 1;
private Animation pickupAnimation;
private TextureRegion[] pickupFrames;
private TextureRegion currentFrame;
float stateTime;
public CoinPickup(float xPos,float yPos) {
super(ResourceManager.getAssetManager().get(ResourceManager.Coin,Texture.class));
setPosition(xPos,yPos);
TextureRegion[][] tmp = TextureRegion.split(getTexture(),
getTexture().getWidth() / FRAME_COLS, getTexture().getHeight()
/ FRAME_ROWS);
pickupFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
pickupFrames[index++] = tmp[i][j];
}
}
pickupAnimation = new Animation(0.07f, pickupFrames);
stateTime = 0f;
}
public void update() {
translateY(-100*Gdx.graphics.getDeltaTime());
currentFrame = pickupAnimation.getKeyFrame(stateTime,true);
}
public void draw(SpriteBatch batch) {
stateTime += Gdx.graphics.getDeltaTime();
batch.draw(currentFrame, getX(), getY());
}
}
+155
View File
@@ -0,0 +1,155 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Enemy extends Sprite {
protected boolean gotHit;
protected int health;
public boolean isDead;
private float redTimer;
protected boolean isInvincible;
Enemy() {
super(ResourceManager.getAssetManager().get(ResourceManager.Enemy,
Texture.class));
health = 1;
redTimer = 0f;
isDead = false;
isInvincible = false;
}
Enemy(int health) {
super(ResourceManager.getAssetManager().get(ResourceManager.Enemy,
Texture.class));
this.health = health;
redTimer = 0f;
isDead = false;
isInvincible = false;
}
Enemy(String filePath) {
super(ResourceManager.getAssetManager().get(filePath, Texture.class));
}
Enemy(int health, String filePath) {
super(ResourceManager.getAssetManager().get(filePath, Texture.class));
this.health = health;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
setY(getY() - 150 * Gdx.graphics.getDeltaTime());
}
public void processHits() {
if (gotHit) {
redTimer += (Gdx.graphics.getDeltaTime() / 5);
if (redTimer > 0.005f) {
setColor(Color.WHITE);
redTimer = 0f;
gotHit = false;
}
}
}
public void hit(int damage) {
gotHit = true;
if (!isInvincible) {
setColor(Color.RED);
health -= damage;
if (health <= 0)
isDead = true;
}
else
setColor(Color.BLUE);
}
//hit() defaults to -1 instead of - damage
public void hit() {
gotHit = true;
if (!isInvincible) {
setColor(Color.RED);
health--;
if (health <= 0)
isDead = true;
}
else
setColor(Color.BLUE);
}
public boolean isInvincible() {
return isInvincible;
}
public float getDistance(Sprite other) {
float dist = (float) Math.sqrt(Math.pow(
getX() - other.getX(), 2)
+ Math.pow(getY() - other.getY(), 2));
return dist;
}
public void moveTowards(double Speed, Sprite other) {
float directionX = getX() - other.getX();
float directionY = getY() - other.getY();
double sq = Math
.sqrt(directionX * directionX + directionY * directionY);
float velocityX = (float) (directionX
* (Speed * Gdx.graphics.getDeltaTime()) / sq);
float velocityY = (float) (directionY
* (Speed * Gdx.graphics.getDeltaTime()) / sq);
translate(-velocityX, -velocityY);
}
public void moveTowardsX(double Speed, Sprite other) {
float directionX = getX() - other.getX();
float directionY = getY() - other.getY();
double sq = Math
.sqrt(directionX * directionX + directionY * directionY);
float velocityX = (float) (directionX
* (Speed * Gdx.graphics.getDeltaTime()) / sq);
translateX(-velocityX);
}
public void rotateTowards(Sprite other) {
float angle = (float) Math.atan2(getY() - other.getY(),
getX() - other.getX());
float degrees = (float) (angle * (180 / Math.PI));
setRotation(degrees - 90);
}
}
/*
* Potentially useful health bar code: private BitmapFont m_Font; private
* BitmapFont m_Font; private String m_String; private Sprite healthBar; private
* Sprite healthBarBk;
*
* healthBar = new Sprite(new Texture("media/healthBar.png"));
* healthBar.setOrigin(getTexture().getWidth()/2,getTexture().getHeight()/2);
* healthBar.setRotation(90); //magic numbers...
* healthBar.setBounds((Gdx.graphics.getWidth() / 3f) +
* 50f,(Gdx.graphics.getHeight() / 3) - 400,m_Energy*3,8);
*
* healthBar.setColor(getColor().r,getColor().g,getColor().b,125);
*
* healthBarBk = new Sprite(new Texture("media/healthBar.png"));
* healthBarBk.setOrigin(getTexture().getWidth()/2,getTexture().getHeight()/2);
* healthBarBk.setRotation(90); //magic numbers...
* healthBarBk.setBounds((Gdx.graphics.getWidth() / 3f) +
* 50f,(Gdx.graphics.getHeight() / 3) - 400,300,8);
* healthBarBk.setColor(Color.RED);
*
* if (gotHit == true) { blinkTimer = 0.50f; gotHit = false;
* healthBar.setBounds((Gdx.graphics.getWidth() / 3f) +
* 50f,(Gdx.graphics.getHeight() / 3) - 400,m_Energy*3,8); isBlinking = true; //
* must blink now bonusScore = 0; powerupTimer = 30f; }
*
* m_Font.draw(batch,m_String,Gdx.graphics.getWidth() / 3 -
* 50,(Gdx.graphics.getHeight() / 3) + 50); healthBarBk.draw(batch);
* healthBar.draw(batch);
*/
+25
View File
@@ -0,0 +1,25 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class EnemyBulletBasic extends Bullet {
float m_velocityX;
float m_velocityY;
float m_rotation;
public EnemyBulletBasic(float xPos,float yPos,float velocityX, float velocityY, float rotation) {
super();
m_velocityX = velocityX;
m_velocityY = velocityY;
m_rotation = rotation;
setRotation(-m_rotation);
setPosition(xPos,yPos);
}
public void updateBullet(SpriteBatch batch,EnemyManager enemyManager) {
setOrigin(getTexture().getWidth() / 2, getTexture().getHeight() / 2);
translate(-m_velocityX * Gdx.graphics.getDeltaTime(), -m_velocityY
* Gdx.graphics.getDeltaTime());
}
}
+30
View File
@@ -0,0 +1,30 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
public class EnemyCargo extends Enemy {
//Cargo enemies shoot two missiles once they pass the player.
private boolean hasShot;
private EnemyManager enemyManager;
public EnemyCargo(EnemyManager enemyManager)
{
super(100,ResourceManager.EnemyCargo1);
hasShot = false;
this.enemyManager = enemyManager;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
setY(getY() - 400 * Gdx.graphics.getDeltaTime());
if (player != null) {
if (getY() < player.getY() && !hasShot) {
enemyManager.addEnemy(new EnemyMissile(),getX(),getY());
enemyManager.addEnemy(new EnemyMissile(),getX() + getWidth(),getY());
hasShot = true;
}
}
}
}
+78
View File
@@ -0,0 +1,78 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
public class EnemyEye extends Enemy {
private float bulletTimer;
private final int HOMING_SPEED;
private boolean isAngry;
EnemyEye() {
super(50, ResourceManager.EnemyEye);
setOrigin(getWidth() / 2, getHeight() / 2);
bulletTimer = 0f;
isAngry = false;
HOMING_SPEED = 150;
setOrigin(getWidth() / 2, getHeight() / 2);
}
public void updateEnemy(Player player, BulletManager bulletManager) {
if (player != null) {
float angle = (float) Math.atan2(getY() - player.getY(), getX()
- player.getX());
float degrees = (float) (angle * (180 / Math.PI));
float directionX = getX() - player.getX();
float directionY = getY() - player.getY();
double sq = Math.sqrt(directionX * directionX + directionY
* directionY);
float velocityX = (float) (directionX
* (HOMING_SPEED * Gdx.graphics.getDeltaTime()) / sq);
float velocityY = (float) (directionY
* (HOMING_SPEED * Gdx.graphics.getDeltaTime()) / sq);
float playerDist = (float) Math.sqrt(Math.pow(
getX() - player.getX(), 2)
+ Math.pow(getY() - player.getY(), 2));
if (isAngry == false) {
translate(-velocityX, -velocityY);
setRotation(degrees - 90);
if (playerDist < 200 && !isAngry) {
isAngry = true;
}
} else if (isAngry == true) {
if (getX() > player.getX())
rotate(-Gdx.graphics.getDeltaTime() * 100);
else
rotate(Gdx.graphics.getDeltaTime() * 100);
bulletTimer += (Gdx.graphics.getDeltaTime() / 5);
if (bulletTimer > 0.03f) {
bulletManager
.getList()
.add(new EnemyBulletBasic(
getX() + getWidth() / 2,
getY() + getHeight() / 2,
(-1f * SpaceShooter.getBulletSpeed())
* (float) Math.cos(Math
.toRadians(getRotation() - 90)),
(-1f * SpaceShooter.getBulletSpeed())
* (float) Math.sin(Math
.toRadians(getRotation() - 90)),
0));
bulletTimer = 0;
}
}
this.setY(this.getY() - 10 * Gdx.graphics.getDeltaTime());
}
else
this.setY(this.getY() - 150 * Gdx.graphics.getDeltaTime());
}
}
+61
View File
@@ -0,0 +1,61 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.MathUtils;
public class EnemyFactory extends Enemy {
private float spawnTimer;
private EnemyManager enemyManager;
private boolean hasChanged; //Returns true if the texture has changed.
private int type;
public EnemyFactory(EnemyManager enemyManager) {
super(50,ResourceManager.EnemyFactory);
spawnTimer = 0.0f;
hasChanged = false;
this.enemyManager = enemyManager;
type = MathUtils.random(0,2);
}
public void updateEnemy(Player player, BulletManager bulletManager) {
spawnTimer += (Gdx.graphics.getDeltaTime() / 5);
setY(getY() - 50 * Gdx.graphics.getDeltaTime());
if (player != null) {
float playerDist = (float) Math.sqrt(Math.pow(
getX() - player.getX(), 2)
+ Math.pow(getY() - player.getY(), 2));
if (type == 0) {
if ((spawnTimer > 0.2f) && (playerDist < 600) && (getY() > player.getY())) {
enemyManager.addEnemy(new EnemyHoming(),getX() + (getWidth()/2) - 15,getY());
spawnTimer = 0f;
}
}
else if (type == 1) {
if ((spawnTimer > 0.5f) && (playerDist < 600) && (getY() > player.getY())) {
enemyManager.addEnemy(new EnemySpinner(),getX() + (getWidth()/2) - 15,getY());
spawnTimer = 0f;
}
}
else if (type == 2) {
if ((spawnTimer > 0.2f) && (playerDist < 600) && (getY() > player.getY())) {
enemyManager.addEnemy(new EnemyPopcorn(),getX() + (getWidth()/2 - 15),getY());
spawnTimer = 0f;
}
}
}
if (health < 15 && !hasChanged) {
setTexture(ResourceManager.getAssetManager().get(ResourceManager.EnemyFactoryDestroyed,Texture.class));
hasChanged = true;
}
}
}
+50
View File
@@ -0,0 +1,50 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
public class EnemyHoming extends Enemy {
private final int HOMING_SPEED;
private float velocityX;
private float velocityY;
EnemyHoming() {
super(3);
HOMING_SPEED = 200;
velocityX = 0.0f;
velocityY = 0.0f;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
if (player != null) {
// Point the enemy towards the player.
float angle = (float) Math.atan2(getY() - player.getY(), getX()
- player.getX());
float degrees = (float) (angle * (180 / Math.PI));
setRotation(degrees - 90);
/*
* translate(100 * Gdx.graphics.getDeltaTime() * (float)
* Math.cos(degrees - 90), 100 * Gdx.graphics.getDeltaTime() * (float)
* Math.sin(degrees - 90));
*/
// this.setY(this.getY() - 150 * Gdx.graphics.getDeltaTime());
// Move the enemy towards the player. Clean this up *
float directionX = getX() - player.getX();
float directionY = getY() - player.getY();
double sq = Math
.sqrt(directionX * directionX + directionY * directionY);
velocityX = (float) (directionX
* (HOMING_SPEED * Gdx.graphics.getDeltaTime()) / sq);
velocityY = (float) (directionY
* (HOMING_SPEED * Gdx.graphics.getDeltaTime()) / sq);
translate(-velocityX, -velocityY);
}
else {
translate(-velocityX,-velocityY);
}
}
}
+86
View File
@@ -0,0 +1,86 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
public class EnemyKiller1 extends Enemy {
private float bulletTimer;
private float angryTimer;
private Texture angryTexture;
private Texture happyTexture;
private boolean isAngry;
private float angle;
private boolean wave;
EnemyKiller1() {
super(50, ResourceManager.EnemyKiller1);
angryTexture = ResourceManager.getAssetManager().get(
ResourceManager.EnemyKiller1b, Texture.class);
happyTexture = ResourceManager.getAssetManager().get(
ResourceManager.EnemyKiller1, Texture.class);
bulletTimer = 0f;
angryTimer = 0f;
isAngry = false;
angle = 0.0f;
this.wave = false;
}
EnemyKiller1(boolean wave, float startAngle) {
super(50, ResourceManager.EnemyKiller1);
angryTexture = ResourceManager.getAssetManager().get(
ResourceManager.EnemyKiller1b, Texture.class);
happyTexture = ResourceManager.getAssetManager().get(
ResourceManager.EnemyKiller1, Texture.class);
bulletTimer = 0f;
angryTimer = 0f;
isAngry = false;
angle = startAngle;
this.wave = wave;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
angle += Gdx.graphics.getDeltaTime() * 100f;
angryTimer += (Gdx.graphics.getDeltaTime() / 5);
if (isAngry == false) {
if (angryTimer > 0.5f) {
setTexture(angryTexture);
isAngry = true;
}
} else if (isAngry == true) {
bulletTimer += (Gdx.graphics.getDeltaTime() / 5);
if (bulletTimer > 0.06f) {
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), 0,SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), 0,SpaceShooter.getBulletSpeed(), 0));
bulletTimer = 0;
}
if (angryTimer > 1.0f) {
isAngry = false;
setTexture(happyTexture);
angryTimer = 0f;
}
}
if (player != null) {
if (wave) {
setPosition(getX() + 4f * (float) Math.sin(Math.toRadians(angle)),
getY() - 75 * Gdx.graphics.getDeltaTime());
}
else {
setY(getY() - 75 * Gdx.graphics.getDeltaTime());
}
}
else {
if (wave) {
setPosition(getX() + 4f * (float) Math.sin(Math.toRadians(angle)),
getY() - 150 * Gdx.graphics.getDeltaTime());
}
else {
setY(getY() - 200 * Gdx.graphics.getDeltaTime());
}
}
}
}
+187
View File
@@ -0,0 +1,187 @@
package com.spaceshooter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.MathUtils;
public class EnemyManager {
public List<Enemy> enemyList;
private List<Enemy> enemyQueue; /*holds references to enemies that need to be added.
since you cannot add while iterating through a list */
private float enemyTimer;
private boolean collision = false;
EnemyManager() {
enemyList = new LinkedList<Enemy>();
enemyQueue = new LinkedList<Enemy>();
enemyTimer = 0.0f;
}
public void update(Player player, ScoreHandler scorehandler,
BulletManager bulletManager) {
enemyTimer += Gdx.graphics.getDeltaTime() / 5;
createEnemy(MathUtils.random(SpaceShooter.getLeftBound()/2,SpaceShooter.getRightBound()/2),
SpaceShooter.getTopBound() + 100);
ListIterator<Enemy> enemyIter = enemyList.listIterator();
while (enemyIter.hasNext()) {
Enemy e = enemyIter.next();
e.updateEnemy(player, bulletManager);
if (player != null) {
if (Intersector.overlaps(e.getBoundingRectangle(),
player.getBoundingRectangle()) == true) {
if (!player.isBlinking) {
/*
* In the future, check for enemy types and add a higher or
* lower score Depending on the enemy
*/
player.killPlayer();
collision = true;
}
}
}
if ((e.getY() < SpaceShooter.getBottomBound() - e.getHeight()) ||
(e.getY() > SpaceShooter.getTopBound() + e.getHeight() + 500) ||
(e.getX() < SpaceShooter.getLeftBound() - e.getWidth() - 100) ||
(e.getX() > SpaceShooter.getRightBound() + e.getWidth() + 100) )
enemyIter.remove();
else if (collision) {
e.hit();
if (e.isDead) {
enemyIter.remove();
}
collision = false;
}
}
//Add enemies that are part of the queue now that we are not iterating.
for (Enemy e : enemyQueue) {
if (e != null)
enemyList.add(e);
}
//Clear the queue:
enemyQueue.clear();
}
public void addEnemy(Enemy enemy,float x,float y) {
enemy.setPosition(x, y);
enemyQueue.add(enemy);
}
public List<Enemy> getList() {
return enemyList;
}
public void createEnemy(float x, float y) {
if (enemyTimer > 0.7f) {
int rand = MathUtils.random(11);
rand = 0;
if (rand == 0) {
//Enemy enemy = new EnemyTurret();
EnemyMissileShip enemy = new EnemyMissileShip(this);
enemy.setPosition(x, y);
enemyList.add(enemy);
enemyTimer = 0.0f;
} else if (rand == 1) {
EnemyPatterns.blockade(this, x, y,"EnemySpinner",5,0);
enemyTimer = 0.0f;
}
else if (rand == 2) {
EnemyPatterns.blockade(this, x, y,"EnemyPopcorn",5,2);
enemyTimer = 0.0f;
}
else if (rand == 3) {
EnemyPatterns.sineWave(this, x, y,15,"EnemyPopcorn");
enemyTimer = 0.0f;
}
else if (rand == 4) {
//EnemyPatterns.sineWave(this, x, y,2,"EnemyKiller1");
EnemyPatterns.blockade(this, x, y,"EnemyKiller1",3,0);
enemyTimer = 0.0f;
}
else if (rand == 5) {
EnemyPatterns.sineWave(this, x, y,2,"EnemyKiller1");
enemyTimer = 0.0f;
}
else if (rand == 6) {
EnemyRedship enemy = new EnemyRedship();
enemy.setPosition(x, y);
enemyList.add(enemy);
enemyTimer = 0.0f;
}
else if (rand == 7) {
EnemyEye enemy = new EnemyEye();
enemy.setPosition(x, y);
enemyList.add(enemy);
enemyTimer = 0.0f;
}
else if (rand == 8) {
EnemyCargo enemy = new EnemyCargo(this);
enemy.setPosition(x, y);
enemyList.add(enemy);
enemyTimer = 0.0f;
}
else if (rand == 9) {
EnemyFactory enemy = new EnemyFactory(this);
enemy.setPosition(x, y);
enemyList.add(enemy);
EnemyFactory enemy2 = new EnemyFactory(this);
enemy2.setPosition(x + enemy2.getWidth() + 20, y);
enemyList.add(enemy2);
enemyTimer = 0.0f;
}
else if (rand == 10) {
EnemyHoming enemy = new EnemyHoming();
enemy.setPosition(x, y);
enemyList.add(enemy);
enemyTimer = 0.0f;
}
else if (rand == 11) {
EnemyTongue enemy = new EnemyTongue(this);
enemy.setPosition(x, y);
enemyList.add(enemy);
}
}
}
public void draw(SpriteBatch batch) {
for (Enemy e : enemyList) {
if (e != null) {
e.draw(batch);
if (e instanceof EnemyMissile)
((EnemyMissile) e).drawParticle(batch);
e.processHits();
}
}
}
public void clear() {
enemyList.clear();
}
}
+86
View File
@@ -0,0 +1,86 @@
//Enemy missile acts as an enemy instead of a bullet, since it must be shot by the player.
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class EnemyMissile extends Enemy {
private final float HOMING_SPEED;
private float velocityX;
private float velocityY;
private float angle;
private ParticleEffect particle;
private float downTimer;// shoot downwards for a limited time to give the
// player time to react.
EnemyMissile() {
super(1, ResourceManager.Missile);
setRotation(getRotation() + 180);
HOMING_SPEED = SpaceShooter.getBulletSpeed() * 2.0f;
velocityX = 0.0f;
velocityY = 0.0f;
particle = new ParticleEffect();
particle.load(Gdx.files.internal(ResourceManager.MissileParticle),
Gdx.files.internal("media"));
for (int i = 0; i < particle.getEmitters().size; i++) {
particle.getEmitters().get(i).getTint()
.setColors(new float[] { 125, 0f, 0f, 125f });
}
}
public void updateEnemy(Player player, BulletManager bulletManager) {
downTimer += Gdx.graphics.getDeltaTime() / 5;
if (player != null) {
// Point the enemy towards the player.
angle = (float) Math.atan2(getY() - player.getY(),
getX() - player.getX());
float degrees = (float) (angle * (180 / Math.PI));
setRotation(degrees + 90);
/*
* translate(100 * Gdx.graphics.getDeltaTime() * (float)
* Math.cos(degrees - 90), 100 * Gdx.graphics.getDeltaTime() *
* (float) Math.sin(degrees - 90));
*/
// this.setY(this.getY() - 150 * Gdx.graphics.getDeltaTime());
// Move the enemy towards the player. Clean this up *
float directionX = getX() - player.getX();
float directionY = getY() - player.getY();
double sq = Math.sqrt(directionX * directionX + directionY
* directionY);
velocityX = (float) (directionX
* (HOMING_SPEED * Gdx.graphics.getDeltaTime()) / sq);
velocityY = (float) (directionY
* (HOMING_SPEED * Gdx.graphics.getDeltaTime()) / sq);
translate(-velocityX, -velocityY);
}
else {
translate(-velocityX, -velocityY);
}
}
public void drawParticle(SpriteBatch batch) {
particle.setPosition(getX() + (getTexture().getWidth() / 2), getY()
+ (getTexture().getHeight() / 2)); // set the particle's
// position to
for (int i = 0; i < particle.getEmitters().size; i++) {
particle.getEmitters().get(i).getAngle().setLow(angle - 180); // min
particle.getEmitters().get(i).getAngle().setHigh(angle - 180); // max
}
particle.draw(batch, Gdx.graphics.getDeltaTime());
}
public void draw(SpriteBatch batch) {
super.draw(batch);
drawParticle(batch);
}
}
+49
View File
@@ -0,0 +1,49 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.*;
public class EnemyMissileShip extends Enemy {
private EnemyManager enemyManager;
private float missileTimer; //Time between each shot;
private int state;
private boolean hasChanged;
private Texture textureM;
private Texture textureL;
private Texture textureR;
private Texture texture;
public EnemyMissileShip(EnemyManager enemyManager) {
super(50,ResourceManager.MissileEnemy);
texture = getTexture();
textureM = ResourceManager.getAssetManager().get(ResourceManager.MissileEnemyM,Texture.class);
textureL = ResourceManager.getAssetManager().get(ResourceManager.MissileEnemyL,Texture.class);
textureR = ResourceManager.getAssetManager().get(ResourceManager.MissileEnemyR,Texture.class);
this.enemyManager = enemyManager;
state = 0; //unaware of player.
hasChanged = false;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
if (state != 0)
missileTimer += Gdx.graphics.getDeltaTime() / 5;
if (state == 0) {
setY(getY() - 150 * Gdx.graphics.getDeltaTime());
}
if (player != null && (getDistance(player) < 200) && state == 0) {
setTexture(textureM);
state++;
}
else if (player != null && state == 1 && missileTimer > 0.3f) {
enemyManager.addEnemy(new EnemyMissile(),getX() - 10,getY());
enemyManager.addEnemy(new EnemyMissile(),getX() + 10,getY());
missileTimer = 0f;
}
//if ()
}
}
+60
View File
@@ -0,0 +1,60 @@
package com.spaceshooter;
public class EnemyPatterns {
public static void sineWave(EnemyManager enemyManager,float x,float y, int amount, String type) {
if (type.equals("EnemyPopcorn")) {
for (int i = 0; i < amount; i++) {
EnemyPopcorn enemy = new EnemyPopcorn(true, ((amount-1) * 15) - (i * 15));
enemy.setPosition(x, y + (i * (enemy.getHeight())));
enemyManager.getList().add(enemy);
}
}
else if (type.equals("EnemyKiller1")) {
for (int i = 0; i < amount; i++) {
EnemyKiller1 enemy = new EnemyKiller1(true, ((amount-1) * 15) - (i * 15));
enemy.setPosition(x, y + (i * (enemy.getHeight())));
enemyManager.getList().add(enemy);
}
}
}
// x,y coordinate is the bottom left corner of the blockade
public static void blockade(EnemyManager enemyManager,float x,float y, String type
, int xSize, int ySize) {
if (type.equals("EnemyPopcorn")) {
for (int i = 0; i < xSize;i++) {
for (int j = 0; j < ySize; j++) {
EnemyPopcorn enemy = new EnemyPopcorn();
enemy.setPosition(x + (enemy.getWidth() * i * 2f),y + (enemy.getHeight() * j * 2f));
enemyManager.getList().add(enemy);
}
}
}
else if (type.equals("EnemyKiller1")) {
for (int i = 0; i < xSize;i++) {
for (int j = 0; j < ySize; j++) {
EnemyKiller1 enemy = new EnemyKiller1();
enemy.setPosition(x + (enemy.getWidth() * i * 2f),y + (enemy.getHeight() * j * 2f));
enemyManager.getList().add(enemy);
}
}
}
else if (type.equals("EnemySpinner")) {
for (int i = 0; i < xSize;i++) {
for (int j = 0; j < ySize; j++) {
EnemySpinner enemy = new EnemySpinner();
enemy.setPosition(x + (enemy.getWidth() * i * 2f),y + (enemy.getHeight() * j * 2f));
enemyManager.getList().add(enemy);
}
}
}
}
}
+40
View File
@@ -0,0 +1,40 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
public class EnemyPopcorn extends Enemy {
private float bulletTimer;
private float angle;
private boolean m_Wave;
EnemyPopcorn() {
super(5);
bulletTimer = 0f;
m_Wave = false;
angle = 0f;
}
EnemyPopcorn(boolean wave, float startAngle) {
super(2);
m_Wave = wave;
bulletTimer = 0f;
angle = startAngle;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
bulletTimer += (Gdx.graphics.getDeltaTime() / 5);
angle += Gdx.graphics.getDeltaTime() * 100f;
if (m_Wave)
setPosition(getX() + 4f * (float) Math.sin(Math.toRadians(angle)),
getY() - 75 * Gdx.graphics.getDeltaTime());
else
setY(getY() - 150 * Gdx.graphics.getDeltaTime());
if (bulletTimer > 0.5f) {
bulletManager.getList().add(
new EnemyBulletBasic(getX(), getY(), 0,SpaceShooter.getBulletSpeed(), 0));
bulletTimer = 0;
}
}
}
+105
View File
@@ -0,0 +1,105 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
public class EnemyRedship extends Enemy {
private float bulletTimer;
private float angryTimer;
private Texture angryTexture;
private Texture happyTexture;
private boolean isAngry;
private boolean alternateShot;
private float angle;
EnemyRedship() {
super(50, ResourceManager.EnemyRedShip1); // tons of health! (45)
angryTexture = ResourceManager.getAssetManager().get(
ResourceManager.EnemyRedShip1b, Texture.class);
happyTexture = ResourceManager.getAssetManager().get(
ResourceManager.EnemyRedShip1, Texture.class);
setOrigin(getWidth() / 2, getHeight() / 2);
bulletTimer = 0f;
angryTimer = 0f;
isAngry = false;
alternateShot = false;
angle= 0.0f;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
angle += Gdx.graphics.getDeltaTime()*100f;
angryTimer += (Gdx.graphics.getDeltaTime() / 5);
if (isAngry == false) {
if (angryTimer > 0.5f) {
setTexture(angryTexture);
isAngry = true;
}
} else if (isAngry == true) {
bulletTimer += (Gdx.graphics.getDeltaTime() / 5);
if (bulletTimer > 0.10f && alternateShot == false) {
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(),
SpaceShooter.getBulletSpeed() * (float) Math.cos(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), 0, SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), (-1f * SpaceShooter.getBulletSpeed())
* (float) Math.cos(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(),
SpaceShooter.getBulletSpeed() * (float) Math.cos(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), 0, SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), (-1f * SpaceShooter.getBulletSpeed())
* (float) Math.cos(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
alternateShot = true;
bulletTimer = 0;
}
if (bulletTimer > 0.10f && alternateShot == true) {
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(),
SpaceShooter.getBulletSpeed() * (float) Math.sin(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), 0,SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), (-1f * SpaceShooter.getBulletSpeed())
* (float) Math.sin(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(),
SpaceShooter.getBulletSpeed() * (float) Math.sin(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), 0,SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), (-1f * SpaceShooter.getBulletSpeed())
* (float) Math.sin(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
alternateShot = false;
bulletTimer = 0;
}
if (angryTimer > 1.0f) {
isAngry = false;
setTexture(happyTexture);
angryTimer = 0f;
}
}
if (player != null)
setPosition(getX() + 4f*(float)Math.sin(Math.toRadians(angle)),getY() - 30*Gdx.graphics.getDeltaTime());
else
setPosition(getX() + 4f*(float)Math.sin(Math.toRadians(angle)),getY() - 100*Gdx.graphics.getDeltaTime());
}
@Override
public void hit() { // Make it easier for the player to see hits. Turns to
// pink instead of red.
// setY(getY() + 150 * Gdx.graphics.getDeltaTime()); Possible impact
// powerup??
gotHit = true;
setColor(Color.CLEAR);
health--;
if (health <= 0)
isDead = true;
}
}
+90
View File
@@ -0,0 +1,90 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
public class EnemySpinner extends Enemy {
private final int HOMING_SPEED;
private boolean isAngry;
private float directionX;
private float directionY;
private boolean m_hard;
private float bulletTimer;
EnemySpinner() {
super(25,ResourceManager.EnemySpinner);
HOMING_SPEED = 100;
isAngry = false;
setOrigin(getWidth() / 2, getHeight() / 2);
directionX = 0f;
directionY = 0f;
m_hard = true;
bulletTimer = 0f;
}
EnemySpinner(boolean hard) {
super(5,ResourceManager.EnemySpinner);
HOMING_SPEED = 300;
isAngry = false;
setOrigin(getWidth() / 2, getHeight() / 2);
directionX = 0f;
directionY = 0f;
m_hard = hard;
bulletTimer = 0f;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
rotate(Gdx.graphics.getDeltaTime()*300);
bulletTimer += (Gdx.graphics.getDeltaTime()/5); //Easier to work with as a smaller #
if (player != null) {
// Move the enemy towards the player. Clean this up *
float playerDist = (float) Math
.sqrt(Math.pow(getX() - player.getX(), 2)
+ Math.pow(getY() - player.getY(), 2));
if (playerDist < 200 || isAngry) {
if (!isAngry) {
isAngry = true;
setTexture(new Texture("media/spinner2.png"));
directionX = getX() - player.getX();
directionY = getY() - player.getY();
}
double sq = Math.sqrt(directionX * directionX + directionY
* directionY);
float velocityX = (float) (directionX
* (HOMING_SPEED * Gdx.graphics.getDeltaTime()) / sq);
float velocityY = (float) (directionY
* (HOMING_SPEED * Gdx.graphics.getDeltaTime()) / sq);
translate(-velocityX, -velocityY);
if (isAngry && m_hard) {
// Make bullets
if (bulletTimer > 0.009f) {
bulletManager.getList().add(new EnemyBulletBasic(getX()+getWidth()/2,getY()+getHeight()/2,
(-1f*SpaceShooter.getBulletSpeed())*(float)Math.cos(Math.toRadians(getRotation()-90)),
(-1f*SpaceShooter.getBulletSpeed())*(float)Math.sin(Math.toRadians(getRotation()-90)),0));
bulletManager.getList().add(new EnemyBulletBasic(getX()+getWidth()/2,getY()+getHeight()/2,
(-1f * SpaceShooter.getBulletSpeed())*(float)Math.cos(Math.toRadians(getRotation()+90)),
(-1f*SpaceShooter.getBulletSpeed())*(float)Math.sin(Math.toRadians(getRotation()+90)),0));
bulletTimer = 0f;
}
}
}
else {
super.updateEnemy(player, bulletManager);
}
}
else {
super.updateEnemy(player, bulletManager);
}
}
}
+125
View File
@@ -0,0 +1,125 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
public class EnemyTongue extends Enemy {
private float bulletTimer;
private float tongueTimer;
private Texture normalTexture;
private Texture tongueTexture;
private boolean tongueOut;
private boolean alternateShot;
private float angle;
private EnemyManager enemyManager;
private boolean straightShot;
EnemyTongue(EnemyManager enemyManager) {
super(40, ResourceManager.EnemyTongue);
this.enemyManager = enemyManager;
normalTexture = ResourceManager.getAssetManager().get(
ResourceManager.EnemyTongue, Texture.class);
tongueTexture = ResourceManager.getAssetManager().get(
ResourceManager.EnemyTongueOut, Texture.class);
setOrigin(getWidth() / 2, getHeight() / 2);
bulletTimer = 0f;
tongueTimer = 0f;
tongueOut = false;
alternateShot = false;
angle= 0.0f;
straightShot = false;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
angle += Gdx.graphics.getDeltaTime()*100f;
tongueTimer += (Gdx.graphics.getDeltaTime() / 5);
if (tongueOut == true) {
if (tongueTimer > 0.5f) {
setTexture(normalTexture);
isInvincible = true;
tongueOut = false;
tongueTimer = 0f;
if (!straightShot)
straightShot = true;
else
straightShot = false;
}
}
else if (tongueOut == false) {
bulletTimer += (Gdx.graphics.getDeltaTime() / 5);
if (!straightShot) {
if (bulletTimer > 0.10f && alternateShot == false) {
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(),
SpaceShooter.getBulletSpeed() * (float) Math.cos(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), 0, SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), (-1f * SpaceShooter.getBulletSpeed())
* (float) Math.cos(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(),
SpaceShooter.getBulletSpeed() * (float) Math.cos(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), 0, SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), (-1f * SpaceShooter.getBulletSpeed())
* (float) Math.cos(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
alternateShot = true;
bulletTimer = 0;
}
if (bulletTimer > 0.10f && alternateShot == true) {
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(),
SpaceShooter.getBulletSpeed() * (float) Math.sin(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), 0,SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 118, getY(), (-1f * SpaceShooter.getBulletSpeed())
* (float) Math.sin(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(),
SpaceShooter.getBulletSpeed() * (float) Math.sin(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), 0,SpaceShooter.getBulletSpeed(), 0));
bulletManager.getList().add(
new EnemyBulletBasic(getX() + 10, getY(), (-1f * SpaceShooter.getBulletSpeed())
* (float) Math.sin(Math.PI / 3),SpaceShooter.getBulletSpeed(), 0));
alternateShot = false;
bulletTimer = 0;
}
}
else {
if (player != null) {
if (bulletTimer > 0.5f && (getY() > player.getY())) {
enemyManager.addEnemy(
new EnemyMissile(),getX() + 118, getY());
enemyManager.addEnemy(
new EnemyMissile(),getX() + 10, getY());
bulletTimer = 0.0f;
}
}
}
if (tongueTimer > 1.0f && !tongueOut) {
tongueOut = true;
isInvincible = false;
setTexture(tongueTexture);
tongueTimer = 0f;
}
}
if (player != null) {
if (getY() > (player.getY() + 200))
setPosition(getX() + 2f*(float)Math.sin(Math.toRadians(angle)),getY() - 30*Gdx.graphics.getDeltaTime());
else
setPosition(getX() + 2f*(float)Math.sin(Math.toRadians(angle)),getY() + 60*Gdx.graphics.getDeltaTime());
}
else
setPosition(getX() + 4f*(float)Math.sin(Math.toRadians(angle)),getY() - 100*Gdx.graphics.getDeltaTime());
}
}
+49
View File
@@ -0,0 +1,49 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
public class EnemyTurret extends Enemy {
private float shootTimer;
private float degrees;
EnemyTurret() {
super(5);
shootTimer = 0;
setOrigin(getWidth()/2,getHeight()/2);
degrees = 0.0f;
}
public void updateEnemy(Player player, BulletManager bulletManager) {
if (player != null) {
// Point the enemy towards the player.
float angle = (float) Math.atan2(getY() - player.getY(), getX()
- player.getX());
float degrees = (float) (angle * (180 / Math.PI));
setRotation(degrees - 90);
super.updateEnemy(player, bulletManager);
// Make a bullet
shootTimer += (Gdx.graphics.getDeltaTime() / 5);
if (shootTimer > 0.4f) {
float directionX = getX() - player.getX();
float directionY = getY() - player.getY();
double sq = Math
.sqrt(directionX * directionX + directionY * directionY);
float velocityX = (float) (directionX
* (SpaceShooter.getBulletSpeed()) / sq);
float velocityY = (float) (directionY
* (SpaceShooter.getBulletSpeed()) / sq);
bulletManager.getList().add(new EnemyBulletBasic(getX(), getY(),velocityX,velocityY,
0));
shootTimer = 0f;
}
}
else {
setRotation(degrees - 90);
super.updateEnemy(player, bulletManager);
}
}
}
+49
View File
@@ -0,0 +1,49 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Explosion extends Sprite {
private ParticleEffect m_Particle;
private Sound explosionSound;
Explosion(float x, float y) {
//m_Particle = new ParticleEffect();
//m_Particle.load(Gdx.files.internal("media/explosion2.p"),
//Gdx.files.internal("media"));
m_Particle = ResourceManager.getAssetManager().get(ResourceManager.ExplosionParticle1,
ParticleEffect.class);
m_Particle.setPosition(x,y);
m_Particle.start();
setPosition(x,y);
explosionSound = ResourceManager.getAssetManager().get(ResourceManager.ExplosionSound1,
Sound.class);
if (!SpaceShooter.isMuted())
explosionSound.play(0.2f);
}
Explosion(float x, float y,float volume) {
m_Particle = ResourceManager.getAssetManager().get(ResourceManager.ExplosionParticle1,
ParticleEffect.class);
m_Particle.setPosition(x,y);
m_Particle.start();
setPosition(x,y);
explosionSound = ResourceManager.getAssetManager().get(ResourceManager.ExplosionSound1,
Sound.class);
if (!SpaceShooter.isMuted())
explosionSound.play(volume);
}
public void draw(SpriteBatch batch) {
drawParticle(batch);
}
public void drawParticle(SpriteBatch batch) {
m_Particle.draw(batch, Gdx.graphics.getDeltaTime());
}
}
+169
View File
@@ -0,0 +1,169 @@
package com.spaceshooter;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
public class LevelScreen implements Screen {
private SpaceShooter game;
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture bgtexture;
private Sprite bgsprite;
private Texture bgtexture2;
private Sprite bgsprite2;
private float scrollTimer = 0.0f;
private Music bgm;
private BulletManager bulletManager;
private EnemyManager enemyManager;
private PlayerManager playerManager;
private ScoreHandler scoreHandler;
private PickupManager pickupManager;
public LevelScreen(SpaceShooter game) {
this.game = game;
Gdx.input.setCursorCatched(true);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(w, h);
batch = new SpriteBatch();
// Load the stars with alpha channel
bgtexture = ResourceManager.getAssetManager().get(
ResourceManager.PrimaryBG1, Texture.class);
bgtexture.setWrap(Texture.TextureWrap.Repeat,
Texture.TextureWrap.Repeat);
bgtexture2 = ResourceManager.getAssetManager().get(
ResourceManager.ParallaxBG, Texture.class);
bgtexture2.setWrap(Texture.TextureWrap.Repeat,
Texture.TextureWrap.Repeat);
// first scroll
bgsprite = new Sprite(bgtexture);
bgsprite.setPosition(SpaceShooter.getLeftBound(), SpaceShooter.getBottomBound());
// Load the stars with alpha channel
// second scroll to make parallax
bgsprite2 = new Sprite(bgtexture2);
//bgsprite2.translate(getLeftBound()*2,getBottomBound());
bgsprite2.setPosition(SpaceShooter.getLeftBound() - (bgsprite2.getWidth()/4), SpaceShooter.getLeftBound());
bgsprite2.setColor(bgsprite2.getColor().r, bgsprite2.getColor().g,
bgsprite2.getColor().b, 125);
bulletManager = new BulletManager();
enemyManager = new EnemyManager();
bgm = Gdx.audio.newMusic(Gdx.files.internal("media/ethervapor.mp3"));
bgm.setLooping(true);
if (!SpaceShooter.isMuted())
bgm.play();
playerManager = new PlayerManager();
scoreHandler = new ScoreHandler();
pickupManager = new PickupManager();
}
@Override
public void render(float delta) {
if (Gdx.input.isKeyPressed(Keys.BACK) || Gdx.input.isKeyPressed(Keys.ESCAPE)) {
game.setScreen(new MenuScreen(game));
}
scrollTimer += (Gdx.graphics.getDeltaTime() / 5);
if (scrollTimer > 2.0f)
scrollTimer = 0.0f;
bgsprite.setV(0.5f * scrollTimer);
bgsprite.setV2(0.5f * scrollTimer - 1);
bgsprite2.setV(2f * scrollTimer);
bgsprite2.setV2(2f * scrollTimer - 1);
enemyManager.update(playerManager.getPlayer(), scoreHandler,
bulletManager);
playerManager.update(enemyManager,bulletManager, pickupManager, scoreHandler);
pickupManager.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
bgsprite.draw(batch);
bgsprite2.draw(batch);
bulletManager.update(enemyManager, batch, scoreHandler,
playerManager.getPlayer(), pickupManager);
enemyManager.draw(batch);
playerManager.drawPlayer(batch);
pickupManager.draw(batch);
bulletManager.draw(batch);
scoreHandler.displayScore(batch,SpaceShooter.getLeftBound(),
SpaceShooter.getTopBound()); // @ Top left of screen
batch.end();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void show() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
dispose();
}
@Override
public void resume() {
// TODO Auto-generated method stub
ResourceManager.loadAll();
}
@Override
public void dispose() {
scoreHandler.saveScore();
batch.dispose();
bulletManager.dispose();
enemyManager.clear();
bgtexture.dispose();
bgtexture2.dispose();
playerManager.dispose();
pickupManager.dispose();
ResourceManager.getAssetManager().dispose();
}
}
+275
View File
@@ -0,0 +1,275 @@
package com.spaceshooter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
public class MenuScreen implements Screen {
private SpaceShooter game;
// setup the dimensions of the menu buttons
private static final float BUTTON_WIDTH = 500f;
private static final float BUTTON_HEIGHT = 60f;
private static final float BUTTON_SPACING = 10f;
private Skin defaultSkin;
private Skin gameSkin;
private static Stage stage;
private Texture bgtexture;
private Sprite bgsprite;
private Texture bgtexture2;
private Sprite bgsprite2;
private float scrollTimer = 0.0f;
private SpriteBatch batch;
private OrthographicCamera camera;
private Player showPlayer;
private static boolean hasAnswered;
private static boolean hasLoaded;
private TextButton startGameButton;
private TextButton exitButton;
private TextButton howToButton;
private Label welcomeLabel;
public static class MuteDialog extends Dialog {
public MuteDialog(Skin skin) {
super("Mute", skin);
text("Would you like to mute the in-game sound?");
button("Yes","mute");
button("No","nomute");
}
protected void result(Object object) {
if (object.toString().equals("mute"))
SpaceShooter.muteSound();
hasAnswered = true;
}
}
public static class HowToDialog1 extends Dialog {
private Skin skin;
public HowToDialog1(Skin skin) {
super("How to Play 1", skin);
this.skin = skin;
text("To play, use the touch screen to maneuver and fire your weapon.\nThe objective of the game is to survive against the enemies and reach a new high score.");
button("Next","next");
button("Exit","nomute");
}
protected void result(Object object) {
if (object.toString().equals("next"))
new HowToDialog2(skin).show(stage);
}
}
public static class HowToDialog2 extends Dialog {
public HowToDialog2(Skin skin) {
super("How to Play 2", skin);
text("As you progress, the enemies will spawn more frequently and become increasingly difficult.\nUse the power-ups to aid you.");
button("Exit","exit");
}
protected void result(Object object) {
}
}
public MenuScreen(final SpaceShooter game) {
ResourceManager.loadAll();
this.game = game;
Gdx.input.setCursorCatched(false);
stage = new Stage(new ScreenViewport());
Gdx.input.setInputProcessor(stage);
defaultSkin = new Skin((Gdx.files.internal("media/skinsandfonts/default/uiskin.json")));
gameSkin = new Skin((Gdx.files.internal("media/skinsandfonts/game/uiskin.json")));
hasAnswered = false;
hasLoaded = false;
MuteDialog mute = new MuteDialog(defaultSkin);
mute.show(stage);
final float buttonX = (Gdx.graphics.getWidth() - BUTTON_WIDTH) / 2;
//float currentY = 280f;
float currentY = Gdx.graphics.getHeight() / 1.5f;
// label "welcome"
welcomeLabel = new Label("SPACE SHOOTER", gameSkin);
welcomeLabel
.setX((Gdx.graphics.getWidth() - welcomeLabel.getWidth()) / 2);
welcomeLabel.setY(currentY + 100);
//stage.addActor(welcomeLabel);
// button "start game"
startGameButton = new TextButton("START GAME", gameSkin,
"default");
startGameButton.setX(buttonX);
//startGameButton.pack();
startGameButton.setY(currentY);
startGameButton.setWidth(BUTTON_WIDTH);
startGameButton.setHeight(BUTTON_HEIGHT);
//stage.addActor(startGameButton);
howToButton = new TextButton("HOW TO PLAY", gameSkin,
"default");
howToButton.setX(buttonX);
howToButton.setY(currentY - startGameButton.getHeight() - BUTTON_SPACING);
howToButton.setWidth(BUTTON_WIDTH);
howToButton.setHeight(BUTTON_HEIGHT);
exitButton = new TextButton("EXIT GAME", gameSkin,
"default");
exitButton.setX(buttonX);
exitButton.setY(howToButton.getY() - howToButton.getHeight() - BUTTON_SPACING);
exitButton.setWidth(BUTTON_WIDTH);
exitButton.setHeight(BUTTON_HEIGHT);
// Load the stars with alpha channel
bgtexture = ResourceManager.getAssetManager().get(
ResourceManager.PrimaryBG1, Texture.class);
bgtexture.setWrap(Texture.TextureWrap.Repeat,
Texture.TextureWrap.Repeat);
bgtexture2 = ResourceManager.getAssetManager().get(
ResourceManager.ParallaxBG, Texture.class);
bgtexture2.setWrap(Texture.TextureWrap.Repeat,
Texture.TextureWrap.Repeat);
// first scroll
bgsprite = new Sprite(bgtexture);
bgsprite.setPosition(SpaceShooter.getLeftBound(),
SpaceShooter.getBottomBound());
// Load the stars with alpha channel
// second scroll to make parallax
bgsprite2 = new Sprite(bgtexture2);
// bgsprite2.translate(getLeftBound()*2,getBottomBound());
bgsprite2.setPosition(
SpaceShooter.getLeftBound() - (bgsprite2.getWidth() / 4),
SpaceShooter.getLeftBound());
bgsprite2.setColor(bgsprite2.getColor().r, bgsprite2.getColor().g,
bgsprite2.getColor().b, 125);
batch = new SpriteBatch();
camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
showPlayer = new Player(0,SpaceShooter.getBottomBound()+ 200,false);
showPlayer.hideHitbox();
//stage.getRoot().setColor(1f,1f,1f,0f);
//stage.getRoot().addAction(Actions.fadeIn(2f));
}
@Override
public void render(float delta) {
if (hasAnswered && !hasLoaded) {
stage.addActor(welcomeLabel);
stage.addActor(startGameButton);
stage.addActor(howToButton);
stage.addActor(exitButton);
stage.getRoot().setColor(1f,1f,1f,0f);
stage.getRoot().addAction(Actions.fadeIn(3f));
startGameButton.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new LevelScreen(game));
}
});
howToButton.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
new HowToDialog1(defaultSkin).show(stage);
}
});
exitButton.addListener(new ClickListener() {
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
hasLoaded = true;
}
if ((Gdx.input.isKeyPressed(Keys.BACK) || Gdx.input.isKeyPressed(Keys.ESCAPE))) {
Gdx.app.exit();
}
stage.act(Gdx.graphics.getDeltaTime());
scrollTimer += (Gdx.graphics.getDeltaTime() / 5);
if (scrollTimer > 2.0f)
scrollTimer = 0.0f;
bgsprite.setV(0.5f * scrollTimer);
bgsprite.setV2(0.5f * scrollTimer - 1);
bgsprite2.setV(2f * scrollTimer);
bgsprite2.setV2(2f * scrollTimer - 1);
Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
bgsprite.draw(batch);
bgsprite2.draw(batch);
showPlayer.draw(batch);
batch.end();
stage.draw();
Table.drawDebug(stage);
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void show() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
dispose();
}
@Override
public void resume() {
// TODO Auto-generated method stub
ResourceManager.loadAll();
}
@Override
public void dispose() {
stage.dispose();
gameSkin.dispose();
defaultSkin.dispose();
}
}
+17
View File
@@ -0,0 +1,17 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
public class MissilePickup extends Pickup {
public MissilePickup(float xPos, float yPos) {
super(ResourceManager.getAssetManager().get(ResourceManager.MissilePickup,Texture.class));
setPosition(xPos,yPos);
}
@Override
public void update() {
translateY(-100 * Gdx.graphics.getDeltaTime());
}
}
+11
View File
@@ -0,0 +1,11 @@
package com.spaceshooter;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
public abstract class Pickup extends Sprite {
public Pickup(Texture texture) {
super(texture);
}
public abstract void update();
}
+41
View File
@@ -0,0 +1,41 @@
package com.spaceshooter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
class PickupManager {
private List<Pickup> pickupList;
public PickupManager() {
pickupList = new LinkedList<Pickup>();
}
public List<Pickup> getList() {
return pickupList;
}
public void draw(SpriteBatch batch) {
for (Pickup p : pickupList) {
p.draw(batch);
}
}
public void update() {
ListIterator<Pickup> pickupIter = pickupList.listIterator();
while (pickupIter.hasNext()) {
Pickup p = pickupIter.next();
p.update();
if (p.getY() < SpaceShooter.getBottomBound() - p.getHeight())
pickupIter.remove();
}
}
public void clear() {
pickupList.clear();
}
public void dispose() {
pickupList.clear();
}
}
+183
View File
@@ -0,0 +1,183 @@
package com.spaceshooter;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Player extends Sprite {
public ParticleEffect particle;
public boolean isBlinking;
private float blinkTimer;
private Sprite playerSprite;
private boolean isDead;
private int weaponLevel;
private boolean hasMissile;
private float missileTimer;
private float bulletTimer;
private boolean hideHitbox;
private float hasMissileTimer; //when this reaches a peak, the player loses missile ability until the player acquires the powerup again.
private static final int MAX_LEVEL = 4;
private float levelTimer; //After some time, decrement the player's level to promote pick-ups.
Player(float x, float y,boolean justDied) {
super(ResourceManager.getAssetManager().get(ResourceManager.Hitbox,Texture.class));
playerSprite = new Sprite(ResourceManager.getAssetManager().get(ResourceManager.Player,Texture.class));
bulletTimer = 0f;
missileTimer = 0f;
levelTimer = 0f;
hasMissileTimer = 0f;
setOrigin(4,4);
setPosition(x, y);
Gdx.input.setCursorPosition((int)x, (int)y);
hideHitbox = false;
particle = ResourceManager.getAssetManager().get(ResourceManager.Thrusters,
ParticleEffect.class);
isDead = false;
weaponLevel = 1;
hasMissile = false;
if (justDied) {
blinkTimer = 0.50f;
isBlinking = true;
}
else {
blinkTimer = 0.0f;
isBlinking = false;
}
playerSprite.setPosition(getX()-27,getY()-10);
particle.setPosition(playerSprite.getX() + 30, playerSprite.getY() - 5);
}
public Sprite getShowSprite() {
return playerSprite;
}
public void update(BulletManager bulletManager) {
System.out.println("XPos: " + getX() + " YPos:" + getY());
if (getX() >= SpaceShooter.getRightBound())
setX(SpaceShooter.getRightBound());
else if (getY() >= SpaceShooter.getTopBound())
setY(SpaceShooter.getTopBound());
else if (getX() <= SpaceShooter.getLeftBound())
setX(SpaceShooter.getLeftBound());
else if (getY() <= SpaceShooter.getBottomBound())
setY(SpaceShooter.getBottomBound());
bulletTimer += Gdx.graphics.getDeltaTime() / 5;
levelTimer += Gdx.graphics.getDeltaTime() / 5;
if (hasMissile) {
missileTimer += Gdx.graphics.getDeltaTime() / 5;
hasMissileTimer += Gdx.graphics.getDeltaTime();
}
translate(Gdx.input.getDeltaX()+(Gdx.graphics.getDeltaTime()),-Gdx.input.getDeltaY()+(Gdx.graphics.getDeltaTime()));
if (Gdx.app.getType() == ApplicationType.Desktop) {
if (Gdx.input.isButtonPressed(Buttons.LEFT) && bulletTimer > 0.02f) {
bulletManager.getList().add(new PlayerBullet(getX() - 20,getY(),0,600f,weaponLevel));
bulletManager.getList().add(new PlayerBullet(getX() + 25,getY(),0,600f,weaponLevel));
bulletTimer = 0.0f;
}
}
else if (Gdx.app.getType() == ApplicationType.Android) {
if (Gdx.input.isTouched() && bulletTimer > 0.04f) {
bulletManager.getList().add(new PlayerBullet(getX() - 20,getY(),0,600f,weaponLevel));
bulletManager.getList().add(new PlayerBullet(getX() + 25,getY(),0,600f,weaponLevel));
bulletTimer = 0.0f;
}
}
if (hasMissileTimer > 10f) {
hasMissile = false;
hasMissileTimer = 0f;
}
if (levelTimer > 100f) {
decrementLevel();
}
if (missileTimer > 0.2f && hasMissile) {
bulletManager.getList().add(new PlayerMissile(getX() - 20,getY()));
bulletManager.getList().add(new PlayerMissile(getX() + 20,getY()));
bulletManager.getList().add(new PlayerMissile(getX() - 40,getY()));
bulletManager.getList().add(new PlayerMissile(getX() + 40,getY()));
missileTimer = 0f;
}
playerSprite.setPosition(getX()-27,getY()-10);
particle.setPosition(playerSprite.getX() + 30, playerSprite.getY() - 5);
if (blinkTimer > 0.0f && isBlinking) {
blinkTimer -= Gdx.graphics.getDeltaTime() / 5;
if (blinkTimer > 0.42f && blinkTimer < 0.50f) {
setColor(Color.CLEAR);
playerSprite.setColor(Color.CLEAR);
}
else if (blinkTimer > 0.17f && blinkTimer < 0.25f) {
setColor(Color.CLEAR);
playerSprite.setColor(Color.CLEAR);
}
else if (blinkTimer > 0.0f && blinkTimer < 0.08f) {
setColor(Color.CLEAR);
playerSprite.setColor(Color.CLEAR);
}
else {
setColor(Color.WHITE);
playerSprite.setColor(Color.WHITE);
}
if (blinkTimer <= 0.0f) {
blinkTimer = 0;
isBlinking = false;
}
}
if (!isBlinking) {
setColor(Color.WHITE);
playerSprite.setColor(Color.WHITE);
}
}
public void killPlayer() {
if (!isBlinking)
isDead = true;
}
public boolean isDead() {
return isDead;
}
public void decrementLevel() {
if (weaponLevel > 0)
weaponLevel--;
}
public void incrementLevel() {
levelTimer = 0f; //reset the level timer.
weaponLevel++;
if (weaponLevel > MAX_LEVEL)
weaponLevel = MAX_LEVEL;
}
public int getWeaponLevel() {
return weaponLevel;
}
public void giveMissile() {
hasMissile = true;
}
public void hideHitbox() {
hideHitbox = true;
}
public void draw(SpriteBatch batch) {
particle.draw(batch,Gdx.graphics.getDeltaTime());
playerSprite.draw(batch);
if (!hideHitbox)
super.draw(batch);
}
}
+65
View File
@@ -0,0 +1,65 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class PlayerBullet extends Bullet {
private ParticleEffect particle;
float velocityX;
float velocityY;
private int weaponLevel;
public PlayerBullet(float xPos,float yPos,float velocityX,float velocityY,int weaponLevel) {
super(ResourceManager.PlayerBulletSprite);
particle = new ParticleEffect();
particle.load(Gdx.files.internal(ResourceManager.PlayerBullet),Gdx.files.internal("media"));
setPosition(xPos,yPos);
this.velocityY = velocityY;
this.velocityX = velocityX;
this.weaponLevel = weaponLevel;
if (weaponLevel == 1) {
for (int i = 0; i < particle.getEmitters().size;i++) {
particle.getEmitters().get(i).getTint().setColors(new float[] {125f,0f,0f,125f});
}
}
// If weaponLevel == 2, leave as blue
else if (weaponLevel == 3) {
for (int i = 0; i < particle.getEmitters().size;i++) {
particle.getEmitters().get(i).getTint().setColors(new float[] {0f,125f,0f,125f});
}
}
else if (weaponLevel >= 4) {
for (int i = 0; i < particle.getEmitters().size;i++) {
particle.getEmitters().get(i).getTint().setColors(new float[] {125f,125f,125f,125f});
}
}
}
//Constructor for subclasses i.e. missiles :
public PlayerBullet(String texture) {
super(texture);
}
public void updateBullet(SpriteBatch batch,EnemyManager enemyManager) {
setOrigin(getTexture().getWidth() / 2, getTexture().getHeight() / 2);
translate(velocityX * Gdx.graphics.getDeltaTime(),velocityY * Gdx.graphics.getDeltaTime());
drawParticle(batch);
}
public void drawParticle(SpriteBatch batch) {
float angle = (float) Math.toDegrees(Math.atan2(velocityY,velocityX));
particle.setPosition(getX(), getY()); // set the particle's position to
for (int i = 0; i < particle.getEmitters().size;i++) {
particle.getEmitters().get(i).getAngle().setLow(angle - 180); //min
particle.getEmitters().get(i).getAngle().setHigh(angle - 180); //max
}
// the bullet's
particle.draw(batch, Gdx.graphics.getDeltaTime());
}
public int getWeaponLevel() {
return weaponLevel;
}
}
+68
View File
@@ -0,0 +1,68 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Intersector;
/* Manages the player re-spawns, spawns, and explosions
*/
public class PlayerManager {
private Player player;
Explosion explosion = null;
float respawnTimer;
PlayerManager() {
player = new Player(0,0,false);
respawnTimer = 0.0f;
}
public void update(EnemyManager enemyManager,BulletManager bulletManager,PickupManager pickupManager,ScoreHandler scoreHandler) {
if (player != null) {
player.update(bulletManager);
for (int i = 0; i < pickupManager.getList().size(); i++) {
if (Intersector.overlaps(pickupManager.getList().get(i).getBoundingRectangle()
,player.getShowSprite().getBoundingRectangle())) {
if (pickupManager.getList().get(i) instanceof CoinPickup)
scoreHandler.addScore(1000);
else if (pickupManager.getList().get(i) instanceof WeaponPickup)
player.incrementLevel();
else if (pickupManager.getList().get(i) instanceof MissilePickup)
player.giveMissile();
pickupManager.getList().remove(i);
}
}
if (player.isDead()) {
explosion = new Explosion(player.getX(),player.getY(),1);
scoreHandler.saveScore();
scoreHandler.setScore(0);
player = null;
}
}
else {
respawnTimer += Gdx.graphics.getDeltaTime();
}
if (respawnTimer >= 2.0f && player == null) {
player = new Player(0,-200,true);
bulletManager.clear();
enemyManager.clear();
respawnTimer = 0.0f;
}
}
public void drawPlayer(SpriteBatch batch) {
if (player != null)
player.draw(batch);
if (explosion != null)
explosion.draw(batch);
}
public void dispose() {
if (player != null)
player.getTexture().dispose();
}
public Player getPlayer() {
return player;
}
}
+84
View File
@@ -0,0 +1,84 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class PlayerMissile extends PlayerBullet {
private ParticleEffect particle;
private float velocityX;
private float velocityY;
private float angle;
private double minDist;
private boolean hasFound;
Enemy closestEnemy;
public PlayerMissile(float xPos,float yPos) {
super(ResourceManager.Missile);
/*particle = ResourceManager.getAssetManager().get(ResourceManager.MissileParticle,
ParticleEffect.class);*/
particle = new ParticleEffect();
//particle.load(new FileHandle(ResourceManager.MissileParticle),new FileHandle("media"));
particle.load(Gdx.files.internal(ResourceManager.MissileParticle),Gdx.files.internal("media"));
setPosition(xPos,yPos);
velocityX = 0f;
velocityY = 0f;
angle = 0;
minDist = 99999.0;
hasFound = false;
}
public void updateBullet(SpriteBatch batch,EnemyManager enemyManager) {
//Find the closest enemy once the bullet has been shot.
if (!hasFound) {
for (Enemy e: enemyManager.getList()) {
float distX = getX() - e.getX();
float distY = getY() - e.getY();
double dist = Math
.sqrt(distX * distX + distY * distY);
if (dist < minDist) {
minDist = dist;
closestEnemy = e;
}
}
hasFound = true;
}
if (minDist == 99999.0 || closestEnemy.isDead) {
velocityX = 0f;
velocityY = -400f * Gdx.graphics.getDeltaTime();
}
else {
float directionX = getX() - closestEnemy.getX();
float directionY = getY() - closestEnemy.getY();
double sq = Math
.sqrt(directionX * directionX + directionY * directionY);
velocityX = (float) (directionX
* (400 * Gdx.graphics.getDeltaTime()) / sq);
velocityY = (float) (directionY
* (400 * Gdx.graphics.getDeltaTime()) / sq);
}
angle = (float) Math.toDegrees(Math.atan2(velocityY,velocityX));
setRotation(angle + 90);
setOrigin(getTexture().getWidth() / 2, getTexture().getHeight() / 2);
translate(-velocityX,-velocityY);
this.draw(batch);
drawParticle(batch);
}
public void drawParticle(SpriteBatch batch) {
particle.setPosition(getX()+(getTexture().getWidth() / 2), getY()+(getTexture().getHeight() / 2)); // set the particle's position to
for (int i = 0; i < particle.getEmitters().size;i++) {
particle.getEmitters().get(i).getAngle().setLow(angle - 180); //min
particle.getEmitters().get(i).getAngle().setHigh(angle - 180); //max
}
particle.draw(batch, Gdx.graphics.getDeltaTime());
}
}
+99
View File
@@ -0,0 +1,99 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.assets.loaders.*;
import com.badlogic.gdx.assets.loaders.resolvers.*;
public class ResourceManager {
private static final AssetManager assetManager = new AssetManager();
private static final ParticleEffectLoader particleLoader = new ParticleEffectLoader(new InternalFileHandleResolver());
public static final String Player = "media/playerspaceship.png";
public static final String Hitbox = "media/hitbox.png";
public static final String Thrusters = "media/thrusters.p";
public static final String PlayerBullet = "media/bullet.p";
public static final String PlayerBulletSprite = "media/bulletsprite.png";
public static final String Missile = "media/missile.png";
public static final String MissileParticle = "media/missile.p";
public static final String WeaponPickup = "media/powerup/weaponpowerup.png";
public static final String Bullet = "media/enemybulletorange.png";
public static final String Coin = "media/powerup/coingreen.png";
public static final String MissilePickup = "media/powerup/missile.png";
public static final String Enemy = "media/enemy.png";
public static final String EnemyCargo1 = "media/cargo1.png";
// Next four are the same enemy, different "animations" :
public static final String MissileEnemy = "media/enemy/missileenemy/enemy.png";
public static final String MissileEnemyM = "media/enemy/missileenemy/enemym.png";
public static final String MissileEnemyL = "media/enemy/missileenemy/enemyl.png";
public static final String MissileEnemyR = "media/enemy/missileenemy/enemyr.png";
public static final String EnemyEye = "media/enemyeye.png";
public static final String EnemyFactory = "media/enemy/factory/factory.png";
public static final String EnemyFactoryDestroyed = "media/enemy/factory/factorydestroyed.png";
public static final String PrimaryBG1 = "media/bg/PrimaryBackground.png";
public static final String ParallaxBG = "media/bg/ParallaxStars.png";
public static final String EnemyRedShip1 = "media/redship1.png";
public static final String EnemyRedShip1b = "media/redship1.png";
public static final String EnemyTongue = "media/enemy/tongueship/ship.png";
public static final String EnemyTongueOut = "media/enemy/tongueship/shiptongue.png";
public static final String EnemyKiller1 = "media/Killer1.png";
public static final String EnemyKiller1b = "media/Killer1b.png";
public static final String EnemySpinner = "media/spinner1.png";
public static final String ExplosionParticle1 = "media/explosion2.p";
public static final String ExplosionSound1 = "media/explosionsound.mp3";
private static ParticleEffectLoader.ParticleEffectParameter param;
public ResourceManager() {
param.imagesDir = Gdx.files.internal("media");
}
public static void loadAll() {
assetManager.load(Player,Texture.class);
assetManager.load(Hitbox,Texture.class);
assetManager.load(PlayerBulletSprite,Texture.class);
assetManager.load(Thrusters,ParticleEffect.class);
assetManager.load(PlayerBullet,ParticleEffect.class);
assetManager.load(Bullet,Texture.class);
assetManager.load(Coin,Texture.class);
assetManager.load(Missile,Texture.class);
assetManager.load(MissileParticle,ParticleEffect.class);
assetManager.load(MissilePickup,Texture.class);
assetManager.load(WeaponPickup,Texture.class);
assetManager.load(Enemy,Texture.class);
assetManager.load(EnemyCargo1,Texture.class);
assetManager.load(MissileEnemy,Texture.class);
assetManager.load(MissileEnemyM,Texture.class);
assetManager.load(MissileEnemyL,Texture.class);
assetManager.load(MissileEnemyR,Texture.class);
assetManager.load(EnemyEye,Texture.class);
assetManager.load(EnemyFactory,Texture.class);
assetManager.load(EnemyFactoryDestroyed,Texture.class);
assetManager.load(PrimaryBG1,Texture.class);
assetManager.load(ParallaxBG,Texture.class);
assetManager.load(EnemyRedShip1,Texture.class);
assetManager.load(EnemyRedShip1b,Texture.class);
assetManager.load(EnemyTongue,Texture.class);
assetManager.load(EnemyTongueOut,Texture.class);
assetManager.load(EnemyKiller1,Texture.class);
assetManager.load(EnemyKiller1b,Texture.class);
assetManager.load(EnemySpinner,Texture.class);
assetManager.load(ExplosionSound1,Sound.class);
assetManager.load(ExplosionParticle1,ParticleEffect.class,param);
assetManager.finishLoading();
}
public static AssetManager getAssetManager() {
return assetManager;
}
}
+106
View File
@@ -0,0 +1,106 @@
package com.spaceshooter;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class ScoreHandler {
private int m_Score;
private BitmapFont m_Font;
private String m_String;
private String m_HighScoreString;
private FileHandle m_File;
private int m_CurrentHighScore;
public ScoreHandler() {
m_Score = 0;
m_Font = new BitmapFont(Gdx.files.internal("media/skinsandfonts/game/gamefont.fnt"),Gdx.files.internal("media/skinsandfonts/game/gamefont.png"),false);
m_Font.setColor(Color.WHITE);
m_Font.setScale(0.5f,0.5f);
m_String = new String("SCORE: " + m_Score);
m_Font.setColor(m_Font.getColor().r,m_Font.getColor().g,m_Font.getColor().b,125f);
m_CurrentHighScore = 0;
m_File = Gdx.files.local("data/data1.bin");
if (m_File.exists()) {
String scoreString = m_File.readString();
m_CurrentHighScore = Integer.valueOf(scoreString.substring(scoreString.indexOf(":") + 1,
scoreString.indexOf(";")));
}
else {
m_CurrentHighScore = 0;
}
m_HighScoreString = new String("HIGH SCORE: " + m_CurrentHighScore);
}
public ScoreHandler(int startingScore) {
m_Score = startingScore;
m_Font = new BitmapFont(Gdx.files.internal("media/skinsandfonts/game/gamefont.fnt"),Gdx.files.internal("media/skinsandfonts/game/gamefont.png"),false);
m_Font.setColor(Color.WHITE);
m_Font.setScale(0.5f,0.5f);
m_String = new String("SCORE: " + m_Score);
m_Font.setColor(m_Font.getColor().r,m_Font.getColor().g,m_Font.getColor().b,125f);
m_CurrentHighScore = 0;
m_File = Gdx.files.local("data/data1.bin");
if (m_File.exists()) {
String scoreString = m_File.readString();
m_CurrentHighScore = Integer.valueOf(scoreString.substring(scoreString.indexOf(":") + 1,
scoreString.indexOf(";")));
}
else {
m_CurrentHighScore = 0;
}
m_HighScoreString = new String("HIGH SCORE: " + m_CurrentHighScore);
}
public int getScore() {
return m_Score;
}
public void setScore(int amount) {
m_Score = amount;
}
public void addScore(int amount) {
m_Score += amount;
}
public void reduceScore(int amount) {
m_Score -= amount;
}
public void displayScore(SpriteBatch batch, float x, float y) // displays
// the score
// on the
// screen.
// (use in
// main
// loop)
{
m_String = "SCORE: " + m_Score + " FPS: " + Gdx.graphics.getFramesPerSecond(); // dynamically set the score.
m_HighScoreString = new String("HIGH SCORE: " + m_CurrentHighScore);
m_Font.draw(batch, m_String, x, y);
m_Font.draw(batch, m_HighScoreString, x, y - 50); //show it later
}
public void saveScore() {
if (m_Score > m_CurrentHighScore) {
m_CurrentHighScore = m_Score;
m_File.writeString("SCORE:" + m_Score + ";", false);
}
}
}
+48
View File
@@ -0,0 +1,48 @@
package com.spaceshooter;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class SpaceShooter extends Game {
private MenuScreen menuScreen;
private static boolean isMuted;
@Override
public void create() {
isMuted = false;
menuScreen = new MenuScreen(this);
setScreen(menuScreen);
}
public static boolean isMuted() {
return isMuted;
}
public static void muteSound() {
isMuted = true;
}
public static int getLeftBound() {
return -1 * (Gdx.graphics.getWidth() / 2);
}
public static int getRightBound() {
return (Gdx.graphics.getWidth() / 2);
}
public static int getTopBound() {
return (Gdx.graphics.getHeight() / 2);
}
public static int getBottomBound() {
return -1 * (Gdx.graphics.getHeight() / 2);
}
//Normalize the speed of bullets across the game.
public static float getBulletSpeed() {
return 175f;
}
}
+17
View File
@@ -0,0 +1,17 @@
package com.spaceshooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
public class WeaponPickup extends Pickup {
public WeaponPickup(float xPos, float yPos) {
super(ResourceManager.getAssetManager().get(ResourceManager.WeaponPickup,Texture.class));
setPosition(xPos,yPos);
}
@Override
public void update() {
translateY(-100 * Gdx.graphics.getDeltaTime());
}
}