Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  .isKeyPressed Issue (I think)  (Read 1924 times)
0 Members and 1 Guest are viewing this topic.
Offline happyfella

Senior Newbie


Projects: 1



« Posted 2013-11-15 15:01:39 »

I am running into an issue with keyboard inputs.

Example; In the SelectPlayer state it does not give you the option to press enter. If enter is not pressed then the game proceeds as intended to the IntroMissionBriefing state and load firstScene, but if enter is pressed during the SelectPlayer state then it will proceed to IntroMissionBriefing and load secondScene completely skipping firstScene altogether.

I have also ran into this issue with a state further in the game but it is around 1000 lines of code and didn't want this post to be too big (this issue is localized within a single state so I am unsure if the issue is identical or not, let me know if you want me to post the code for this issue as well).

I have posted all the code I thought would be relevant to finding the cause. The way I have the game coded right now is the only way I knew how to get this to work, but obviously it doesn't. Please critique my code, only way I can learn  Smiley

P.S. if you guys/gals would like me to post a video of what is happening to actually get a visual please let me know.

Main Game Class
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  
package game;


import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends StateBasedGame{
   
   public static final String gameName = "SSC Fizzle";
   public static final int menu = 0;
   public static final int selectPlayer = 1;
   public static final int missionBriefing = 2;
   public static final int selectCrew = 3;
   public static final int launchSite = 4;
   
   public Game(String gameName){
      super(gameName);
      this.addState(new Menu(menu));
      this.addState(new IntroLaunchSite(launchSite));
      this.addState(new SelectPlayer(selectPlayer));
      this.addState(new IntroMissionBriefing(missionBriefing));
      this.addState(new SelectCrew(selectCrew));
   }
   
   public void initStatesList(GameContainer gc) throws SlickException{
      this.getState(menu).init(gc, this);
      this.getState(launchSite).init(gc, this);
      this.getState(selectPlayer).init(gc, this);
      this.getState(missionBriefing).init(gc, this);
      this.getState(selectCrew).init(gc, this);
      this.enterState(menu);
   }

   public static void main(String[] args) {
      AppGameContainer appgc;
      try{
         appgc = new AppGameContainer(new Game(gameName));
         appgc.setDisplayMode(1280, 720, false);
         appgc.start();
      }catch(SlickException e){
         e.printStackTrace();
      }

   }

}


