Jump to content
  • Advertisement
Sign in to follow this  
jake_Ghost

[java] Need help with vectors

This topic is 4876 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on an enemy class for a game that I am making and I am using vectors to get the data. I recently noticed that no matter how u get the data for the vector it always becomes an object. I was wondering if there was a way to make the data another type of my choice. The main reason i need this is for declaring images, but this would also make it easier for my integers. Here is my class code:
import java.util.Vector;
import java.io.*;
import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.awt.event.*;

public class Enemy
{
    /* FILE ORDER
        enemy x
        enemy y
        patrol x
        patrol y
        aggro range
        retreat range
        health
        attack
        image up
        image down
        image right
        image left
        image death
    */

    Vector StartX = new Vector (0, 5);
    Vector StartY = new Vector (0, 5);
    Vector PatrolX = new Vector (0, 5);
    Vector PatrolY = new Vector (0, 5);
    Vector eX = new Vector (0, 5);
    Vector eY = new Vector (0, 5);
    Vector aggroRange = new Vector (0, 5);
    Vector retreatRange = new Vector (0, 5);
    Vector Health = new Vector (0, 5);
    Vector Attack = new Vector (0, 5);
    Vector Up = new Vector (0, 5);
    Vector Down = new Vector (0, 5);
    Vector Right = new Vector (0, 5);
    Vector Left = new Vector (0, 5);
    Vector Death = new Vector (0, 5);
    Vector State = new Vector (0, 5);
    Vector ImageState = new Vector (0, 5);
    BufferedReader fileInput;
    int EnemyCount = 0;

    int ENEMY_NORMAL = 0;
    int ENEMY_DEAD = 1;
    int eUP = 0;
    int eDOWN = 1;
    int eRIGHT = 2;
    int eLEFT = 3;
    int eDEAD = 4;

    public boolean Init (Applet THIS)
    {
        String strTemp = "";
        Image image = null;
        try
        {
            fileInput = new BufferedReader (new FileReader ("Enemy.dat"));
            strTemp = fileInput.readLine ();

            if (strTemp == null)
            {
                return false;
            }
            else
            {
                fileInput.close ();
                fileInput = null;
                fileInput = new BufferedReader (new FileReader ("Enemy.dat"));
            }

            while (strTemp != null)
            {
                strTemp = fileInput.readLine ();
                StartX.addElement (new Integer (Integer.parseInt (strTemp)));

                eX.addElement (new Integer (Integer.parseInt (strTemp)));

                strTemp = fileInput.readLine ();
                StartY.addElement (new Integer (Integer.parseInt (strTemp)));

                eY.addElement (new Integer (Integer.parseInt (strTemp)));

                strTemp = fileInput.readLine ();
                PatrolX.addElement (new Integer (Integer.parseInt (strTemp)));

                strTemp = fileInput.readLine ();
                PatrolY.addElement (new Integer (Integer.parseInt (strTemp)));

                strTemp = fileInput.readLine ();
                aggroRange.addElement (new Integer (Integer.parseInt (strTemp)));

                strTemp = fileInput.readLine ();
                retreatRange.addElement (new Integer (Integer.parseInt (strTemp)));

                strTemp = fileInput.readLine ();
                Health.addElement (new Integer (Integer.parseInt (strTemp)));

                strTemp = fileInput.readLine ();
                Attack.addElement (new Integer (Integer.parseInt (strTemp)));

                strTemp = fileInput.readLine ();
                Up.addElement (image);
                Up.setElementAt (THIS.getImage (THIS.getDocumentBase (), strTemp), EnemyCount);

                strTemp = fileInput.readLine ();
                Down.addElement (image);
                Down.setElementAt (THIS.getImage (THIS.getDocumentBase (), strTemp), EnemyCount);

                strTemp = fileInput.readLine ();
                Right.addElement (image);
                Right.setElementAt (THIS.getImage (THIS.getDocumentBase (), strTemp), EnemyCount);

                strTemp = fileInput.readLine ();
                Left.addElement (image);
                Left.setElementAt (THIS.getImage (THIS.getDocumentBase (), strTemp), EnemyCount);

                strTemp = fileInput.readLine ();
                Death.addElement (new String (strTemp));

                State.addElement (new Integer (ENEMY_NORMAL));

                ImageState.addElement (new Integer (eDOWN));

                EnemyCount += 1;
            }

            fileInput.close ();

            return true;
        }


        catch (IOException iox)
        {
            return false;
        }
    }


    public void Draw (Graphics gr, Applet THIS)
    {
        int TempState;
        for (int i = 0 ; i < EnemyCount ; i++)
        {
            TempState = Integer.parseInt (State.get (i).toString ());
            if (TempState != ENEMY_DEAD)
            {
                if (Integer.parseInt (ImageState.get (i).toString ()) == eUP)
                    gr.drawImage (Up.get (i), eX.get (i), eY.get (i), this);
                else if (Integer.parseInt (ImageState.get (i).toString ()) == eDOWN)
                    gr.drawImage (Down.get (i), eX.get (i), eY.get (i), this);
                else if (Integer.parseInt (ImageState.get (i).toString ()) == eRIGHT)
                    gr.drawImage (Right.get (i), eX.get (i), eY.get (i), this);
                else if (Integer.parseInt (ImageState.get (i).toString ()) == eLEFT)
                    gr.drawImage (Left.get (i), eX.get (i), eY.get (i), this);
            }
        }
    }


    public void Move ()
    {

    }


    public void Attack ()
    {

    }
}


Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Nope. Vectors store things in an array of objects- there's no way around it.

Your options are to either write a class/method to get and cast a specific object out of a Vector or to write your own Vector class.

Vectors just manage an arary. It's very simple so it wouldn't be too hard to write your own. You could even write one to use a linked list or something, which would be faster for resizing.

Share this post


Link to post
Share on other sites
If it's Java 1.5, you have the syntatic sugar of generics; the casting is done "under the hood" by the compiler.

Why these Vectors? I find them very expensive... I'm sure you can get better performance with another approach to the matter at hand.

Son Of Cain

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!