Skip to content

Commit

Permalink
Worked on menu interaction and graphics
Browse files Browse the repository at this point in the history
  • Loading branch information
mbl111 committed Nov 26, 2012
1 parent b7dd199 commit 3c8c7fd
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 19 deletions.
2 changes: 1 addition & 1 deletion GitHubGameJam2012/src/com/mbl111/ggo12/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public Input getInput() {
private void tick() {
input = inputHandle.updateMouseStatus(SCALE);
if (menuStack.getMenuSize() > 0) {
menuStack.tick();
menuStack.tick(input);
} else {
level.tick();
gui.tick(input);
Expand Down
1 change: 1 addition & 0 deletions GitHubGameJam2012/src/com/mbl111/ggo12/gfx/Art.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Art {
public static final Bitmap[][] TILES = load(16, 16, "/tiles.png");
public static final Bitmap[][] FONT = load(8, 8, "/font.png");
public static final Bitmap[][] ITEMS = load(8, 8, "/items.png");
public static final Bitmap[][] GUI = load(4, 4, "/gui.png");

public static Bitmap[][] load(int sx, int sy, int w, int h, String file) {
try {
Expand Down
4 changes: 4 additions & 0 deletions GitHubGameJam2012/src/com/mbl111/ggo12/gfx/Screen.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public void draw(Bitmap bitmap, int x, int y, int data) {
super.draw(bitmap, x - xOffset, y - yOffset, data);
}

public void draw(Bitmap bitmap, int x, int y) {
super.draw(bitmap, x - xOffset, y - yOffset, 0);
}

public void setOffset(int x, int y) {
this.xOffset = x;
this.yOffset = y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mbl111.ggo12.gfx.Font;
import com.mbl111.ggo12.gfx.Screen;
import com.mbl111.ggo12.input.Input;

public class ExceptionMenu extends Menu {

Expand All @@ -11,10 +12,9 @@ public ExceptionMenu(Exception e) {
stack = e.getStackTrace();
}

public void tick() {
super.tick();
System.out.println(aliveTime);
if (aliveTime >= 60 * 10) game.requestClose();
public void tick(Input input) {
super.tick(input);
if (tickCount >= 60 * 10) game.requestClose();
}

public boolean isFullScreen() {
Expand All @@ -24,6 +24,6 @@ public boolean isFullScreen() {
public void render(Screen screen) {
screen.fill(0xFF000000, 0, 0, screen.w, screen.h, 0);
String error = "The Game Broke!!";
Font.draw(error, (screen.w - error.length())/2, (screen.h/2) - 4, 0xFFFFFFFF, screen);
Font.draw(error, (screen.w - error.length()) / 2, (screen.h / 2) - 4, 0xFFFFFFFF, screen);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mbl111.ggo12.Game;
import com.mbl111.ggo12.Util.BoundingBox;
import com.mbl111.ggo12.gfx.Screen;
import com.mbl111.ggo12.gfx.menu.component.Button;
import com.mbl111.ggo12.gfx.menu.component.ClickListener;
import com.mbl111.ggo12.input.Input;

public class InGameGUI {
Expand All @@ -25,10 +27,13 @@ public void tick(Input input) {
game.getCurrentLevel().onClick(input.x, input.y);
}
}
if (input.down.typed) {
game.setMenu(new InventoryMenu(game.getPlayer().getInventory()));
}
}

public void render(Screen screen) {

}

}
37 changes: 37 additions & 0 deletions GitHubGameJam2012/src/com/mbl111/ggo12/gfx/menu/InventoryMenu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.mbl111.ggo12.gfx.menu;

import com.mbl111.ggo12.Game;
import com.mbl111.ggo12.gfx.Font;
import com.mbl111.ggo12.gfx.Screen;
import com.mbl111.ggo12.gfx.menu.component.Button;
import com.mbl111.ggo12.gfx.menu.component.ClickListener;
import com.mbl111.ggo12.input.Input;
import com.mbl111.ggo12.inventory.Inventory;

public class InventoryMenu extends Menu {

private Inventory inventory;

public InventoryMenu(Inventory inventory) {
this.inventory = inventory;
Button play = new Button(Game.GAME_WIDTH /2 - 36, Game.GAME_HEIGHT - 20, 72, 16, 0).setText("Close!");
play.setClickListener(new ClickListener(this) {
public void onClick() {
game.setMenu(null);
}
});
addComponent(play);
}

public void tick(Input input) {
super.tick(input);
}

public boolean isFullScreen() {
return true;
}

public void render(Screen screen) {
super.render(screen);
}
}
52 changes: 43 additions & 9 deletions GitHubGameJam2012/src/com/mbl111/ggo12/gfx/menu/Menu.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,61 @@
package com.mbl111.ggo12.gfx.menu;

import java.util.ArrayList;
import java.util.List;

import com.mbl111.ggo12.Game;
import com.mbl111.ggo12.gfx.Font;
import com.mbl111.ggo12.gfx.Screen;
import com.mbl111.ggo12.gfx.menu.component.MenuComponent;
import com.mbl111.ggo12.input.Input;

public class Menu {

public Game game;
public int aliveTime = 0;
protected Game game;
protected int tickCount;
protected int selectedIndex;
protected List<MenuComponent> components = new ArrayList<MenuComponent>();
private int actionCooldown = 0;
private boolean initialised = false;
public int lastClickedIndex = -1;

public void tick(Input input) {
if (initialised) {
int x = input.x;
int y = input.y;
for (MenuComponent e : components) {
boolean over = e.isOver(x, y);
e.tick(tickCount, input);
if (over && input.b0Released) {
e.click();
break;
}
}
}
}

public void init(Game game) {
this.game = game;
public void render(Screen screen) {
if (initialised) {
for (int i = 0; i < components.size(); i++) {
components.get(i).render(screen);
}
} else {
Font.draw("MENU NOT INITIALISED", 0, 0, 0xFFFFFF, screen);
}
}

public void tick() {
aliveTime++;
public void init(Game game) {
this.game = game;
this.initialised = true;
}

public boolean isFullScreen() {
return false;
return true;
}

public void render(Screen screen) {

public void addComponent(MenuComponent mc) {
this.components.add(mc);
mc.init(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.mbl111.ggo12.Game;
import com.mbl111.ggo12.gfx.Screen;
import com.mbl111.ggo12.input.Input;

public class MenuStack {

Expand All @@ -28,14 +29,14 @@ public void removeMenu(Menu menu) {
menus.remove(menu);
}

public void tick() {
public void tick(Input input) {
int i = menus.size() - 1;
Menu m = menus.get(i);
m.tick();
m.tick(input);
while (!m.isFullScreen() && i >= 0) {
i--;
m = menus.get(i);
m.tick();
m.tick(input);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mbl111.ggo12.gfx.menu.component;

import com.mbl111.ggo12.gfx.Art;
import com.mbl111.ggo12.gfx.Font;
import com.mbl111.ggo12.gfx.Screen;
import com.mbl111.ggo12.input.Input;

public class Button extends MenuComponent {

public Button(int x, int y, int width, int height, int selectedIndex) {
super(x, y, width, height, selectedIndex);
if (height < 12) throw new IllegalArgumentException("Height must be > 12 for buttons!!");
if (height % 4 != 0) throw new IllegalArgumentException("Height must be a multiple of 4 for buttons!!");
}

public void tick(int TickCount, Input input) {
super.tick(TickCount, input);
}

public void render(Screen screen) {
int col = 0xFF333333;
String msg = text;
int yp = h / 4;
int xp = w / 4;
int xib = 0;
int yib = 0;

if (selected) {
col = 0xFFFFFFFF;
xib = 3;
}
// derp
for (int yc = 0; yc < yp; yc++) {
for (int xc = 0; xc < xp; xc++) {
if (xc == 0 && yc == 0) screen.draw(Art.GUI[0 + xib][0 + yib], x + xc * 4, y + yc * 4);
else if (xc == xp - 1 && yc == 0) screen.draw(Art.GUI[2 + xib][0 + yib], x + xc * 4, y + yc * 4);
else if (xc == 0 && yc == yp - 1) screen.draw(Art.GUI[0 + xib][2 + yib], x + xc * 4, y + yc * 4);
else if (xc == xp - 1 && yc == yp - 1) screen.draw(Art.GUI[2 + xib][2 + yib], x + xc * 4, y + yc * 4);
else if (yc == 0) screen.draw(Art.GUI[1 + xib][0 + yib], x + xc * 4, y + yc * 4);
else if (yc == yp - 1) screen.draw(Art.GUI[1 + xib][2 + yib], x + xc * 4, y + yc * 4);
else if (xc == 0) screen.draw(Art.GUI[0 + xib][1 + yib], x + xc * 4, y + yc * 4);
else if (xc == xp - 1) screen.draw(Art.GUI[2 + xib][1 + yib], x + xc * 4, y + yc * 4);
else screen.draw(Art.GUI[1 + xib][1 + yib], x + xc * 4, y + yc * 4);
}
}
Font.draw(msg, (x + w / 2) - ((msg.length()) / 2 * 8) + (msg.length() % 2 == 0 ? 4 : 0), y + 4, col, screen);
}

public Button setText(String text) {
this.text = text;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mbl111.ggo12.gfx.menu.component;

import com.mbl111.ggo12.gfx.menu.Menu;

public abstract class ClickListener {

public Menu menu;

public ClickListener(Menu parent) {
this.menu = parent;
}

public abstract void onClick();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.mbl111.ggo12.gfx.menu.component;

import com.mbl111.ggo12.gfx.Screen;
import com.mbl111.ggo12.gfx.menu.Menu;
import com.mbl111.ggo12.input.Input;

public class MenuComponent {

public String text = "";
public boolean selected;
protected int x, y, w, h, index;
protected Menu menu;
protected int tick = 0;
protected ClickListener clickListener;

public MenuComponent(int x, int y, int w, int h, int selectedIndex) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.index = selectedIndex;
}

public void init(Menu menu) {
this.menu = menu;
}

public void tick(int TickCount, Input input) {
tick++;
}

public void render(Screen screen) {
}

public MenuComponent setText(String text) {
this.text = text;
return this;
}

public void setSize(int width, int height) {
this.w = width;
this.h = height;
}

public boolean isOver(int x2, int y2) {
if (x2 > x && x2 <= x + w + 1 && y2 > y && y2 <= y + h + 1) {
selected = true;
} else {
selected = false;
}
return selected;
}

public void click() {
menu.lastClickedIndex = index;
if (clickListener != null) {
clickListener.onClick();
}
}

public MenuComponent setClickListener(ClickListener clickListener2) {
this.clickListener = clickListener2;
return this;
}
}

0 comments on commit 3c8c7fd

Please sign in to comment.