SelectPlayer
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  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
package game;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class SelectPlayer extends BasicGameState{
   
   private boolean firstScene = true;
   private boolean quit = false;
   private boolean oneDescription = false;
   private boolean twoDescription = false;
   private boolean threeDescription = false;
   
   Image backdropOne;
   Image backdropTwo;
   Image blurBackground;
   Image escBackdrop;
   
   public SelectPlayer(int state){
     
   }
   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
     
      backdropOne = new Image("res/BackdropOne.png");
      backdropTwo = new Image("res/BackdropTwo.png");
      blurBackground = new Image("res/IntroBlurOne.png");
      escBackdrop = new Image("res/EscMenu.png");
   }
   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
     
      g.drawImage(blurBackground, 0, 0);
     
      if(firstScene == true){
         
         //Player One - Military Commander
        g.drawImage(backdropOne, 210, 110);
         g.drawImage(Player.playerOne().picture, 223, 120);
         g.drawString(Player.playerOne().name, 390, 125);
         g.drawString(Player.playerOne().position, 390, 150);
         g.drawString(Player.playerOne().traitOne, 390, 175);
         g.drawString(Player.playerOne().traitTwo, 390, 200);
         g.drawString(Player.playerOne().traitThree, 390, 225);
         g.drawString("Background (Q)", 390, 250);
         
         //Player Two - Scientist
        g.drawImage(backdropOne, 210, 300);
         g.drawImage(Player.playerTwo().picture, 223, 310);
         g.drawString(Player.playerTwo().name, 390, 315);
         g.drawString(Player.playerTwo().position, 390, 340);
         g.drawString(Player.playerTwo().traitOne, 390, 365);
         g.drawString(Player.playerTwo().traitTwo, 390, 390);
         g.drawString(Player.playerTwo().traitThree, 390, 415);
         g.drawString("Background (W)", 390, 440);
         
         //Player Three - Pilot
        g.drawImage(backdropOne, 210, 490);
         g.drawImage(Player.playerThree().picture, 223, 500);
         g.drawString(Player.playerThree().name, 390, 505);
         g.drawString(Player.playerThree().position, 390, 530);
         g.drawString(Player.playerThree().traitOne, 390, 555);
         g.drawString(Player.playerThree().traitTwo, 390, 580);
         g.drawString(Player.playerThree().traitThree, 390, 605);
         g.drawString("Background (E)", 390, 630);
         
         if(firstScene == false){
            g.clear();
         }
      }
     
      //Description One render
     if(oneDescription == true){
         g.drawImage(backdropTwo, 670, 160);
         g.drawString(Player.playerOne().history, 680, 180);
         g.drawString("Backspace to Return", 780, 520);
         if(oneDescription == false){
            g.clear();
         }
      }
     
      //Description Two render
     if(twoDescription == true){
         g.drawImage(backdropTwo, 670, 160);
         g.drawString(Player.playerTwo().history, 680, 180);
         g.drawString("Backspace to Return", 780, 520);
         if(twoDescription == false){
            g.clear();
         }
      }
     
      //Description Three render
     if(threeDescription == true){
         g.drawImage(backdropTwo, 670, 160);
         g.drawString(Player.playerThree().history, 680, 180);
         g.drawString("Backspace to Return", 780, 540);
         if(threeDescription == false){
            g.clear();
         }
      }
     
      //Quit Menu render
     if(quit == true){
         g.drawImage(escBackdrop, 0, 0);
         if(quit == false){
            g.clear();
         }
      }
   }
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
      Input userInput = gc.getInput();
     
      //Choose Player One
     if(userInput.isKeyPressed(Input.KEY_1)){
         Player.player = Player.playerOne();
         sbg.enterState(2);
      }
     
      //Choose Player Two
     if(userInput.isKeyPressed(Input.KEY_2)){
         Player.player = Player.playerTwo();
         sbg.enterState(2);
      }
     
      //Choose Player Three
     if(userInput.isKeyPressed(Input.KEY_3)){
         Player.player = Player.playerThree();
         sbg.enterState(2);
      }
     
      //Quit Menu
     if(userInput.isKeyPressed(Input.KEY_ESCAPE)){
         quit = true;
      }
      if(quit == true){
         if(userInput.isKeyPressed(Input.KEY_R)){
            quit = false;
         }
         if(userInput.isKeyPressed(Input.KEY_Q)){
            System.exit(0);
         }
      }
     
      //Description One update
     if(userInput.isKeyPressed(Input.KEY_Q)){
         twoDescription = false;
         threeDescription = false;
         oneDescription = true;
      }
      if(oneDescription == true){
         if(userInput.isKeyPressed(Input.KEY_BACK)){
            oneDescription = false;
         }
      }
     
      //Description Two update
     if(userInput.isKeyPressed(Input.KEY_W)){
         oneDescription = false;
         threeDescription = false;
         twoDescription = true;
      }
      if(twoDescription == true){
         if(userInput.isKeyPressed(Input.KEY_BACK)){
            twoDescription = false;
         }
      }
     
      //Description Three update
     if(userInput.isKeyPressed(Input.KEY_E)){
         oneDescription = false;
         twoDescription = false;
         threeDescription = true;
      }
      if(threeDescription == true){
         if(userInput.isKeyPressed(Input.KEY_BACK)){
            threeDescription = false;
         }
      }
   }
   
   public int getID(){
      return 1;
   }
}


