I am pretty new to game development. I used to mess with RPG Maker when I was a kid and used the RMXP and VX to write some code and have made a batle system in that so I have some experience I guess. Anyways, I am learning to program games in Java using Slick2D. I have made a Pong clone and am now on to a more adventerous project. It is a side-scrolling space shooter. I have a good start so far I have a player ship that I can move, I have enemies that I can spawn. Collision is working. But I have a few questions.
1) I have a level class that contains all the Arrays that hold my objects. One of the Arrays holds the bullets. Currently I have two different types of weapons the player can use. You start with a machine gun and can upgrade to a plasma missile (Ooooh!). Anywys I am usure of a good way of adding the weapons to the array in a clean manner. Right now each of my weapon types has a create method that will create a weapon of its type at the position of the owner and move in the direction the owner is facing. It seems bad to me to have a object have a create method that crates a copy of itslef.
Here is how it works basically (This is from the update method of my level object):
if (player.getState() == GameEntity.State.shooting){
try{
bullets.add(player.createBullet());
}catch(Exception e){
System.out.println("Cant make a bullet");
}
}
and in my player object:
public BaseWeapon createBullet() throws SlickException{
Vector2f position = this.getPosition();
int direction = getDirection();
if (direction == 1){
return weapon.create(direction,(int)( position.x + bulletOffset.x), (int)(position.y + bulletOffset.y));
}else{
return weapon.create(direction, (int)(position.x), (int)(position.y + bulletOffset.y));
}
}
and finally in my weapon class (this is the machine gun):
public BaseWeapon create(int direction, int x, int y)
throws SlickException {
// TODO Auto-generated method stub
return new MachineGun(direction,x,y);
}
It seems like a bad way of doing it, but I am not sure how to have it determine what wepaon the player has and then create an object of that type and add it to the array other than making a method that returns the type and going through an if statement for each type. That would be a headache.
2.) Hopefully this one will be easier... In my player method I have the left mouse control shooting. All it does is set the state to shooting and the level object checks to see if a object is in the shooting state and then it creates a bullet from the code above. Sometimes if I double click or click quickly it gets stuck in the shooting state and it will fire an endless stream of bullets until I click again can you see why?
if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && getState() != GameEntity.State.overheated){
if(getFire() == 0){
System.out.println("Fire!");
setFire(getFireRate());
increaseHeat(getWeaponHeatRate());
setState(GameEntity.State.shooting);
}else{
setState(GameEntity.State.idle);
System.out.println("no fire");
setFire(getFire() - 1);
}
}
If you need/want to see more code I'll glady upload the source.
Thanks for any input
Nick






