Slick Forums

Discuss the Slick 2D Library
It is currently Wed May 22, 2013 6:51 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Apr 18, 2012 2:50 pm 
Offline

Joined: Wed Feb 29, 2012 7:07 pm
Posts: 9
Hello all. My problem at this time is trying to create a firing system so that a new bullet is created while the button is down. I currently have one bullet rendering and moving (though not continuous movement from one press). So I wanted to have it so that each time the update reads the key pressed, it creates a new bullet.

Code:
@Override
   public void init(GameContainer gc, StateBasedGame sbg)
         throws SlickException {
      
      gc.setTargetFrameRate(60);
      screen = new Image("res/gamescreen.png");
      
      player = new Entity("player");
      player.AddComponent( new ImageRenderComponent("PlayerRender", new Image("res/player.png")) );
      player.setPosition(new Vector2f(300, 176));
      
      bullet = new Entity("bullet");
      bullet.AddComponent(new ImageRenderComponent("BulletRender", new Image("res/bullet.png")));
      bullet.setPosition(new Vector2f(315, 197));
      bullet.AddComponent(new BulletFiringComponent("BulletFire"));
   }


Here I init the player and the bullet. I'm perplexed by having the bullet entity being stored in an arraylist, then having a new one created that works correctly. I'll also post my component that handles the bullet firing motion.

Code:
package entities;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

public class BulletFiringComponent extends Component {

   float direction;
   ArrayList<Entity> bullets;
   
   public BulletFiringComponent (String id)  {
      
      this.id = id;
   }
   
   
   @Override
   public void update(GameContainer gc, StateBasedGame sbg, int delta) {

      Vector2f position = owner.getPosition();
      Vector2f start = new Vector2f(315, 197);
      Input input = gc.getInput();
      
      if (input.isKeyDown(Input.KEY_LEFT)) {
         
         
         position.x -= 6f;
         
      } else if (input.isKeyDown(Input.KEY_RIGHT))  {
                     
         
         position.x += 6f;
      }
      
      if (input.isKeyPressed(Input.KEY_B))  {
         
         owner.setPosition(start);
      }
   }
   
}


If anyone can explain what I need to do I would appreciate it greatly. :)


Top
 Profile  
 
PostPosted: Wed Apr 18, 2012 5:42 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
My game would create a new BulletEntity for each bullet (allowing each bullet has its own velocity, position, etc). Shooting a bullet would simply add it to my large list of (all) entities, which was updated every frame. Once the bullet hit a player or fell off the screen, it would be removed from the large list of entities (for the entities array, I used this technique).

I don't know what the deal is with your bullet firing component, or "owner", or whatever (ideally each bullet should be independent from each other; unless you want all the bullets to be translated when the user presses B). But basically you will need a new entity object per bullet.

And you'll probably want to delay it so that bullets aren't being fired every frame, e.g. see here.


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 7:40 am 
Offline

Joined: Mon Apr 16, 2012 5:04 pm
Posts: 14
Sorry to hijack the thread, but this is relevant and I don't feel like it needs its own thread.

dave, you mention how you handle ArrayLists. I instead use an iterator and a second "to remove" list. It's a little something like this in update():

Code:
Iterator<Entity> iter = entities.iterator();
while (iter.hasNext()) {
   Entity e = iter.next();
   
   //update the entity

   if (someCondition) entitiesToRemove.add(e);
}

//After all entities have been processed, remove expired entities

Iterator<Entity> iter2 = entitiesToRemove.iterator();
while (iter2.hasNext()) {
   Entity e = iter2.next();
   entities.remove(e);
}

entitiesToRemove.clear();


How would the effectiveness of this compare to your method? It seems roughly the same.


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 5:27 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
The "efficiency" we're talking about here is pretty negligible. In most 2D games, your bottleneck will not be because of operations like these, but because of fill-rate limitation, texture binds, complex pathfinding/AI/etc.

With that said, your method is inefficient for a few reasons:
  • For an ArrayList, it's better to iterate through the indices as if it was an array. It should be slightly faster than creating a new Iterator.
  • Your implementation requires iterating over two lists.
  • Your implementation depends on ArrayList.remove(T), which is pretty slow. The method iterates over the list to find the object's index, then shifts all subsequent elements to the left (using System.arraycopy).
  • Your list might grow while updating an entity (e.g. to spawn a bullet), and since you're using iterators you'll run into ConcurrentModificationExceptions.
  • Ideally you should check "someCondition" twice to avoid updating a dead entity (once before updating the entity, and again after updating) -- e.g. if entity A 'kills' entity B, then entity B will not be updated on next iteration.


Top
 Profile  
 
PostPosted: Tue Apr 24, 2012 6:18 pm 
Offline

Joined: Mon Apr 16, 2012 5:04 pm
Posts: 14
All fair and well. Although I haven't hit any concurrent errors yet, but I guess that's just been because of the scope of my games so far. I see how it can be an issue though. Thanks!


Top
 Profile  
 
PostPosted: Wed May 02, 2012 12:36 am 
Offline

Joined: Sun Dec 25, 2011 5:48 pm
Posts: 34
ConcurrentModificationExceptions shouldn't be a problem unless you use several threads which, in my opinion, is something you should avoid unless you really have to.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group