/***********************************
**** SIMPLE BATTLES DOCUMENT ****
***********************************/
// source package
package simplebattle;
//JFC
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;
// game class
public class SimpleBattle extends Game {
//Game Variables//
//background sprites
Sprite background_image0, background_image1, clouds0, clouds1, cloud_shadow0, cloud_shadow1;
// character sprites
AnimatedSprite player;
AnimatedSprite weapon[]; // <--- here is the problem... I think. It wont let me create multiple instances this way.
// if I leave the square braces empty, I get the ok to compile but get an error on result.
// if I put something in the square braces I get the red line of don't bother.
/****************************************************************************/
/****************************** FIREWEAPON **********************************/
/****************************************************************************/
public void createWeapon(int n) {
// player's weapon
for(n=0; n<3; n++){
weapon[n] = new AnimatedSprite(getImages("resources/ioncannon.png",6,1),0,0);
weapon[n].setLocation(100, 100);
weapon[n].setFrame(0);
weapon[n].setAnimate(false);
weapon[n].setLoopAnim(false);
weapon[n].setActive(false);
}
}
public void updateWeapon(long t, int n){
double speed = -0.4; // set speed for simplicity
weapon[n].update(t); // update the weapon
if(weapon[n].isActive()) // if weapon is active, move it.
{
weapon[n].move(0, speed*t); // remember t is elapsed time.
}
if(weapon[n].getY() <= -32) // if weapon is off screen kill it.
{
weapon[n].setActive(false);
}
}
public void fireWeapon(int n){ // call this in Inputs
// set the weapon to active and set its loaction.
weapon[n].setActive(true);
weapon[n].setLocation((player.getX()-player.getWidth()/2)+45,player.getY());
}
public void renderWeapon(Graphics2D g, int n){
// render the weapon if it is active
if(weapon[n].isActive())
weapon[n].render(g);
}
/****************************************************************************/
/******************************* CHARACTER **********************************/
/****************************************************************************/
public void createPlayeri() {
// setup Player
Player = new AnimatedSprite(getImages("resources/player24bit6.png",6,1),0,0);//66,68),0,0);
// set player start location// this will change when the select screen is added
player.setLocation((player.getX()+640)/2, (player.getY()+480)/2);
// set start frame
player.setFrame(0);
player.setAnimate(false); // this is false because the animation is static
player.setLoopAnim(false); // only one frame per pose. why waste overhead?
}
public void updatePlayer(long t){
double speed = 0.25;
player.update(t);
// movements will be placed in INPUT section
if(keyPressed(KeyEvent.VK_ESCAPE)|| keyPressed(KeyEvent.VK_Q))//exit game
super.finish();
// move sprite and update it's frames
if(!keyDown(KeyEvent.VK_A) ||!keyDown(KeyEvent.VK_D))
player.setFrame(0); // normal
if (keyDown(KeyEvent.VK_S))
player.move(0, speed*t);
if (keyDown(KeyEvent.VK_W))
player.move(0, -speed*t);
if (keyDown(KeyEvent.VK_A))
{
player.setFrame(2); // bank left
player.move(-speed*t, 0);
}
if (keyDown(KeyEvent.VK_D))
{
player.setFrame(1);// bank right
player.move(speed*t, 0);
}
// fire weapon
for(int n=0; n<3;n++)
if(!weapon[n].isActive()) // if weapon is already on screen don't fire, otherwise fire
if(keyPressed(KeyEvent.VK_SPACE))
fireWeapon(n);
}
public void renderPlayer(Graphics2D g){
player.render(g);
}
/****************************************************************************/
/******************************* LEVEL **************************************/
/****************************************************************************/
public void createLevel(){
//set background
background_image0 = new Sprite(getImage("resources/CanyonVally.jpg"), 0,0);
background_image1 = new Sprite(getImage("resources/CanyonVally.jpg"), 0,480);
clouds0 = new Sprite(getImage("resources/Clouds.png"), 0,0);
clouds1 = new Sprite(getImage("resources/Clouds.png"), 0,480);
cloud_shadow0 = new Sprite(getImage("resources/Clouds_Shadow.png"), 5,-10);
cloud_shadow1 = new Sprite(getImage("resources/Clouds_Shadow.png"), 5,-490);
}
public void updateLevel(long t){
//update the background
background_image0.update(t);
background_image1.update(t);
clouds0.update(t);
clouds1.update(t);
cloud_shadow0.update(t);
cloud_shadow1.update(t);
// move backgroud
// basically I don't know how to wrap images, so I have two images that warp
// to each other's location once one image leaves the screen. If A is off screen
// move to the ass of B. if B is off screen, move to the ass of A.
background_image0.move(0,-0.1);
background_image1.move(0,-0.1);
if(background_image0.getY() <= -480)
{
background_image0.setLocation(0,background_image1.getY()+480);
}
if(background_image1.getY() <= -480)
{
background_image1.setLocation(0,background_image0.getY()+480);
}
// move foreground
clouds0.move(0,0.1);
clouds1.move(0,0.1);
cloud_shadow0.move(0,0.1);
cloud_shadow1.move(0,0.1);
// this is the same setup as the background images with A moving to the ass of B
// and B moving to the ass of A when either are off screen. Additionally the shadows of
// A and B are set to their locations with an offset to create depth.
if(clouds0.getY() >= 480)
clouds0.setLocation(0,clouds1.getY()-480);//-480);
if(cloud_shadow0.getY() >= 490)
cloud_shadow0.setLocation(5,clouds0.getY()-10);//-490);
if(clouds1.getY() >= 480)
clouds1.setLocation(0,clouds0.getY()-480);//-480);
if(cloud_shadow1.getY() >= 490)
cloud_shadow1.setLocation(5,clouds1.getY()-10);//-490);
}
public void renderLevel(Graphics2D g){
// draw the level to the screen
background_image0.render(g);
background_image1.render(g);
cloud_shadow0.render(g);
cloud_shadow1.render(g);
clouds0.render(g);
clouds1.render(g);
}
/****************************************************************************/
/**************************** GAME SKELETON *********************************/
/****************************************************************************/
// initialize game resources
@Override
public void initResources() {
// init level goes first
createLevel();
// init charscters
createplayer();
// init bullet (this needs to come before player for layering, I'll do that later)
for(int n = 0; n<3; n++)
createWeapon(n);
}
// update game functions
@Override
public void update(long elapsedTime) {
// level updates
updateLevel(elapsedTime);
// player updates
updatePlayer(elapsedTime);
for(int n = 0; n<3; n++)
updateWeapon(elapsedTime,n);
}
// redner graphics to the screen
@Override
public void render(Graphics2D g) {
// Graphics Engine
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
/////////////////////////
//redner game elements///
/////////////////////////
// draw level first
renderLevel(g);
// draw players (should go inside level for layering)
renderPlayer(g);
// draw bullet (this needs to come before player for layering, I'll do that later)
for(int n = 0; n<3; n++)
renderWeapon(g,n);
}
/****************************************************************************/
/***************************** START-POINT **********************************/
/****************************************************************************/
// main function
public static void main(String[] args) {
GameLoader game = new GameLoader();
game.setup(new SimpleBattle(), new Dimension(640,480), false);
game.start();
}
}