Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)

    Hello again.

    So I'm making a combat system for a game I'm working on.
    The combat is basically like old-school Runescape's, where the 2 participating combatants take turns hitting each other.
    Except, in this game, I'm trying to make it so you can only fight one enemy at a time, no matter what. I'm trying to
    make so if you're fighting a mob and you try to attack another, your action is ignored and you continue your current fight.

    I'd post the code I have, but it is such a mess with all the booleans and other variables being
    in other classes that I'd have to post the whole project itself.

    Here's the sequence I have:

    1. I try to fight an enemy. (Once I'm in their "fight radius" and press space)
    2. They fight me back. (Executes their combat-style method, in this case, non-hostile)
    3. I walk away and try to fight another enemy.
    (I'm still in the other enemy's "chase radius" and they follow me as I try to attack another enemy)
    4. I'm able to fight the other enemy, resulting in a 1 vs 2 fight.
    (The second enemy executes the same non-hostile combat method to fight back)

    I don't want that. This is my goal:

    1. I try to fight an enemy. (Once I'm in their "fight radius" and press space)
    2. They fight me back. (Executes their combat-style method, in this case, non-hostile)
    3. I walk away and try to fight another enemy.
    (I'm still in the other enemy's "chase radius" and they follow me as I try to attack another enemy)
    4. I'm unable to start another fight because I'm already fighting a different mob.
    (The second enemy just ignores as the enemy I attacked first walks up to me and continues to attack until I kill it or run away)

    Is this because of static/non-static methods and values? Instances?
    I tried everything but I cannot get this 1-enemy-per-fight limit to work.

    I know this is a seriously complicated situation, but does anyone know any solutions?
    Let me know if I have to provide more info. O-o


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)

    Do you have at least an idea of which part of your code needs to be tweaked? Seems to me that when you press space to fight there could be a boolean, let's call it isFighting, which determines if you engage the enemy or not. When requirements are met such as killing the opponent or perhaps out running them it goes back to false and you can again attack something.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)

    Ah, yes. I have a feeling it has to do with my boolean "fighting."
    It is set to false to begin with, until I start fighting anything. Then turns back to false when I run away.
    (I didn't add a health system yet, so my only option to turn "fighting" off is to run)

    "fighting" is non-static, which I think is why I'm able to fight more than one enemy.
    I want to make "fighting" static so I'm only fighting one enemy, but then every enemy of
    the same fight-style goes after me if "fighting" is true. So that seems like a bad idea to make it static.

    I think I need a way to tell if there are more than 1 instances of "fighting" being equal to true
    or something along those lines.
    I don't know. That is the closest thing I can think of right now.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)

    Is this because of static/non-static methods and values? Instances?
    Maybe Sunspots.
    So that seems like a bad idea to make it static.
    Correct.
    I think I need a way to tell if there are more than 1 instances of "fighting" being equal to true
    I think you're over thinking it. As KucerakJM said, if a fighter is engaged in a fight, fighting is true. To keep the fighter engaged in the same fight, the object being fought should be identified as an instance variable, and only that opponent can be fought until the fighter disengages, fighting changes to false, and the object being fought is cleared, ready to accept a new opponent.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)

    Quote Originally Posted by GregBrannon View Post
    To keep the fighter engaged in the same fight, the object being fought should be identified as an instance variable
    Ahh, objects, the things I never really knew how to work with.
    I tried to look up some tuts about objects and instances, but nothing really helped much.

    Nooby question time:
    Where would I identify the object? In the mob's nonHostile() method? In the Mob class?

    How would I use the instance of that enemy to check if there are other instances of the same type of mob?

    Would I be using instanceof at all?

    I'm 100% out of ideas for this, but all I can think of is random things like:
    "currentEnemy.add"
    "if (enemy == currentEnemy) fighting = true; etc."
    "if (enemy != currentEnemy) just walk around, etc."

    So, in short, I don't really know what that means.
    I tried using the mob type object as a parameter for the non-hostile method, but I didn't get very far.
    Didn't know what to do.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)

    Ahh, objects, the things I never really knew how to work with.
    Then, 1) Why are you using an OOP language?, or 2) Why don't you learn to use your chosen tool properly?

    Mob, Fighter, Enemy should all be separate classes, perhaps extensions of another basic class that contains attributes common to all game characters. To accomplish what I described, an instance of class Fighter, therefore a Fighter object, would have an instance variable that contains the current object being fought, perhaps named 'opponent', which is an instance of Enemy. While fighter.fighting = true, any other object (enemy, mob, whatever) will be ignored because they are not the same as the current fighter.opponent. (Note that class names are capitalized, instances of a class, or objects, have names that begin with lowercase letters.)

    This is basic OOP stuff that you should know how to do. Take a few months to learn your chosen tool better.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)

    Well... I learned to use it in a different way.
    I solved the problem by adding 2 lists: currentEnemy and otherEnemies.

    If the enemy is the first to be attacked, it gets added to the currentEnemy list. (currentEnemy list can only contain one instance of enemy)
    Otherwise, if the first list size = 1, the enemy gets added to the otherEnemies list.

    If the enemy instance is in the currentEnemy list, it attacks and does its stuff.
    If not, it just walks around.

    EDIT: Got rid of the otherEnemies list, but everything still works. Just added a few more if statements.
    Last edited by Rexoa; March 2nd, 2014 at 07:53 AM.

Similar Threads

  1. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum Java Theory & Questions
    Replies: 4
    Last Post: July 19th, 2013, 09:06 AM
  2. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  3. [SOLVED] Game static enemy question
    By hemla in forum Object Oriented Programming
    Replies: 3
    Last Post: March 15th, 2013, 04:20 AM
  4. Replies: 4
    Last Post: November 15th, 2012, 12:09 AM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM

Tags for this Thread