IntroMissionBriefing
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  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
package game;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class IntroMissionBriefing extends BasicGameState{
   
   private boolean firstScene = true;
   private boolean secondScene = false;
   private boolean thirdScene = false;
   private boolean quit = false;
   
   Image blurBackground;
   Image escBackdrop;
   
   public IntroMissionBriefing(int state){
     
   }
   
   public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
     
      blurBackground = new Image("res/IntroBlurOne.png");
      escBackdrop = new Image("res/EscMenu.png");
   }
   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
     
      g.drawImage(blurBackground, 0, 0);
     
      //First Scene of the Mission Briefing render
     if(firstScene == true){
         g.drawString("Explain the mission, going to space to spy on\n"
               + "the **** party. The year is 1933, Sectrete Space Command\n"
               + "Send the SSC Fizzel into space to spy on the Germans\n"
               + "be very detailed to setup the game", 500, 200);
         g.drawString("press enter", 500, 300);
         if(firstScene == false){
            g.clear();
         }
      }
     
      //Second Scene of the Mission Briefing render
     if(secondScene == true){
         g.drawString("More detail for mission briefing if needed", 500, 200);
         g.drawString("press enter", 500, 300);
         if(secondScene == false){
            g.clear();
         }
      }
     
      //Third Scene of the Mission Briefing render
     if(thirdScene == true){
         g.drawString("additional space if needed for Mission Briefing", 500, 200);
         g.drawString("press enter", 500, 300);
         if(thirdScene == false){
            g.clear();
         }
      }
     
      //Quit Menu render
     if(quit == true){
         g.drawImage(escBackdrop, 0, 0);
         if(quit == false){
            g.clear();
         }
      }
   }
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
      Input userInput = gc.getInput();
     
      //Quit Menu update
     if(userInput.isKeyPressed(Input.KEY_ESCAPE)){
         quit = true;
      }
      if(quit == true){
         if(userInput.isKeyPressed(Input.KEY_R)){
            quit = false;
         }
         if(userInput.isKeyPressed(Input.KEY_M)){
            sbg.enterState(0);
         }
         if(userInput.isKeyPressed(Input.KEY_Q)){
            System.exit(0);
         }
      }
     
      //First Scene update
     if(firstScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = true;
         thirdScene = false;
      }
     
      //Second Scene update
     if(secondScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = false;
         thirdScene = true;
      }
     
      //Third Scene update
     if(userInput.isKeyPressed(Input.KEY_ENTER)){
         sbg.enterState(3);
      }
   }
   
   public int getID(){
      return 2;
   }
}
Offline jmguillemette
« Reply #1 - Posted 2013-11-15 15:33:21 »

your issue is likely in this logic..( see below)

In a single pass when enter is pressed..

if its currently the first scene = true

First if block passes.
second scene is now true.

second if block passes
third scene is now true

and the third if block now passes..

Im guessing you didnt want this to work this way.. instead you wanted to if/else so that the first matching check will run but not the others...

j.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
 //First Scene update
     if(firstScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = true;
         thirdScene = false;
      }
     
      //Second Scene update
     if(secondScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = false;
         thirdScene = true;
      }
     
      //Third Scene update
     if(userInput.isKeyPressed(Input.KEY_ENTER)){
         sbg.enterState(3);
      }



-=Like a post.. give the author a medal!=-
Offline happyfella

Senior Newbie


Projects: 1



« Reply #2 - Posted 2013-11-15 15:48:02 »

Actually the logic you described is exactly how I intended it to be. This is an intro sequence that has a bit of text that I spread out over 3 key strokes, this way the player doesn't have a short story to read on a single page.

The if/else will display one or the other, I am looking for a sequence from first to second then to third.

Thank you for your help, my experience level is quite low so your response could contain merit that I have missed  Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jmguillemette
« Reply #3 - Posted 2013-11-15 16:08:50 »

I dont believe you are intending for the states to change from one to another instantaneously....
(thus creating the illusion that its skipping to very last state)



-=Like a post.. give the author a medal!=-
Offline happyfella

Senior Newbie


Projects: 1



« Reply #4 - Posted 2013-11-15 16:23:04 »

I am sorry j but I do not follow what you are saying. Let me know if I missed what you said;

The state change from 1(selectPlayer) to 2(missionBriefing) should happen instantaneously, this is where you lost me. If I am in state 1(selectPlayer) and press enter then select the player and it loads state 2(missionBriefing) it will start off at secondScene instead of loading firstScene.

Being that they are different states I am assuming to treat them as if they are different classes. In state 2 I have firstScene, secondScene and thirdScene all set to private so I am unsure how state 1 can access the info and process the firstScene without actually being there yet.
Offline xsvenson
« Reply #5 - Posted 2013-11-15 16:29:39 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
     //First Scene update
     if(firstScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = true;
         thirdScene = false;
      }
     
      //Second Scene update
     if(secondScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = false;
         thirdScene = true;
      }


