Buttons!
This commit is contained in:
parent
10b4a3b455
commit
dc80e82281
8 changed files with 184 additions and 197 deletions
|
|
@ -41,7 +41,7 @@ public class EntityPlayer extends EntityLiving
|
|||
this.health = 20F;
|
||||
this.maxHealth = 20F;
|
||||
this.speed = .6F;
|
||||
this.heldItem = new ItemFist().setCursor();
|
||||
this.heldItem = new ItemFist();
|
||||
}
|
||||
|
||||
public EntityPlayer()
|
||||
|
|
@ -59,6 +59,10 @@ public class EntityPlayer extends EntityLiving
|
|||
@Override
|
||||
public void update(float delta)
|
||||
{
|
||||
if(Mouse.getNativeCursor() == null && this.heldItem != null)
|
||||
{
|
||||
this.heldItem.setCursor();
|
||||
}
|
||||
int motion = (int) (this.speed * delta);
|
||||
int verticalMotion = (int) (motion * motionMultiplier);
|
||||
if (Keyboard.isKeyDown(Keyboard.KEY_A) && this.box.getX() > 0 && !(this.state == State.STUNNED))
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
package com.oitsjustjose.crawlnbrawl;
|
||||
|
||||
import com.oitsjustjose.crawlnbrawl.Entity.Living.EntityMonster;
|
||||
import com.oitsjustjose.crawlnbrawl.Entity.Living.EntityPlayer;
|
||||
import com.oitsjustjose.crawlnbrawl.Manager.AudioManager;
|
||||
import com.oitsjustjose.crawlnbrawl.Manager.SceneManager;
|
||||
import com.oitsjustjose.crawlnbrawl.Manager.ScreenshotManager;
|
||||
import com.oitsjustjose.crawlnbrawl.Scene.Dungeon;
|
||||
import com.oitsjustjose.crawlnbrawl.Scene.GUIButton;
|
||||
import com.oitsjustjose.crawlnbrawl.Scene.Menu;
|
||||
import com.oitsjustjose.crawlnbrawl.Scene.Scene;
|
||||
import com.oitsjustjose.crawlnbrawl.Util.Config;
|
||||
|
|
@ -49,11 +51,10 @@ public class Game
|
|||
|
||||
public void initGame()
|
||||
{
|
||||
com.oitsjustjose.crawlnbrawl.Scene.Menu gameMenu = new com.oitsjustjose.crawlnbrawl.Scene.Menu();
|
||||
gameMenu.addItem("Play", new Dungeon());
|
||||
gameMenu.addSpecial("Exit", Menu.DO_EXIT);
|
||||
|
||||
Scene currScene = gameMenu;
|
||||
Menu menu = new Menu();
|
||||
menu.addButton(new GUIButton((Display.getWidth() / 2) - 64, (Display.getHeight() / 2) - 64, 128, 32, "Play"), new Dungeon());
|
||||
menu.addButton(new GUIButton((Display.getWidth() / 2) - 64, (Display.getHeight() / 2) - 16, 128, 32, "Exit"), null);
|
||||
Scene currScene = menu;
|
||||
|
||||
while (currScene.go())
|
||||
{
|
||||
|
|
@ -61,7 +62,7 @@ public class Game
|
|||
|
||||
if (currScene == null)
|
||||
{
|
||||
currScene = gameMenu;
|
||||
currScene = menu;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,6 @@ public class Item
|
|||
*/
|
||||
public Item setCursor()
|
||||
{
|
||||
|
||||
InputStream stream = ResourceLoader.getResourceAsStream("assets/textures/items/".replace("/", File.separator) + iconString + ".png");
|
||||
BufferedImage cursorImg;
|
||||
try
|
||||
|
|
|
|||
|
|
@ -28,5 +28,4 @@ public class SceneManager
|
|||
{
|
||||
this.currentSceneLevel = s;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,11 +80,6 @@ public class Dungeon extends SceneLevel
|
|||
initScene();
|
||||
}
|
||||
|
||||
// public static Living getInstance()
|
||||
// {
|
||||
// return instance;
|
||||
// }
|
||||
|
||||
public void initFont()
|
||||
{
|
||||
try
|
||||
|
|
@ -173,18 +168,18 @@ public class Dungeon extends SceneLevel
|
|||
}
|
||||
|
||||
Menu menu = new Menu();
|
||||
|
||||
if (player == null || player.getState() == EntityPlayer.State.DEAD)
|
||||
{
|
||||
menu.addItem("Play Again", new Dungeon());
|
||||
menu.addButton(new GUIButton((Display.getWidth() / 2) - 64, (Display.getHeight() / 2) - 64, 128, 32, "Play Again"), new Dungeon());
|
||||
}
|
||||
else
|
||||
{
|
||||
menu.addItem("Resume", this);
|
||||
menu.addButton(new GUIButton((Display.getWidth() / 2) - 64, (Display.getHeight() / 2) - 64, 128, 32, "Resume"), this);
|
||||
}
|
||||
menu.addSpecial("Exit", Menu.DO_EXIT);
|
||||
menu.addButton(new GUIButton((Display.getWidth() / 2) - 64, (Display.getHeight() / 2) - 16, 128, 32, "Exit"), null);
|
||||
|
||||
return menu;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
117
src/com/oitsjustjose/crawlnbrawl/Scene/GUIButton.java
Normal file
117
src/com/oitsjustjose/crawlnbrawl/Scene/GUIButton.java
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
package com.oitsjustjose.crawlnbrawl.Scene;
|
||||
|
||||
import com.oitsjustjose.crawlnbrawl.Manager.AudioManager;
|
||||
import com.oitsjustjose.crawlnbrawl.Util.FileUtils;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.util.Rectangle;
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.TrueTypeFont;
|
||||
import org.newdawn.slick.opengl.TextureImpl;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.FontFormatException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class GUIButton
|
||||
{
|
||||
private String buttonText;
|
||||
private Rectangle box;
|
||||
private Color color;
|
||||
private Color textColor;
|
||||
private TrueTypeFont font;
|
||||
private boolean hovering;
|
||||
|
||||
public GUIButton(int x, int y, int w, int h, String text)
|
||||
{
|
||||
this(x, y, w, h, text, new Color(0, 0, 0), new Color(0.9F, 0.9F, 0.9F));
|
||||
}
|
||||
|
||||
public GUIButton(int x, int y, int w, int h, String text, Color textColor, Color color)
|
||||
{
|
||||
this.box = new Rectangle(x, y, w, h);
|
||||
this.buttonText = text;
|
||||
this.color = color;
|
||||
this.textColor = textColor;
|
||||
initFont();
|
||||
}
|
||||
|
||||
public void initFont()
|
||||
{
|
||||
try
|
||||
{
|
||||
File fontFile = new File("natives/justabit.ttf".replace("/", File.separator)).getAbsoluteFile();
|
||||
if (!fontFile.exists())
|
||||
{
|
||||
System.out.println("Font did not exist; copying...");
|
||||
FileUtils.copyStream(ResourceLoader.getResourceAsStream("assets/justabit.ttf"), new File("natives/justabit.ttf".replace("/", File.separator)));
|
||||
}
|
||||
|
||||
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, fontFile).deriveFont(32F);
|
||||
font = new TrueTypeFont(awtFont, false);
|
||||
}
|
||||
catch (IOException | FontFormatException e)
|
||||
{
|
||||
font = new TrueTypeFont(new Font("Arial", Font.PLAIN, 32), false);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isClicked()
|
||||
{
|
||||
Rectangle mouse = new Rectangle(Mouse.getX(), Display.getHeight() - Mouse.getY(), 1, 1);
|
||||
if (box.intersects(mouse) && Mouse.isButtonDown(0))
|
||||
{
|
||||
AudioManager.getInstance().play("menu_select");
|
||||
// Sleep before processing the action
|
||||
try
|
||||
{
|
||||
Thread.sleep(100);
|
||||
}
|
||||
catch (InterruptedException e)
|
||||
{
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void update()
|
||||
{
|
||||
Rectangle mouse = new Rectangle(Mouse.getX(), Display.getHeight() - Mouse.getY(), 1, 1);
|
||||
this.hovering = box.intersects(mouse);
|
||||
}
|
||||
|
||||
public void draw()
|
||||
{
|
||||
TextureImpl.bindNone();
|
||||
float x = (float) box.getX();
|
||||
float y = (float) box.getY();
|
||||
float w = (float) box.getWidth();
|
||||
float h = (float) box.getHeight();
|
||||
|
||||
if (this.hovering)
|
||||
{
|
||||
GL11.glColor3f(color.r + (0.1F), color.g + (0.1F), color.b + (0.1F));
|
||||
}
|
||||
else
|
||||
{
|
||||
GL11.glColor3f(color.r, color.g, color.b);
|
||||
}
|
||||
GL11.glBegin(GL11.GL_QUADS);
|
||||
GL11.glVertex2f(x, y);
|
||||
GL11.glVertex2f(x + w, y);
|
||||
GL11.glVertex2f(x + w, y + h);
|
||||
GL11.glVertex2f(x, y + h);
|
||||
GL11.glEnd();
|
||||
|
||||
font.drawString(x + (w / 2) - (font.getWidth(buttonText) / 2), y + (h / 2) - (font.getHeight(buttonText) / 2), buttonText, textColor);
|
||||
}
|
||||
|
||||
public Rectangle getHitBox()
|
||||
{
|
||||
return box;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,168 +1,39 @@
|
|||
package com.oitsjustjose.crawlnbrawl.Scene;
|
||||
|
||||
import com.oitsjustjose.crawlnbrawl.Game;
|
||||
import com.oitsjustjose.crawlnbrawl.Manager.AudioManager;
|
||||
import com.oitsjustjose.crawlnbrawl.Tile.TileBackground;
|
||||
import com.oitsjustjose.crawlnbrawl.Util.FileUtils;
|
||||
import com.oitsjustjose.crawlnbrawl.Util.Helper;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
import org.lwjgl.LWJGLException;
|
||||
import org.lwjgl.input.Mouse;
|
||||
import org.lwjgl.opengl.Display;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.newdawn.slick.Color;
|
||||
import org.newdawn.slick.TrueTypeFont;
|
||||
import org.newdawn.slick.opengl.TextureImpl;
|
||||
import org.newdawn.slick.util.ResourceLoader;
|
||||
|
||||
import java.awt.Font;
|
||||
import java.awt.FontFormatException;
|
||||
import java.awt.Toolkit;
|
||||
import java.awt.font.TextAttribute;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
public class Menu extends Scene
|
||||
{
|
||||
public static final int DO_EXIT = 0;
|
||||
private List<Item> items;
|
||||
private int currItem;
|
||||
private long lastChanged;
|
||||
private TileBackground bg;
|
||||
private TrueTypeFont menuFont;
|
||||
private TrueTypeFont selectedFont;
|
||||
private TrueTypeFont titleFont;
|
||||
private LinkedHashMap<GUIButton, Scene> buttons;
|
||||
private Scene nextScene;
|
||||
|
||||
public Menu()
|
||||
{
|
||||
items = new LinkedList<>();
|
||||
lastChanged = Helper.getTime();
|
||||
bg = new TileBackground("menu");
|
||||
HashMap<TextAttribute, Integer> titleAttributes = new HashMap<>();
|
||||
titleAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
|
||||
|
||||
HashMap<TextAttribute, Integer> selectedAttributes = new HashMap<>();
|
||||
selectedAttributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
|
||||
|
||||
try
|
||||
{
|
||||
File font = new File("natives/justabit.ttf".replace("/", File.separator)).getAbsoluteFile();
|
||||
if (!font.exists())
|
||||
{
|
||||
System.out.println("Font did not exist; copying...");
|
||||
FileUtils.copyStream(ResourceLoader.getResourceAsStream("assets/justabit.ttf"), new File("natives/justabit.ttf".replace("/", File.separator)));
|
||||
}
|
||||
|
||||
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(titleAttributes).deriveFont(64F);
|
||||
titleFont = new TrueTypeFont(awtFont, false);
|
||||
|
||||
awtFont = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(selectedAttributes).deriveFont(Font.BOLD).deriveFont(52F);
|
||||
selectedFont = new TrueTypeFont(awtFont, false);
|
||||
|
||||
awtFont = Font.createFont(Font.TRUETYPE_FONT, font).deriveFont(40F);
|
||||
menuFont = new TrueTypeFont(awtFont, false);
|
||||
Mouse.setNativeCursor(null);
|
||||
}
|
||||
catch (IOException | FontFormatException e)
|
||||
catch (LWJGLException e)
|
||||
{
|
||||
titleFont = new TrueTypeFont(new Font("Arial", Font.PLAIN, 64).deriveFont(titleAttributes).deriveFont(64F), false);
|
||||
selectedFont = new TrueTypeFont(new Font("Arial", Font.BOLD, 52).deriveFont(selectedAttributes).deriveFont(Font.BOLD).deriveFont(52F), false);
|
||||
menuFont = new TrueTypeFont(new Font("Arial", Font.PLAIN, 40).deriveFont(40F), false);
|
||||
|
||||
}
|
||||
buttons = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
// reset menu
|
||||
public void clear()
|
||||
public void addButton(GUIButton button, Scene scene)
|
||||
{
|
||||
items.clear();
|
||||
}
|
||||
|
||||
public void addItem(String label, Scene s)
|
||||
{
|
||||
items.add(new Item(label, s));
|
||||
}
|
||||
|
||||
public void addSpecial(String label, int tag)
|
||||
{
|
||||
items.add(new Special(label, tag));
|
||||
}
|
||||
|
||||
public Scene nextScene()
|
||||
{
|
||||
return items.get(currItem).scene;
|
||||
buttons.put(button, scene);
|
||||
}
|
||||
|
||||
public boolean drawFrame(float delta)
|
||||
{
|
||||
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
|
||||
|
||||
bg.draw();
|
||||
|
||||
while (Keyboard.next())
|
||||
{
|
||||
if (Keyboard.getEventKeyState()) // key was pressed
|
||||
{
|
||||
switch (Keyboard.getEventKey())
|
||||
{
|
||||
case Keyboard.KEY_W:
|
||||
AudioManager.getInstance().play("menu_move");
|
||||
currItem--;
|
||||
if (currItem < 0)
|
||||
{
|
||||
currItem += items.size(); // go to end
|
||||
}
|
||||
break;
|
||||
case Keyboard.KEY_UP:
|
||||
AudioManager.getInstance().play("menu_move");
|
||||
currItem--;
|
||||
if (currItem < 0)
|
||||
{
|
||||
currItem += items.size(); // go to end
|
||||
}
|
||||
break;
|
||||
case Keyboard.KEY_S:
|
||||
AudioManager.getInstance().play("menu_move");
|
||||
currItem = (currItem + 1) % items.size();
|
||||
break;
|
||||
case Keyboard.KEY_DOWN:
|
||||
AudioManager.getInstance().play("menu_move");
|
||||
currItem = (currItem + 1) % items.size();
|
||||
break;
|
||||
case Keyboard.KEY_RETURN:
|
||||
AudioManager.getInstance().play("menu_select");
|
||||
Item item = items.get(currItem);
|
||||
if (item instanceof Special)
|
||||
{
|
||||
switch (((Special) item).tag)
|
||||
{
|
||||
case DO_EXIT:
|
||||
exit();
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
case Keyboard.KEY_F2:
|
||||
Game.getInstance().screenshotManager.takeScreenshot();
|
||||
break;
|
||||
case Keyboard.KEY_F11:
|
||||
if (Display.isFullscreen())
|
||||
{
|
||||
Game.getInstance().setDisplayMode(Game.getInstance().config.resolutionWidth, Game.getInstance().config.resolutionHeight, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
Game.getInstance().setDisplayMode(Toolkit.getDefaultToolkit().getScreenSize().width, Toolkit.getDefaultToolkit().getScreenSize().height, true);
|
||||
}
|
||||
Display.setResizable(true);
|
||||
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight());
|
||||
GL11.glMatrixMode(GL11.GL_PROJECTION);
|
||||
GL11.glLoadIdentity();
|
||||
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GL11.glClearColor(0.15F, 0.15F, 0.15F, 0F);
|
||||
// Screen resize handler
|
||||
if (Display.wasResized())
|
||||
{
|
||||
|
|
@ -172,57 +43,58 @@ public class Menu extends Scene
|
|||
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1);
|
||||
}
|
||||
|
||||
float spacing = Display.getHeight() / (items.size() + 4);
|
||||
float offset = 2 * spacing;
|
||||
|
||||
TextureImpl.bindNone();
|
||||
titleFont.drawString((Display.getWidth() / 2) - (titleFont.getWidth("Crawl 'n Brawl") / 2), 64F, "Crawl 'n Brawl", new Color(0, 123, 124));
|
||||
|
||||
for (int i = 0; i < items.size(); i++)
|
||||
{
|
||||
if (i == currItem)
|
||||
{
|
||||
TextureImpl.bindNone();
|
||||
|
||||
selectedFont.drawString(Display.getWidth() / 2 - (selectedFont.getWidth(items.get(i).label) / 2), offset, items.get(i).label, new Color(85, 124, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
TextureImpl.bindNone();
|
||||
menuFont.drawString(Display.getWidth() / 2 - (menuFont.getWidth(items.get(i).label) / 2), offset, items.get(i).label, new Color(74, 91, 37));
|
||||
}
|
||||
|
||||
offset += spacing;
|
||||
}
|
||||
|
||||
// font binds a texture, so let's turn it off..
|
||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
|
||||
|
||||
updateButtons();
|
||||
drawButtons();
|
||||
|
||||
if (Mouse.isButtonDown(0))
|
||||
{
|
||||
for (GUIButton b : buttons.keySet())
|
||||
{
|
||||
if (b.isClicked())
|
||||
{
|
||||
nextScene = buttons.get(b);
|
||||
if (nextScene == null)
|
||||
{
|
||||
System.exit(0);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// a menu item: label and associated Scene to jump to
|
||||
private static class Item
|
||||
public void updateButtons()
|
||||
{
|
||||
public String label;
|
||||
public Scene scene;
|
||||
int buttonCount = buttons.keySet().size();
|
||||
|
||||
public Item(String label, Scene s)
|
||||
for (GUIButton b : buttons.keySet())
|
||||
{
|
||||
this.label = label;
|
||||
this.scene = s;
|
||||
b.update();
|
||||
if (Display.wasResized())
|
||||
{
|
||||
System.out.println("fixing buttons");
|
||||
int x = (Display.getWidth() / 2) - (b.getHitBox().getWidth() / 2);
|
||||
int y = (Display.getHeight() / 2) - (b.getHitBox().getHeight() * buttonCount) - ((buttonCount - buttons.keySet().size()) * 16);
|
||||
b.getHitBox().setLocation(x, y);
|
||||
buttonCount -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class Special extends Item
|
||||
public void drawButtons()
|
||||
{
|
||||
public int tag;
|
||||
|
||||
public Special(String label, int tag)
|
||||
for (GUIButton b : buttons.keySet())
|
||||
{
|
||||
super(label, null);
|
||||
this.tag = tag;
|
||||
b.draw();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Scene nextScene()
|
||||
{
|
||||
return nextScene;
|
||||
}
|
||||
}
|
||||
|
|
@ -117,17 +117,17 @@ public class Surface extends SceneLevel
|
|||
{
|
||||
return new Dungeon();
|
||||
}
|
||||
|
||||
Menu menu = new Menu();
|
||||
if (player == null || player.getState() == EntityPlayer.State.DEAD)
|
||||
{
|
||||
menu.addItem("Play Again", new Dungeon());
|
||||
menu.addButton(new GUIButton((Display.getWidth() / 2) - 64, (Display.getHeight() / 2) - 64, 128, 32, "Play Again"), new Dungeon());
|
||||
}
|
||||
else
|
||||
{
|
||||
menu.addItem("Resume", this);
|
||||
menu.addButton(new GUIButton((Display.getWidth() / 2) - 64, (Display.getHeight() / 2) - 64, 128, 32, "Resume"), this);
|
||||
}
|
||||
menu.addSpecial("Exit", Menu.DO_EXIT);
|
||||
|
||||
menu.addButton(new GUIButton((Display.getWidth() / 2) - 64, (Display.getHeight() / 2) - 16, 128, 32, "Exit"), null);
|
||||
return menu;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue