Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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  
  JavaFX GameLoop  (Read 279 times)
0 Members and 1 Guest are viewing this topic.
Offline macmanmatty

Junior Newbie





« Posted 2017-10-17 15:45:29 »

 I have  an RPG I am writing  in javafx kind of like an RPG / RTS  mix. You have many units  and there are many enemies.  You click on tile to move units , pick up items, pitch tents, launch canoes, ECT.  The tiles are all are  linked to a GridPane of  sub classed buttons that have an event listener tied to each one.  Each tile has  an array list of items that are drawn and  one TileOccupant plant, enemy solider, wall, table that is also drawn. When the Gui needs to be updated  only the tiles that change are drawn.  The tiles are  on maps and each map has  150 x 150 tiles.  Each is 25 px by 25 px in size. So drawing just the tiles I need to is faster.
,
I was able to get one man moving from  tile  to tile using a while loop

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
while(newLocation != currentLocation){
move();



Enemy  enemy= enemyCheck();


if (enemy!=null){
attack(enemy);
}
if (hunger==0){
eat();
}
if (thirst==0){
drink();
}

hunger--;
thirst--;

}


My horrible plan for  the game progressing  was to have all the moving /attacking / action  code called from  the Run() method have each instance of the fighter class (which is the  base class that enemies and soliders extend ) implement the Runnable interface  and when  a new instance of an enemy was created  pick a tile and call  the Run() method and have start moving somewhere in real time and when hit something start attacking it same for soliders only  the the user would tell them where to move through button clicks.  Since each time run was called they would be moving / attacking / eating ECt.  in new thread the units would be moving in real time like an RTS game.


Now I know my plan stinks and has many pitfalls. I know I need some kind of game loop. I think I still need two threads though as having my game  loop running on the JaxaFX applaction thread  is not a good Idea.  I'm doing this whole  game my self  and since I suck at drawing and animation there will not but much of that  just men / enemies moving from tile to tile.  My question is how do I write a proper game loop? Since I'm not animating anything or doing heavy logic what kind of timer do I need for the loop? How do I enter the loop?   I have made a loop but after I enter  the game while loop the map is never drawn on the screen and the game just stays at the start screen.

