123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package pathgame.gameplay.menu;
- import me.hammerle.snuviengine.api.KeyBinding;
- import pathgame.gameplay.Gamestate;
- import pathgame.gameplay.Keys;
- import pathgame.gameplay.Level;
- /**
- * Base class of the menus
- *
- * @author julia
- */
- public abstract class BaseMenu
- {
- private final int id;
- private int index = 0;
- public abstract MenuButton[] getOptions();
- private int returnId;
- /**
- * Contructor generating and initializing the menu
- *
- * @param id the id of the desired menu
- */
- public BaseMenu(int id)
- {
- this.id = id;
- }
- /**
- * Returns the index of the active menu button
- *
- * @return the index of the active menu button
- */
- public int getActiveIndex()
- {
- return index;
- }
- /**
- * Returns if this is the OptionMenu
- *
- * @return if this is the OptionMenu
- */
- public boolean isOptionMenu()
- {
- return false;
- }
- /**
- * Updates the menu state every gametick based on user input and returns the
- * id of the current menu
- *
- * @param gamestate the gamestate
- * @param level a level containing the current map and the current player
- *
- * @return the id of the current menu
- */
- public int tick(Gamestate gamestate, Level level)
- {
- returnId = id;
- if(isKeyPressed(Keys.UP_KEY) || isKeyPressed(Keys.CAM_UP_KEY))
- {
- int length = getOptions().length;
- index = ((index - 1) + length) % length;
- }
- else if(isKeyPressed(Keys.DOWN_KEY) || isKeyPressed(Keys.CAM_DOWN_KEY))
- {
- index = (index + 1) % getOptions().length;
- }
- else if(isKeyPressed(Keys.CONFIRM_KEY))
- {
- getOptions()[index].run(gamestate, level);
- }
- if(returnId != id)
- {
- index = 0;
- }
- return returnId;
- }
- /**
- * Sets the id of the current menu
- *
- * @param id the id of the current menu
- *
- */
- public void setReturnId(int id)
- {
- returnId = id;
- }
- /**
- * Resets the index of the current button to the first button
- *
- */
- public void resetIndex()
- {
- index = 0;
- }
- /**
- * Returns if the key of the parameter is pressed
- *
- * @param key the key that is checked
- * @return if the key is pressed
- */
- private boolean isKeyPressed(KeyBinding key)
- {
- return key.getTime() == 1 || (key.getTime() >= 20 && key.getTime() % 5 == 1);
- }
- }
|