The first if block puts "secondScene" true, which means it will enter the second if block in the same loop

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Offline jmguillemette
« Reply #6 - Posted 2013-11-15 16:34:03 »

Let go back to the code snippet im highlighting.
In this snippet:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
//First Scene update
     if(firstScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = true;
         thirdScene = false;
      }
     
      //Second Scene update
     if(secondScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = false;
         thirdScene = true;
      }
     
      //Third Scene update
     if(userInput.isKeyPressed(Input.KEY_ENTER)){
         sbg.enterState(3);
      }


You are just changing boolean variables. Nothing is actually loading a new scene or changing the game state YET...
All of this logic is in line with one another meaning it will all be executed before what ever happens next happens.
So effectively this is what you did...

int stage = 0;

stage=1;
stage=2;
stage=3;

ok now lets get the stage value and load the stage
Stage stage = getStage(stage);
stage.draw();

by the time you get to your processing logic .. youve changed the value your keying off of 3 times...
(and not once per iteration which is what i believe your expecting)


-=Like a post.. give the author a medal!=-
Offline jmguillemette
« Reply #7 - Posted 2013-11-15 16:35:17 »

xsven is exactly correct.
 Grin

-=Like a post.. give the author a medal!=-
Offline happyfella

Senior Newbie


Projects: 1



« Reply #8 - Posted 2013-11-15 16:38:18 »

xsven is exactly correct.
 Grin

Thank you both for your help, a bit over my head at the current moment but that's why I asked so I can learn.  Smiley

How would you recommend I get around this, I unfortunately do not see an alternative. If enter is not pressed in state 1(plaerSelect) then it progresses as intended, firstScene loads every time.
Offline jmguillemette
« Reply #9 - Posted 2013-11-15 16:52:52 »

here try this :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
 if(firstScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = true;
         thirdScene = false;
      }else   if(secondScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){ // second scene
        firstScene = false;
         secondScene = false;
         thirdScene = true;
      }else if(thirdScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){ // third scene
        sbg.enterState(3);
      }

-=Like a post.. give the author a medal!=-
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline happyfella

Senior Newbie


Projects: 1



« Reply #10 - Posted 2013-11-15 17:04:22 »

Same result j.  Sad

Would you guys recommend I avoid using booleans altogether? I can't think of another way of doing it though. From what I took from xsvenson, it seems using bool is a futile approach.
Offline jmguillemette
« Reply #11 - Posted 2013-11-15 17:41:43 »

just out of curiousity.. is this the only place you actually change the state?

 //Third Scene update
      if(userInput.isKeyPressed(Input.KEY_ENTER)){
         sbg.enterState(3);
      }


if so then i would expect after the last suggestion ..
you would hit enter.. nothing happends
hit enter again nothing happends
and hit it again and it goes to 3rd state.

is this whats occuring ?

-=Like a post.. give the author a medal!=-
Offline xsvenson
« Reply #12 - Posted 2013-11-15 18:02:53 »

Having lots of booleans gets fast out of hand.

I haven't really analyzed Your code but my suggestion is to have Your scenes as different objects.
Your main class will only have 1, the current active scene, also the scene itself knows what to do with the enter key.

Imagine it as a book, You are reading only 1 page, You can go back or forward or pick a arbitary page (via number or whatever). Your main class is the book and Your current scene is the current page etc

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Offline happyfella

Senior Newbie


Projects: 1



« Reply #13 - Posted 2013-11-15 18:48:55 »

just out of curiousity.. is this the only place you actually change the state?

 //Third Scene update
      if(userInput.isKeyPressed(Input.KEY_ENTER)){
         sbg.enterState(3);
      }


if so then i would expect after the last suggestion ..
you would hit enter.. nothing happends
hit enter again nothing happends
and hit it again and it goes to 3rd state.

is this whats occuring ?


That is what has me puzzled, it only skips the firstScene in state 2 no matter how many times I hit enter when in state 1. I really appreciate your effort and help in this, means a lot!  Cheesy


Having lots of booleans gets fast out of hand.

I haven't really analyzed Your code but my suggestion is to have Your scenes as different objects.
Your main class will only have 1, the current active scene, also the scene itself knows what to do with the enter key.