here is the drawing code liked to the map classes 



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  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
public class MapDraw {
   Stage stage;
   HBox headerPane;
   ScrollPane bottomConsole;
   Label title;
   Console console;
   GridPane mapPane;
   Button [] [] buttonMap;
   StackPane [] [] paneMap;
   int mapCounter;
   int currentMap;
   VBox mainPane;
   Scene scene;
   ScrollPane pane;
   Map []  maps= new Map [4];
   ArrayList <MapButton>  mapChangeButtons = new ArrayList<MapButton>();
   MapDraw(){
      console= new Console();
mainPane= new VBox();
       headerPane=new HBox();
       title= new Label("");
       title.setStyle( "-fx-border-style: none; -fx-border-width: 0; -fx-border-insets: 0; -fx-font-size:34px;");
       headerPane.setStyle("-fx-background-color: #2A7226");
       headerPane.setPrefSize(1200,100);
       headerPane.getChildren().add(title);
       mapPane= new GridPane();
       mapPane.setHgap(0);
       mapPane.setVgap(0);
       mapCounter=0;
   }
   public void reDrawTile( LandSquareTile tile){
      int xLocation=tile.getLocationx();
      int yLocation=tile.getLocationy();
      System.out.println("xlocation" + xLocation);
      System.out.println("ylocation" + yLocation);
      paneMap[xLocation][yLocation]= new StackPane();
      paneMap[xLocation][yLocation].getChildren().add(buttonMap[xLocation][yLocation]);
      String imageSource=tile.getImage();
      if(imageSource==null){
         imageSource="empty.png";
      }
      Image image= new Image(imageSource);
      ImageView imageView= new ImageView(image);
      imageView.setFitHeight(25);
      imageView.setFitWidth(25);
      imageView.setMouseTransparent(true);
      paneMap[xLocation][yLocation].getChildren().add(imageView);
      ArrayList<Things> thingsToDraw= new ArrayList<Things>();
      thingsToDraw.addAll(tile.getThings());
      int numberOfThings=thingsToDraw.size();
      if (numberOfThings>0){
      for (int counter=0; counter<numberOfThings; counter++){
          imageSource=thingsToDraw.get(counter).getImageSource();
         drawOnMap( paneMap[xLocation][yLocation], imageSource,  xLocation,  yLocation);
      }
      }
      mapPane.add(paneMap[xLocation][yLocation], xLocation, yLocation );
      reDrawMap();
   }
   public void reDrawTile( ArrayList<LandSquareTile> tiles){
      int size=tiles.size();
      for (int count=0; count<size; count++){
      int xLocation=tiles.get(count).getLocationx();
      int yLocation=tiles.get(count).getLocationy();
      paneMap[xLocation][yLocation]= new StackPane();
      paneMap[xLocation][yLocation].getChildren().add(buttonMap[xLocation][yLocation]);
      String imageSource=tiles.get(count).getImage();
      if(imageSource==null){
         imageSource="empty.png";
      }
      Image image= new Image(imageSource);
      ImageView imageView= new ImageView(image);
      imageView.setFitHeight(25);
      imageView.setFitWidth(25);
      imageView.setMouseTransparent(true);
      paneMap[xLocation][yLocation].getChildren().add(imageView);
      ArrayList<Things> thingsToDraw= new ArrayList<Things>();
      thingsToDraw.addAll(tiles.get(count).getThings());
      int numberOfThings=thingsToDraw.size();
      if (numberOfThings>0){
      for (int counter=0; counter<numberOfThings; counter++){
          imageSource=thingsToDraw.get(counter).getImageSource();
System.out.println("xlocation" + xLocation);
System.out.println("ylocation" + yLocation);
         drawOnMap( paneMap[xLocation][yLocation], imageSource,  xLocation,  yLocation);
      }
      }
      mapPane.add(paneMap[xLocation][yLocation], xLocation, yLocation );
      }
      reDrawMap();
   }
private   void drawOnMap( StackPane pane, String imageSource, int xLocation, int yLocation){
      Image  image = new Image(imageSource);
      ImageView imageView= new ImageView(image);
      imageView.setMouseTransparent(true);
      double height=imageView.getFitHeight();
      double width=imageView.getFitWidth();
      double newHeight=height;
      double newWidth=width;
      if (height>width){
       newHeight=20;
       newWidth= (height/20)*width;
      }
      else if (height>width){
          newWidth=20;
          newHeight= (width/20)*height;
         }
      else {
          newHeight=20;
          newWidth=20;
      }
      imageView.setFitHeight(newHeight);
      imageView.setFitWidth(newWidth);
      pane.getChildren().add(imageView);
   }
public void addConsoleInfo(String info){
   console.addDisplayInfo(info);
}
   public void drawMap(int xSize, int ySize,  LandSquareTile [] [] map, Button [] [] buttonMap, Stage stage){ /// draws the map of tiles
      this.stage=stage;
         String imageSource="";
      for (int countx=0; countx<xSize; countx++){
         for (int county=0; county<ySize; county++){
            imageSource=map[countx][county].getImage();
            if(imageSource==null){
               imageSource="empty.png";
            }
            Image image= new Image(imageSource);
            ImageView imageView= new ImageView(image);
            imageView.setFitHeight(25);
            imageView.setFitWidth(25);
            imageView.setMouseTransparent(true);
            buttonMap[countx][county].setMinSize(25,25);
            buttonMap[countx][county].setMaxSize(25,25);
            buttonMap[countx][county].setStyle("  -fx-border-style: none; -fx-border-width: 0; -fx-border-insets: 0; -fx-fout-size: 4px");  
            StackPane stackPane= new StackPane();
            stackPane.getChildren().add(buttonMap[countx][county]);
            stackPane.getChildren().add(imageView);
            stackPane.setMaxSize(25,25);
            stackPane.setMinSize(25,25);
            paneMap[countx][county]=stackPane;
         }
      }
      imageSource="";
      this.buttonMap=buttonMap;
      for (int countx=0; countx<xSize; countx++){
         for (int county=0; county<ySize; county++){
            ArrayList<Things> thingsToDraw= new ArrayList<Things>();
            thingsToDraw.addAll(map[countx][county].getThings());
            int numberOfThings=thingsToDraw.size();
            if (numberOfThings>0){
            for (int count=0; count<numberOfThings; count++){
                imageSource=thingsToDraw.get(count).getImageSource();
               drawOnMap( paneMap[countx][county], imageSource,  count,  county);
            }
         }
            mapPane.add(paneMap[countx][county],countx,county);
         }
      }
      displayMap();
   }
private void displayMap(){
      bottomConsole=console.getConsole();
      ScrollPane pane = new ScrollPane(mapPane);
      pane.setPrefSize(1200,1000);
      String mapName=maps[currentMap].getMapName();
      this.title.setText(mapName);
      mainPane.getChildren().add(headerPane);
      mainPane.getChildren().add(pane);
      mainPane.getChildren().add(bottomConsole);
      scene= new Scene (mainPane);
      stage.setScene(scene);
      stage.show();
      System.out.println("here");
      }
private void reDrawMap(){
 pane = new ScrollPane(mapPane);
   pane.setPrefSize(1200,1000);
   stage.setScene(scene);
   stage.show();
   System.out.println("here");
   }
   public  void newMap(Map map, Stage stage){ // makes a new map button and adds it to the header
      maps[mapCounter]=map;
      currentMap=mapCounter;
      int xSize=map.getXSize();
      int ySize=map.getYSize();
      paneMap= new StackPane[xSize][ySize];
      MapButton button= new MapButton(0, mapCounter, null, map);// y location is mapnumber
      String mapName=map.getMapName();
      button.setText(mapName);
      button.setOnAction(new EventHandler <ActionEvent>(){
         @Override
         public void handle (ActionEvent k ){
            MapButton button= (MapButton) k.getSource();
            int mapNumber= button.getYLocation();
            LandSquareTile[] [] map=maps[mapNumber].getMap();
            Button [] [] buttonMap = maps[mapNumber].getButtonMap();
            int xSize=maps[mapNumber].getXSize();
            int ySize=maps[mapNumber].getYSize();
               currentMap=mapNumber;
            drawMap( xSize, ySize, map, buttonMap, stage);
         }});
      mapChangeButtons.add(button);
      headerPane.getChildren().add(button);
      mapCounter++;
console.addDisplayInfo("welcome to the " +mapName+"!");
   }
   private StackPane getNodeFromGridPane(GridPane gridPane, int col, int row) {
       for (Node node : gridPane.getChildren()) {
           if (GridPane.getColumnIndex(node) == col && GridPane.getRowIndex(node) == row) {
               return (StackPane) node;
           }
       }
       return null;
   }
}




