java - Adapting a command line-driven turn-based rpg to swing's event-driven paradigm -


background:

for past few months, i've been working on turn-based rpg in free time in java. have been programming around year, expertise limited command line applications , object-oriented programming principles learned in elementary cs classes.

development going swimmingly until started adapt game taking input command line using custom-made ui started building using swing. largely because command line-based input lets me stop i'm doing , wait input scanner or in vein, judging threads i've read on here (waiting multiple button inputs in java swing, waiting till button pressed), swing's event-driven nature doesn't allow wait without interrupting edt , freezing gui, bad thing purposes.

the problem:

while swing quite novel me, i've got working on main menu , see how readily applied games data relatively centralized between few objects utilize simple commands user move or attack or have you. problem rpg has anywhere between 2 , 16 active characters in battle scenario, can user-controlled or ai controlled, can use number of different commands (including 16 different skills) , player characters have able access inventory common between of them. suffice say, game heavily menu-driven. example, i'll post method called loop run during battle:

//initializes turn; written function cut down on duplicate code public battledata initializeturn(battledata toinitialize){     if(toinitialize.initializeskill()) {         if(toinitialize.initializetarget(playerparty, playerminions, 1) == 0)             return initializeturn(toinitialize); //recursive call if user cancelled     }                                            //the skill selected.     else {         if(toinitialize.initializetarget(enemyparty, enemyminions, -1) == 0)             return initializeturn(toinitialize); //see above     }     return toinitialize; } 

this method calls 2 functions written in gamecharacter class, initializetarget , initializeskill choose target 2 arrays of passed targets , 1 skill out of character's 2 skill lists. derived gamecharacter class playercharacter , monster classes, calculate return values of methods user input , rudimentary ai, respectively. since have lot of data skills , items stored across different classes , user input behaves differently depending on character getting input, whether they're choosing target or selecting skill @ moment , in scope of skill selection menu, i'm finding difficult formulate algorithm lets me make entire thing event-driven, since events can mean many different things depending on part of menu being looked @ presently.

solutions:

i've read things swingworkers being used handle background tasks, states being used feed input different places , timers being used regularly update graphical components, i'm not familiar enough swing know best route take @ point. i'm more looking general algorithm implement magical solution let me solve problem without changing of code. i'm more willing reshape quite bit of i've written far, don't afraid tell me i'll have redo quite bit if think make program better.

thanks bearing me; if need more context, i'd happy explain or post more code. have posted more start with, of menus mentioned 1-200 lines , more explained copied.

first of all, can make problem easier thinking of gui does nothing while updating screen, opposed command line not doing anything while waiting input. since it's turn based game, there must requirements when turn considered ready computation. typically happens when user has completed input. in case, i've gathered, input orders different game characters. hence, every time user orders (clicks order button game character) there must check similar isreadytocompute(). when method returns true can call computeturn(). below rough representation in code. there's few gameplay related checks need add should give idea of start

skillbutton1.addactionlistener(e -> {     // change cursor "select" type cursor     skill1 = true; });  public class mouse implements mouselistener {     public void mouseclicked(mouseevent e) {          // mouse x y          // find target game character @ x y or if none return          // remember character target          // of skill1 of selected character          // , count order complete selected character          skill1 = false;          // change cursor normal           if (isreadytocompute()) computeturn();     } } 

the overall idea don't need loop because gui has own internal 1 , runs on edt. edt captures , dispatches events handled action , mouse listeners. when using command line wait scanner listens input event, in gui wait aforementioned listeners in turn listen button press or mouse events. more information on how write listeners please refer official oracle tutorials:

https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html

if same you, i'd recommend switch javafx, more rich graphics framework, better option games , sort of successor of swing. http://docs.oracle.com/javase/8/javafx/get-started-tutorial/jfx-overview.htm#jfxst784


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -