Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Making a bullet, inventory + weapon system for a top down shooter[Kinda solved?]  (Read 1198 times)
0 Members and 1 Guest are viewing this topic.
Offline flameomega

Senior Newbie





« Posted 2013-05-20 06:27:40 »

Hey guys, I've been working on a 2D top-down shooter for about 4 months now with a couple of friends. I've recently been working on the GUI (finished implementing Slick's spritesheet system -- showcase is the health meter) and now I'm onto making the placeholder bullet (which is just a yellow line, haha) an actual "bullet" which has damage and velocity and what not. Which means making weapon Objects -- which leads into making an Inventory system. But I've been having some trouble with wrapping my head around certain concepts such as making an Array within the inventory class to contain the weapon Objects which will then set player sprite and assign the correct fields such as magSize, damage, ammoCarryLimit and what not.

Some help would be appreciated.  Smiley

<a href="http://www.youtube.com/v/hBwL6cX7EaE?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/hBwL6cX7EaE?version=3&amp;hl=en_US&amp;start=</a>
Offline quew8

JGO Coder


Medals: 32



« Reply #1 - Posted 2013-05-20 09:50:13 »

So first of all, just like you said, create an Object for the weapons containing all of its stats (maybe even an enum). After that it comes down to how your game will actually work. The way I see it, there are three options.

1) The current favorite for most games originally from Halo - two or three guns which you switch and swap. Have two Weapon fields in you Player class (or whatever) and an int to say which weapon is in use. Have a method getCurrentWeapon which switches between the two, and get the required stats from that method.

2) Oldschool Half-Life Style with a slot for each weapon in the game. Same as before but with more Weapon fields and some checks to see if you have the weapon before you switch to it. Also you'll need some kind of NoWeapon class (could use null but I find this works more smoothly)

3) RPG style like Skyrim with a general inventory so some are weapons, others are armour, quest items etc. Probably best to have a Weapon for the currentWeapon field rather than the int and you just return this. You would need a super class to all inventory items with some kind of onClick method which in the case of a weapon sets the currentWeapon filed to itself.

You could come up with a more imaginative way of handling an inventory but it would probably be some kind os specialization of these right?
Offline dime26

Senior Devvie


Medals: 2
Projects: 3
Exp: 5 years


Should traffic wardens be armed?


« Reply #2 - Posted 2013-05-20 12:53:13 »

Catacombs Snatch is a top down shooter and has lots of nice code, you may be interested in the Weapon classes:

https://github.com/Maescool/Catacomb-Snatch/tree/develop/src/com/mojang/mojam/entity/weapon

Browse all the classes might find some usefull code here.

https://github.com/Maescool/Catacomb-Snatch/tree/develop/src/com/mojang/mojam/entity

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline flameomega

Senior Newbie





« Reply #3 - Posted 2013-05-20 15:11:41 »

Thanks for all the info guys!

@Quew8
This is our Inventory in-game so far:
Sadly, it's just an image that pops up when you press E. (Toggleable)

So let me explain how it's supposed to work, the main row at the bottom is the hot bar, and all the other rows surrounding the player are for carrying any item/weapon.
And it's supposed to function like Minecraft's inventory where you can drag stuff and what not.

But for now, my friend told me to start off simple and make it so it works like a First Person Shooter's inventory (Array of 5, top-row number keys access indexes 0 to 4)

