Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Two parameters mapped to object  (Read 797 times)
0 Members and 1 Guest are viewing this topic.
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Posted 2011-11-19 03:07:55 »

I need a solution for two parameters, both enums, to return me a seperate enum.

This is hard to explain, so let me elaborate by setting up an example.
I got three enumerations: A, B, and C.
Each hold no data, but they are different enums.

I want a solution that can do something like this:
1  
2  
public C(A a, B b) {
   return...


Kind of like a hashmap, but with two mappings. I need this to map animations for a 2d side-scroller.
I have my characters direction, and state.
States would be jumping, standing, walking, running.
Directions would be left, and right.

Feel free to suggest anything that can make this easier. If it includes a different approach - so be it! Currently, this is the best approach I can come up with and I have no idea as to how to solve it.
I want to get the correct animation, from the characters direction, and state.

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-11-19 03:12:16 »

What exactly do you want to accomplish here? Wouldn't it be easier to have a Hashmap that maps Strings to Animations?

Offline counterp

Senior Member


Medals: 11



« Reply #2 - Posted 2011-11-19 03:39:35 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
static Map<Integer, Object> map = new HashMap<Integer, Object>();
static {
 // populate map
}

public Object getDirection(State a, Direction b) {
    return map.get(a.value() | b.value());
}

//values of states: jumping = 1, standing = 2, walking = 4, running = 8.
//values of directions: left = 16, and right = 32.

// it would be better if you used constants like:

final int STATE_JUMPING = 1;
final int STATE_STANDING = 2;
final int DIRECTION_RIGHT = 32;
...

// and you replaced your enums with

int state = STATE_STANDING;
int direction = DIRECTION_RIGHT;


there are so many ways you can accomplish this using numbers
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #3 - Posted 2011-11-19 04:30:30 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
static Map<Integer, Object> map = new HashMap<Integer, Object>();
static {
 // populate map
}

public Object getDirection(State a, Direction b) {
    return map.get(a.value() | b.value());
}

//values of states: jumping = 1, standing = 2, walking = 4, running = 8.
//values of directions: left = 16, and right = 32.

// it would be better if you used constants like:

final int STATE_JUMPING = 1;
final int STATE_STANDING = 2;
final int DIRECTION_RIGHT = 32;
...

// and you replaced your enums with

int state = STATE_STANDING;
int direction = DIRECTION_RIGHT;


there are so many ways you can accomplish this using numbers

This is really nice! I admire your thought! Why only use the numbers representing bits though?

Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-11-19 04:35:07 »

So that OR-ing them would work in getDirection(State,Direction) Wink

Offline counterp

Senior Member


Medals: 11



« Reply #5 - Posted 2011-11-19 08:42:45 »

Yeah. You wouldn't have to make a corresponding number value for each enum, and if the enum class bodies are empty and you're just using them for their names (LEFT, RIGHT, JUMPING, etc), you may as well just use constant integers instead, with the same names (LEFT = 1, RIGHT = 2, JUMPING = 3, etc)
Offline cylab

JGO Ninja


Medals: 38



« Reply #6 - Posted 2011-11-19 10:58:34 »

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  
package net.highteq.randomtests;

import java.util.NoSuchElementException;
import static net.highteq.randomtests.Main.Dir.*;
import static net.highteq.randomtests.Main.State.*;

public class Main
{

    enum Dir
    {
        LEFT, RIGHT;
    }

    enum State
    {
        JUMPING, STANDING, WALKING, RUNNING;
    }
   
    enum Anim
    {
        JUMP_LEFT(JUMPING, LEFT),
        STAND_LEFT(STANDING, LEFT),
        WALK_LEFT(WALKING, LEFT),
        RUN_LEFT(RUNNING, LEFT),
        JUMP_RIGHT(JUMPING, RIGHT),
        STAND_RIGHT(STANDING, RIGHT),
        WALK_RIGHT(WALKING, RIGHT),
        RUN_RIGHT(RUNNING, RIGHT);
       
        private State state;
        private Dir dir;
        Anim(State state, Dir dir)
        {
            this.state=state;
            this.dir=dir;
        }
       
        static Anim get(State state, Dir dir)
        {
            for (Anim anim : Anim.values()) if(anim.state==state && anim.dir==dir) return anim;
            throw new NoSuchElementException(state.toString()+"+"+dir.toString());
        }
    }

    public static void main(String[] args)
    {
        for (State state : State.values()) for (Dir dir : Dir.values())
            System.out.println( "Animation for "+state+"+"+dir+" is "+Anim.get(state,dir) );
    }
}

Mathias - I Know What [you] Did Last Summer!
Offline lhkbob

JGO Knight


Medals: 32



« Reply #7 - Posted 2011-11-19 23:37:43 »

Or use nested maps:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
private class AnimationStore {
   private final Map<State, Map<Dir, Animation>> animations;

   public AnimationStore() {
      animations = new HashMap<State, Map<Dir, Animation>>();
   }

   public Animation getAnimation(State state, Dir dir) {
      Map<Dir, Animation> dirs = animations.get(state);
      if (dirs != null)
          return dirs.get(dir);
      else
          return null;
   }

   public void setAnimation(State state, Dir dir, Animation anim) {
      Map<Dir, Animation> dirs = animations.get(state);
      if (dirs == null) {
          dirs = new HashMap<Dir, Animation>();
          animations.put(state, dirs);
      }
      dirs.put(dir, anim);
   }
}

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.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!