and my crappy  game loop that doesn't work the drawing code is called from each fighter function to update the map with the changed tiles.

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  
MainLoop(){
     
   }
   public void setMap(Map map){
      this.map=map;
   }
   
   public void moveLoop(){
   run();
     
   }
     
   public void    run(){

   
   
   
   int times=0;
   
    while(numberOfSoliders!=0){
if( times==0){
   map.drawMap();
   
}
      String action="";
   int size=fightersOnMap.size();
   
       for (int count=0; count<size; count++){
          action=fightersOnMap.get(count).getAction();
          if  (action.equals("move")){
          fightersOnMap.get(count).run();
          }
          if  (action.equals("rest")){
             fightersOnMap.get(count).rest();
             }
          if  (action.equals("attack")){
             fightersOnMap.get(count).attack();
             }
          if  (action.equals("distanceattack")){
             fightersOnMap.get(count).distanceAttack();
             }
          if  (action.equals("tent")){
             fightersOnMap.get(count).pitchTent();
             }
          if  (action.equals("eat")){
             fightersOnMap.get(count).eat();
             }
          if  (action.equals("drink")){
             fightersOnMap.get(count).drink();
             }
          if  (action.equals("pick up")){
             fightersOnMap.get(count).pickUpItem();
             }
          if  (action.equals("throw")){
             fightersOnMap.get(count).throwItem();
             }
         
          if  (action.equals("launchcanoe")){
             ((Soliders) fightersOnMap.get(count)).launchCanoe();
             }
       
       
       times++;
       
       
    }

    }
       
   }
   
   
   
   
      public void addFighter(Enemies  fighter){ // add a new enemy to the game loop
         
         fightersOnMap.add(fighter);
         numberOfEnemies++;
         
      }
public void addFighter(Soliders fighter){ // adds a new solider to the game loop
         
         fightersOnMap.add(fighter);
         numberOfSoliders++;
         System.out.println("ewghwefy");
               
         
      }

public void removeFighter(Enemies fighter){ // removes an an enemy from the game loop
   
   fightersOnMap.remove(fighter);
   numberOfEnemies--;
   
}
public void removeFighter(Soliders fighter){ //removes  a solider from the game loop
   
   fightersOnMap.remove(fighter);
   numberOfSoliders--;
   
   
}
           
     
     
     
   }
   
   

Offline philfrei
« Reply #1 - Posted 2017-10-17 17:14:52 »

I'd like to help, especially since JavaFX is being used. But this is a lot of material to digest (for the amount of time and brain space I have available). Is there a more specific question that can be considered?

music and music apps: http://adonax.com
Offline macmanmatty

Junior Newbie





« Reply #2 - Posted 2017-10-17 17:29:27 »

most of my problems have been solved by properly creating  a new thread to  run my gameloop in.  implementing runnable  and calling the overridden run  method does not make new thread.


you have to do
1  
2  
 Thread thread = new Thread(this);
thread.start();
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!