So, I understand that concept, and I've been following this game's code for the weapon-manager (https://www.dropbox.com/s/7p2jazq587h5ecx/Enigma.7z)

Here's a snippet of code from the gunManager class and abstractWeapon class (basically a template for a weapon Object, so each weapon Object will extend from this class) I've been making:

- abstractWeapon snippet_1
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
boolean bulletFired = false;
   boolean triggerPulled = false;
   
   public Timer fireRate = new Timer(0100);
   
   private ShootType shootType;
   //Shoot code block
   
   Sound singleFire;
   Sound autoFire;
   Sound reloadFull;
   Sound reloadEmpty;
   
   public static double magSize;
   public static double currentMagSize;
   public static double currentRemainingAmmo;
   public static double ammoCarryLimit;
   public static double damage;
   public static double recoil;
   public static double maxRecoil;
   
   double spawnX;
   double spawnY;
   
   Timer spawnRate;


- abstractWeapon snippet_2
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public static double getMagSize() {
      return magSize;
   }
   public static double getCurrentMagSize() {
      return damage;
   }
   public static double getCurrentRemainingAmmo() {
      return currentRemainingAmmo;
   }
   public static double getAmmoCarryLimit() {
      return ammoCarryLimit;
   }
   public static double getDamage() {
      return damage;
   }
   public static double getRecoil() {
      return recoil;
   }
   public static double getMaxRecoil() {
      return maxRecoil;
   }
   public static double getRecoilReduction() {
      return recoilReduction;
   }


- gunManager snippet_1 (code has been ripped from Enigma, so it's more so a reference than a straight copy from the game and it hasn't been optimized to fit our game's design pattern yet)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
public double getCurrentRemainingAmmo() {
            if (this.currentWeapon.equals("Pistol"))
               return Pistol.getCurrentRemainingAmmo();
            if (this.currentWeapon.equals("ShotGun"))
               return ShotGun.getCurrentRemainingAmmo();
            if (this.currentWeapon.equals("LE9AR"))
               return LE9AR.getCurrentRemainingAmmo();
            if (this.currentWeapon.equals("MP5"))
               return MP5.getCurrentRemainingAmmo();
            if (this.currentWeapon.equals("Barrett")) {
               return Barrett.getCurrentRemainingAmmo();
            }
            return 0;
         }
         public double getMagSize() {
            if (this.currentWeapon.equals("Pistol"))
               return Pistol.getMagSize();
            if (this.currentWeapon.equals("ShotGun"))
               return ShotGun.getMagSize();
            if (this.currentWeapon.equals("LE9AR"))
               return LE9AR.getMagSize();
            if (this.currentWeapon.equals("MP5"))
               return MP5.getMagSize();
            if (this.currentWeapon.equals("Barrett")) {
               return Barrett.getMagSize();
            }
            return 0;
         }
         public double getCurrentMagSize() {
            if (this.currentWeapon.equals("Pistol"))
               return Pistol.getCurrentMagSize();
            if (this.currentWeapon.equals("ShotGun"))
               return ShotGun.getCurrentMagSize();
            if (this.currentWeapon.equals("LE9AR"))
               return LE9AR.getCurrentMagSize();
            if (this.currentWeapon.equals("MP5"))
               return MP5.getCurrentMagSize();
            if (this.currentWeapon.equals("Barrett")) {
               return Barrett.getCurrentMagSize();
            }
            return 0;
         }
         public double getGunRecoilReduction() {
            if (this.currentWeapon.equals("Pistol"))
               return Pistol.getRecoilReduction();
            if (this.currentWeapon.equals("ShotGun"))
               return ShotGun.getRecoilReduction();
            if (this.currentWeapon.equals("LE9AR"))
               return LE9AR.getRecoilReduction();
            if (this.currentWeapon.equals("MP5"))
               return MP5.getRecoilReduction();
            if (this.currentWeapon.equals("Barrett")) {
               return Barrett.getRecoilReduction();
            }
            return 0.0D;
         }


So, would you guys suggest that I should start on our initial idea of the inventory (basically your idea of an RPG styled inventory, quew8), or should I start off slow with a Halo/FPS type inventory and then once I get that working -- change it so it fits the initial idea?

And I'll be sure to check out the source code for that game, dime26.
Offline quew8

JGO Coder


Medals: 32



« Reply #4 - Posted 2013-05-20 17:47:31 »

I would advise creating what you want to end up with from the very start, that way you program with that in mind rather than bending what you have later on. This concept makes for cleaner, better and prettier code. I don't think it requires to much change to your code anyway. Make AbstractWeapon implement some kind of InventoryItem interface.
Offline flameomega

Senior Newbie





« Reply #5 - Posted 2013-05-20 18:26:15 »

Alrighty, and yep I've made an inventory class (that's how the inventory image works right now)
But I forgot to mention the code snippets from said class:

Snippet_1
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class Inventory implements KeyListener, MouseListener{
   
   private Image inventoryImage;
   
   private boolean inventOpen = false;
   
   public static float inventoryGUIX = 0;
   public static float inventoryGUIY = 0;
   
   Play play;
   Player mainPlayer;


Snippet_2
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public void update(GameContainer gc, int delta) throws SlickException {
     
      try {
      } catch (Exception e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
      }
      Input input = gc.getInput();
     
      if(inventOpen == false)
      {
         if (input.isKeyPressed(Input.KEY_E))
         {
            inventOpen = true;
            System.out.println("inventory is now open");
         }
      }
      if(inventOpen == true)
      {
         if (input.isKeyPressed(Input.KEY_E))
         {
            inventOpen = false;
            System.out.println("inventory is now closed");
         }
      }
     
      mainPlayer.update(gc, delta);
     
   }


So my friend and I have decided to just start with the Halo inventory idea, having at least 3 weapons that can be swapped out by using the top-row keys and then once we have that down we can change it later on to fit our initial idea. Why exactly we've decided to do this is because the Alpha phase for this project is due in nine days, and we're really time restricted due to other school projects. (In short the thick of this game is for a computer science project, but after it's handed I'm planning on making it a full-fledged game ;P)

I'll keep posting updates Smiley
Offline 65K
« Reply #6 - Posted 2013-05-20 20:59:38 »

Why are the weapon attributes static ? Very unlikely that all weapons share the same attribute values.

Instead of using those if-blocks with string comparisons, rather directly call appropriate methods on the current weapon.

Offline flameomega

Senior Newbie





« Reply #7 - Posted 2013-05-20 21:23:58 »

I actually noticed that mistake right after I posted the code haha, don't worry it's all been fixed.

And thanks for the tip, appropriate methods would be using getters and setters, correct?
Offline 65K
« Reply #8 - Posted 2013-05-21 07:04:12 »

And thanks for the tip, appropriate methods would be using getters and setters, correct?
Yes, but only add those truly required, for instance, if the remaining bullet count of a weapon is set from inside a weapon's fire() method, do not add a public setter for it.

Offline flameomega

Senior Newbie





« Reply #9 - Posted 2013-05-21 16:27:06 »

Here's another update!
<a href="http://www.youtube.com/v/BjhqQyTxPJY?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/BjhqQyTxPJY?version=3&amp;hl=en_US&amp;start=</a>

Update:
Hitbox calculation method for the player's sprite has been created

And more progress on the inventory/weaponTemplate classes

[EDIT]
The reason why the hitbox is bigger than the player's actual image, is because we had to make the image's center right over the gun so the bullet will shoot out nicely.
Otherwise, I haven't a clue how we would give the player a perfectly cropped sprite, whilst having nice bullet direction.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline flameomega

Senior Newbie





« Reply #10 - Posted 2013-05-23 21:37:57 »

Ok so, I've been wanting to make a runnable jar and/or an executable of the game so far -- so how exactly should I go about doing that?

I've tried exporting the java project (in eclipse) as a runnable jar and then packaging required libraries into the generated JAR but the jar doesn't load the actual game (Launches the login screen and once I click play, it closes the login screen and doesn't do anything else. Yet the javaw.exe is still running in my processes)

We also have an ant script to build a resources.jar, but I'm guessing when I try to run the jar, it's not properly accessing the resource files.

Any suggestions?

[EDIT]

Also, how exactly would I get state the game is currently in?
I tried doing StateBasedGame.getCurrentStateID(); but it gave me a "cannot make static reference to a non-static method" :S
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!