Imagine it as a book, You are reading only 1 page, You can go back or forward or pick a arbitary page (via number or whatever). Your main class is the book and Your current scene is the current page etc

I think I am going to take this approach. From day 1 of this project having so many booleans really had me unsettled, I felt there was a better way to do it. I will take this advice and see what I can do with it, I am sure it will be a bit more challenging having them as objects but it seems to make more sense. Thank you for all your help xsvenson.  Cheesy

Also this is just for giggles, j I recorded this for you so you can see it play out in real time. I know you understand my issue so please don't take this as an insult, and xsvenson, my bad for forgetting your name during the recording.

<a href="http://www.youtube.com/v/sueNMidUOT8?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/sueNMidUOT8?version=3&amp;hl=en_US&amp;start=</a>
Offline jmguillemette
« Reply #14 - Posted 2013-11-15 20:19:55 »

happy ill post some sample code that i have later this evening for a technique that does what your looking to do.
see if this helps you out.

ill be on the IRC channel too.

-=Like a post.. give the author a medal!=-
Offline happyfella

Senior Newbie


Projects: 1



« Reply #15 - Posted 2013-11-15 21:03:05 »

Cool, I look forward to seeing what you have come up with.  Smiley
Offline xsvenson
« Reply #16 - Posted 2013-11-15 22:52:46 »

No offense taken

There are different approaches, some more complex than others, more flexible then other. With experience and some troubleshooting You'll soon figure out what are the pros and cons with whatever You are using and if there are other ways to do it better.

The one I described is basically like the web works (or used to work :/ ), one page at the time. If You need to show something new, just make another page and the page itself knows what to do with all the input.

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Offline jmguillemette
« Reply #17 - Posted 2013-11-15 23:16:29 »

here is my code as promised..
for my own simplicity im pasting in the whole class.

for your use case look at currentScene and queuedScene variables.

and the begining of the start method

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  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
package com.windpoweredgames.mocha;

import static org.lwjgl.opengl.GL11.glLoadIdentity;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;

import com.windpoweredgames.mocha.camera.Camera;
import com.windpoweredgames.mocha.component.BasicComponentLayer;
import com.windpoweredgames.mocha.component.BasicComponentLayerManager;
import com.windpoweredgames.mocha.component.ComponentLayer;
import com.windpoweredgames.mocha.component.ComponentLayerManager;
import com.windpoweredgames.mocha.display.GameDisplay;
import com.windpoweredgames.mocha.input.BasicCamera;
import com.windpoweredgames.mocha.input.InputController;
import com.windpoweredgames.mocha.input.InputProcessor;
import com.windpoweredgames.mocha.scene.Scene;
import com.windpoweredgames.mocha.test.scene.TestScene;

public class GameEngine {

   private boolean isExitRequested = false;
   private InputProcessor inputProcessor = new InputProcessor();
   private Scene currentScene = new TestScene();
   private Scene queuedScene = null;
   private ComponentLayerManager compLayerMgr = new BasicComponentLayerManager();
   private Camera camera = new BasicCamera();
   
   private static GameEngine instance = new GameEngine();
   
   
   private GameEngine(){
     
   }
   
   public static GameEngine getInstance() {
      return instance;
   }
   
   public void setCamera(Camera camera) {
      this.camera = camera;
   }
   public Camera getCamera() {
      return camera;
   }
   
   
   public ComponentLayerManager getCompLayerMgr() {
      return compLayerMgr;
   }
   
   public void queueScene(Scene scene){
      this.queuedScene = scene;
   }
   
   
   public InputProcessor getInputProcessor() {
      return inputProcessor;
   }
   
   public void setExitRequested(boolean isExitRequested) {
      this.isExitRequested = isExitRequested;
   }
   
   public boolean isExitRequested() {
      return isExitRequested;
   }
   
   
   public void init()throws GameException{
      GameDisplay gameDisp = GameDisplay.getInstance();
      gameDisp.initDisplay();
      gameDisp.init3DMode();
   }
   
