Skip to content

Commit 227890f

Browse files
committed
added layers and a button in package ui
1 parent b83e4d7 commit 227890f

File tree

7 files changed

+96
-20
lines changed

7 files changed

+96
-20
lines changed

.attach_pid53052

Whitespace-only changes.
2.27 KB
Binary file not shown.

src/JavaGameEngine/Backend/GameWorld.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
import JavaGameEngine.Backend.Input.Input;
44
import JavaGameEngine.Backend.Input.Keys;
55
import JavaGameEngine.Components.Component;
6-
import JavaGameEngine.Components.GameObject;
7-
import JavaGameEngine.Components.Ui.UiComponent;
8-
import JavaGameEngine.msc.Debug;
96
import JavaGameEngine.msc.Vector2;
107

118
import javax.swing.*;
@@ -14,7 +11,9 @@
1411
import java.awt.event.KeyEvent;
1512
import java.awt.event.MouseAdapter;
1613
import java.awt.event.MouseEvent;
17-
import java.awt.geom.AffineTransform;
14+
import java.util.Collections;
15+
import java.util.Comparator;
16+
import java.util.List;
1817

1918
public class GameWorld extends JPanel{
2019
/*
@@ -90,7 +89,15 @@ private void drawUi(Graphics g){
9089

9190
}
9291
private void drawComponents(Graphics g){
93-
for(Component c : ComponentHandler.getObjects()){
92+
List<Component> list = ComponentHandler.getObjects();
93+
Collections.sort(list, new Comparator<Component>() {
94+
@Override
95+
public int compare(Component o1, Component o2) {
96+
return o1.getLayer() - o2.getLayer();
97+
}
98+
});
99+
100+
for(Component c : list){
94101
(c).draw(g);
95102
g.drawString(fps,0,10);
96103
}

src/JavaGameEngine/Components/Component.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public class Component {
2727
Component parent = null; // if component has parent it should update with some of the parents data
2828
LinkedList<Component> components = new LinkedList<>(); // children
2929

30+
int layer = 0;
31+
32+
public int getLayer() {
33+
return layer;
34+
}
35+
36+
public void setLayer(int layer) {
37+
this.layer = layer;
38+
}
39+
3040
private boolean mouseInside = false;
3141

3242
boolean isEnabled = true;
@@ -132,7 +142,12 @@ public void setRotation(Vector2 rotation) {
132142
}
133143

134144
public boolean isEnabled() {
135-
return isEnabled;
145+
if(isParent()){
146+
return isEnabled;
147+
}
148+
else{
149+
return getParent().isEnabled();
150+
}
136151
}
137152
public void setEnabled(boolean enabled) {
138153
isEnabled = enabled;
@@ -211,18 +226,18 @@ public void destroy(){
211226
*/
212227
public void update() {
213228

214-
if(insideComp()&&isEnabled){
229+
if(insideComp()&&isEnabled()){
215230
if(!isMouseInside()){
216231
onMouseEntered();
217232
setMouseInside(true);
218233
}
219234

220235
}
221-
else if (isMouseInside()&&isEnabled){
236+
else if (isMouseInside()&&isEnabled()){
222237
onMouseExit();
223238
setMouseInside(false);
224239
}
225-
if(isMouseInside()&&Input.isMousePressed()&&isEnabled){
240+
if(isMouseInside()&&Input.isMousePressed()&&isEnabled()){
226241
onMousePressed();
227242
if(getParent()!=null) getParent().onMousePressed();
228243
}

src/JavaGameEngine/JavaGameEngine.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,7 @@ private static void startGame(){
6262
@Override
6363
public void run() {
6464
calcThread.Update();
65-
float current = System.nanoTime();
66-
deltaTime = current - previous;
67-
previous = current;
68-
totalElapsed += deltaTime;
6965

70-
if((totalElapsed/1000000000)>1){
71-
GameWorld.fps = String.valueOf(fps);
72-
totalElapsed = 0;
73-
fps = 0;
74-
}
75-
fps++;
7666
}
7767
}, DELAY,DELAY);
7868
Timer timer1 = new Timer();
@@ -83,7 +73,17 @@ public void run() {
8373
// Debug.log(totalElapsed);
8474
GAMEWORLD.repaint();
8575
Toolkit.getDefaultToolkit().sync(); // so it does not lag on linux
76+
float current = System.nanoTime();
77+
deltaTime = current - previous;
78+
previous = current;
79+
totalElapsed += deltaTime;
8680

81+
if((totalElapsed/1000000000)>1){
82+
GameWorld.fps = String.valueOf(fps);
83+
totalElapsed = 0;
84+
fps = 0;
85+
}
86+
fps++;
8787
}
8888
}, DELAY,DELAY);
8989

src/JavaGameEngine/UI/Button.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package JavaGameEngine.UI;
2+
3+
import JavaGameEngine.Components.GameObject;
4+
import JavaGameEngine.msc.Vector2;
5+
6+
import java.awt.*;
7+
8+
public class Button extends GameObject {
9+
10+
String text = "Press me";
11+
12+
public Button(String text){
13+
this.text = text;
14+
init();
15+
}
16+
public Button(){init();}
17+
18+
@Override
19+
public void onMouseEntered() {
20+
super.onMouseEntered();
21+
setLocalScale(getLocalScale().multiply(1.1f));
22+
}
23+
@Override
24+
public void onMouseExit() {
25+
super.onMouseEntered();
26+
setLocalScale(getLocalScale().devide(1.1f));
27+
}
28+
public void init(){
29+
setLocalScale(new Vector2(0,-50));
30+
}
31+
32+
@Override
33+
public void onMousePressed() {
34+
super.onMousePressed();
35+
onPress();
36+
}
37+
38+
public void onPress(){
39+
40+
}
41+
42+
@Override
43+
public void draw(Graphics g) {
44+
super.draw(g);
45+
g.setColor(Color.white);
46+
Graphics2D g2 = (Graphics2D)g;
47+
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
48+
g2.setFont(new Font("minectaft",0,30));
49+
50+
//g2.drawString("This is gona be awesome",70,20);
51+
g2.drawString(this.text, (int) (getPosition().getX()-(g2.getFont().getSize())), (int) getPosition().getY());
52+
}
53+
}

src/Testing/Test.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public static void main(String[] args){
2525
g.setPosition(new Vector2(0,500));
2626
b.setUseGravity(false);
2727
g.addChild(b);
28+
g.setLayer(0);
2829
ComponentHandler.addObject(g);
2930
start();
3031
}
@@ -33,7 +34,7 @@ static class Player extends GameObject{
3334
public Player(){
3435

3536
setColor(Color.CYAN);
36-
37+
setLayer(10);
3738
setTag("player");
3839
PhysicsBody b = new PhysicsBody();
3940
setPosition(new Vector2(400,300));

0 commit comments

Comments
 (0)