Skip to content

Commit

Permalink
Merge pull request #4 from TiPaNiMo/UI
Browse files Browse the repository at this point in the history
UI
  • Loading branch information
Momithso authored Jun 21, 2021
2 parents a4e063e + bf52fe7 commit 01cbc32
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 77 deletions.
Binary file added resource/textures/level/noNam3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 52 additions & 7 deletions src/de/game/engine/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.newdawn.slick.*;
import org.newdawn.slick.geom.Circle;

import org.newdawn.slick.geom.Rectangle;
import de.game.levels.LevelController;
import de.game.objects.Ball;
import de.game.ui.UI_Button_Volume;
import java.awt.Font;

public class Main extends BasicGame {

Expand All @@ -19,6 +21,13 @@ public class Main extends BasicGame {
private Input input;
public static Ball PLAYER;

private UI_Button_Volume ui_volume;

private static Rectangle UI_BUTTON = new Rectangle(50, 50, 250, 100);
private static Font BUTTON_FONT = new Font("Calibri", Font.BOLD, 50);
private static TrueTypeFont BUTTON_TEXT;
private static Image STARTBACKGROUND;

@SuppressWarnings("unused")
private LevelController levelController;
@SuppressWarnings("unused")
Expand All @@ -28,7 +37,7 @@ public enum State{
START, GAME, GAME_OVER;
}

private static State state = State.GAME;
private static State state = State.START;

public Main() {
super(windowName);
Expand All @@ -42,6 +51,8 @@ public static void main(String[] args) throws SlickException {

@Override
public void init(GameContainer container) throws SlickException {
container.setShowFPS(false);

input = container.getInput();

PLAYER = new Ball("Player", input, new Circle(0f, 0f, 10f));
Expand All @@ -53,27 +64,44 @@ public void init(GameContainer container) throws SlickException {
* Play background song
*/
SoundController.BACKGROUND.loop(1f, 0.25f);

/**
* Create UI
*/
ui_volume = new UI_Button_Volume(container.getWidth() - 80, 10, container.getInput());
BUTTON_TEXT = new TrueTypeFont(BUTTON_FONT, false);
UI_BUTTON.setCenterX(container.getWidth() / 2);
UI_BUTTON.setCenterY(container.getHeight() / 2 + 50);
STARTBACKGROUND = new Image("resource/textures/level/noNam3.jpg");
}

@Override
public void render(GameContainer container, Graphics g) throws SlickException {


switch(state) {
case START:

g.drawString("TiPaNiMo!", (container.getWidth() / 2) - 32, container.getHeight() / 2);
g.drawImage(STARTBACKGROUND, 0, 0);
g.setColor(new Color(13, 34, 39));
g.fill(UI_BUTTON);
BUTTON_TEXT.drawString(container.getWidth() / 2 - 42, container.getHeight() / 2 + 28, "Play", Color.white);

break;
case GAME:

try {
LevelController.LEVELS.get(LevelController.LEVELINDEX).render(container, g);
} catch(Exception e) {
System.out.println("[ERROR] Level nicht gefunden!");
e.printStackTrace();
//System.out.println("[ERROR] Level nicht gefunden!");
//e.printStackTrace();
state = State.START;
levelController.LEVELINDEX = 0;
}


this.ui_volume.render(container, g);

break;
case GAME_OVER:

Expand All @@ -92,15 +120,32 @@ public void update(GameContainer container, int delta) throws SlickException {
switch(state) {
case START:

if (input.isMousePressed(0)) {
if (UI_BUTTON.contains(input.getMouseX(), input.getMouseY())) {
Main.state = State.GAME;
}
}

break;
case GAME:

this.ui_volume.update(container, delta);

if(input.isMousePressed(0)) {
PLAYER.firstMousePress();

if (this.ui_volume.getUi_button().contains(input.getMouseX(), input.getMouseY())) this.ui_volume.clicked = true;
} else {
this.ui_volume.clicked = false;
}

try {
LevelController.LEVELS.get(LevelController.LEVELINDEX).update(container, delta);
} catch(Exception e) {
System.out.println("[ERROR] Level nicht gefunden!");
e.printStackTrace();
//System.out.println("[ERROR] Level nicht gefunden!");
//e.printStackTrace();
state = State.START;
levelController.LEVELINDEX = 0;
}

break;
Expand Down
100 changes: 30 additions & 70 deletions src/de/game/objects/Ball.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,75 +85,33 @@ public void update(int delta, GameContainer container) {
position.set(this.x, this.y);

/**
* On first mouse press
* On mouse is pressed
*/
if (input.isMousePressed(0)) {
if (input.isMouseButtonDown(0)) {

/**
* Resets level and ball
* Drawing shooting UI
*/
this.reset();
this.calculateShootingUI();

/**
* Save mouse position on first click for placing the ball before release
* Save shooting position to calculate direction
*/
this.pressedMousePosition.set(input.getMouseX(), input.getMouseY());
this.setPosition(this.pressedMousePosition.getX(), this.pressedMousePosition.getY());
}

/**
* On mouse is pressed
*/
if (input.isMouseButtonDown(0)) {
this.shootMousePosition.set(input.getMouseX(), input.getMouseY());
this.lastPosition.set(this.shootMousePosition);

/**
* Check for not placing ball in an object
* Calculating the direction vector to set first direction
*/
for (LevelObject object : LevelController.LEVELS.get(LevelController.LEVELINDEX).getDynamicObjects()) {
if (!object.getShape().contains(this.getShape()) && !object.getShape().intersects(this.getShape())) {
this.collidesOnStart = false;
} else {
this.collidesOnStart = true;
}
}
for (LevelObject object : LevelController.LEVELS.get(LevelController.LEVELINDEX).getStaticObjects()) {
if (!object.getShape().contains(this.getShape()) && !object.getShape().intersects(this.getShape())) {
this.collidesOnStart = false;
} else {
this.collidesOnStart = true;
}
}
this.calculateDirectionVector();

if (!this.collidesOnStart) {

/**
* Drawing shooting UI
*/
this.calculateShootingUI();

/**
* Save shooting position to calculate direction
*/
this.shootMousePosition.set(input.getMouseX(), input.getMouseY());
this.lastPosition.set(this.shootMousePosition);

/**
* Calculating the direction vector to set first direction
*/
this.calculateDirectionVector();

/**
* Sets rendering of balls and shooting UI to true
*/
RENDER = true;
RENDERBALLS = true;
} else {

RENDER = false;
RENDERBALLS = false;
}
/**
* Sets rendering of balls and shooting UI to true
*/
RENDER = true;
RENDERBALLS = true;
} else {

/**
* Check for reflect
*/
Expand All @@ -175,18 +133,6 @@ public void update(int delta, GameContainer container) {
*/
RENDERBALLS = false;
}

/**
* Volume control
*/
if (this.input.isKeyPressed(Input.KEY_UP)) {
container.setMusicVolume(container.getMusicVolume() + 0.1f);
container.setSoundVolume(container.getSoundVolume() + 0.1f);
}
if (this.input.isKeyPressed(Input.KEY_DOWN)) {
container.setMusicVolume(container.getMusicVolume() - 0.1f);
container.setSoundVolume(container.getSoundVolume() - 0.1f);
}
}

/**
Expand Down Expand Up @@ -381,4 +327,18 @@ public void changeLevel() {
public boolean isCollidesOnStart() {
return collidesOnStart;
}

public void firstMousePress() {

/**
* Resets level and ball
*/
this.reset();

/**
* Save mouse position on first click for placing the ball before release
*/
this.pressedMousePosition.set(input.getMouseX(), input.getMouseY());
this.setPosition(this.pressedMousePosition.getX(), this.pressedMousePosition.getY());
}
}
28 changes: 28 additions & 0 deletions src/de/game/ui/UI_Base.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package de.game.ui;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

public interface UI_Base {

/**
* Update the game logic here. No rendering should take place in this method
* though it won't do any harm.
*
* @param container The container holing this game
* @param delta The amount of time thats passed since last update in milliseconds
* @throws SlickException Throw to indicate an internal error
*/
public void update(GameContainer container, int delta) throws SlickException;

/**
* Render the game's screen here.
*
* @param container The container holing this game
* @param g The graphics context that can be used to render. However, normal rendering
* routines can also be used.
* @throws SlickException Throw to indicate a internal error
*/
public void render(GameContainer container, Graphics g) throws SlickException;
}
70 changes: 70 additions & 0 deletions src/de/game/ui/UI_Button.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package de.game.ui;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;

public abstract class UI_Button extends UI_Element {

/** Text Element */
protected UI_Text text;

/** Button Shape */
protected Rectangle ui_button;

/** Button Color */
protected Color ui_color = Color.white;

/** Clicked */
public boolean clicked = false;

/** Rectangle show */
protected boolean ui_show = true;

public UI_Button(float x, float y, Input input, UI_Text text, Rectangle rectangle) {
super(x, y, input);
this.setText(text);
this.text = text;
this.ui_button = rectangle;
}

@Override
public void update(GameContainer container, int delta) throws SlickException {
}

@Override
public void render(GameContainer container, Graphics g) throws SlickException {

this.text.render(container, g);

g.setColor(this.ui_color);
if (ui_show) g.fill(this.ui_button);
}

public Rectangle getUi_button() {
return ui_button;
}

protected void setUi_button(Rectangle ui_button) {
this.ui_button = ui_button;
}

protected UI_Text getText() {
return text;
}

protected void setText(UI_Text text) {
this.text = text;
}

protected boolean isUi_show() {
return ui_show;
}

protected void setUi_show(boolean ui_show) {
this.ui_show = ui_show;
}
}
Loading

0 comments on commit 01cbc32

Please sign in to comment.