   public void start(){
      Display.setVSyncEnabled(true);
     
      while(!Display.isCloseRequested() && !isExitRequested()){
         // check if scene switch is needed
        if(this.queuedScene!=null){
            for(InputController ic : this.currentScene.getInputControllers()){
               inputProcessor.removeInputController(ic);
            }
            this.currentScene.cleanUp(); // perform clean up duties prior to changing scenes
           this.currentScene = this.queuedScene; // change current scene.
           this.currentScene.setup();
            this.queuedScene = null; // remove queued scene.
           for(InputController ic : this.currentScene.getInputControllers()){
               inputProcessor.addInputController(ic);
            }
         }
         GameDisplay.getInstance().init3DMode();
         glLoadIdentity();
         GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer
        // draw stage
       
         camera.positionWorld(); // shift the world
        currentScene.draw();
         camera.positionCamera(); // shift camera
        GameDisplay.getInstance().init2DMode();
         compLayerMgr.draw();
         
         Display.update();
         // update stage
        inputProcessor.processInput();
         currentScene.update();
         compLayerMgr.update();
         
         Display.sync(60);
         
      }
      Display.destroy(); // clean up the display
  }
   
   
}


hope this helps you out.. let me know if you have questions.


-=Like a post.. give the author a medal!=-
Offline happyfella

Senior Newbie


Projects: 1



« Reply #18 - Posted 2013-11-16 01:20:53 »

Thank you both!  Smiley

I will let you know how it goes with your example j, you will hear from me tomorrow when I have time to sit down and work on it.
Offline SHC
« Reply #19 - Posted 2013-11-16 14:05:18 »

here try this :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
 if(firstScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){
         firstScene = false;
         secondScene = true;
         thirdScene = false;
      }else   if(secondScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){ // second scene
        firstScene = false;
         secondScene = false;
         thirdScene = true;
      }else if(thirdScene == true && userInput.isKeyPressed(Input.KEY_ENTER)){ // third scene
        sbg.enterState(3);
      }


Or even better, use an integer instead of booleans for states like this.

1  
2  
3  
4  
5  
6  
7  
8  
9  
// The state ID
int stateID = 0;

// Whenever you change the state, do this.
if (userInput.isKeyPressed(Input.KEY_ENTER))
{
    stateID++;
    enterState(stateID);
}

This way, you can reduce the code and make it even more readable.

Offline happyfella

Senior Newbie


Projects: 1



« Reply #20 - Posted 2013-11-17 01:45:47 »

Man was xsvenson right or what. I didn't realize there are so many different approaches to solve this issue, and I am sure there are more then what was presented here.

Thank you SHC for you input and idea, looks like I have some work to do  Smiley

I will be taking all these ideas and working them in this upcoming week, once I have found the solution I will report back to show you guys.
Offline happyfella

Senior Newbie


Projects: 1



« Reply #21 - Posted 2013-11-18 19:06:09 »

Here is the solution I have come up with so far that actually works flawlessly. What do you think?

1  
2  
3  
if(userInput.isKeyPressed(Input.KEY_ENTER)){
         userInput.clearKeyPressedRecord();
      }


I also added it to every nonessential if statement that just progresses the story line but doesn't contribute to data collection. I did a test where a variable is updated dependent on the user input and it worked while still not blocking or skipping the progression of the story line.

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  
//Description One update
     if(userInput.isKeyPressed(Input.KEY_Q)){
         userInput.clearKeyPressedRecord();
         twoDescription = false;
         threeDescription = false;
         oneDescription = true;
      }
      if(oneDescription == true){
         if(userInput.isKeyPressed(Input.KEY_BACK)){
            oneDescription = false;
         }
      }
     
      //Description Two update
     if(userInput.isKeyPressed(Input.KEY_W)){
         userInput.clearKeyPressedRecord();
         oneDescription = false;
         threeDescription = false;
         twoDescription = true;
      }
      if(twoDescription == true){
         if(userInput.isKeyPressed(Input.KEY_BACK)){
            twoDescription = false;
         }
      }
     
      //Description Three update
     if(userInput.isKeyPressed(Input.KEY_E)){
         userInput.clearKeyPressedRecord();
         oneDescription = false;
         twoDescription = false;
         threeDescription = true;
      }
      if(threeDescription == true){
         if(userInput.isKeyPressed(Input.KEY_BACK)){
            threeDescription = false;
         }
      }


This is a quick fix that allows me to keep most the code I have already written. You all have shown me different approaches and I value them greatly. Thank you for the help guys!   Cheesy
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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!