Skip to content

Commit 271e381

Browse files
committed
feat: adicionado componentes da cena principal do jogo
1 parent 81f4a5c commit 271e381

File tree

11 files changed

+307
-40
lines changed

11 files changed

+307
-40
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.gustavolr;
22

33
import com.gustavolr.engine.GameEngine;
4+
import com.gustavolr.game_scenes.MainScene;
45

56
public final class Main
67
{
78
public static void main( String[] args ) {
8-
new GameEngine();
9+
GameEngine gameEngine = new GameEngine();
10+
11+
MainScene ms = new MainScene();
12+
gameEngine.addScene(ms);
13+
14+
gameEngine.start();
915
}
16+
1017
}

src/main/java/com/gustavolr/engine/GameEngine.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,48 @@
11
package com.gustavolr.engine;
22

3-
import java.awt.Color;
43
import java.awt.Graphics;
5-
import java.awt.event.KeyEvent;
4+
import java.util.ArrayList;
5+
import java.util.List;
66

77
import com.gustavolr.engine.game_loop.GameLoop;
88
import com.gustavolr.engine.game_loop.GameLoopListener;
99
import com.gustavolr.engine.input.GameInput;
10+
import com.gustavolr.engine.scene.Scene;
1011
import com.gustavolr.engine.window.GameWindow;
1112

1213
public final class GameEngine implements GameLoopListener{
1314

1415
private final GameWindow window;
1516
private final GameInput input;
1617

17-
int x = 0;
18-
int y = 0;
18+
private final List<Scene> scene_manager;
1919

2020
public GameEngine() {
2121

22+
this.scene_manager = new ArrayList<>();
23+
2224
this.input = new GameInput();
2325

2426
this.window = new GameWindow("Pong");
2527
this.window.addKeyListener(input);
26-
new GameLoop(this);
2728
}
2829

30+
public void addScene(Scene scene) {
31+
this.scene_manager.add(scene);
32+
}
33+
34+
public void start() {
35+
this.window.toggleFrameVisibility(true);
36+
this.window.requestFocus();
37+
new GameLoop(this);
38+
}
39+
2940
@Override
3041
public void update() {
3142

32-
if (GameInput.isKeyPressed(KeyEvent.VK_LEFT)) {
33-
x--;
43+
for(Scene scene : scene_manager) {
44+
scene.update();
3445
}
35-
else if (GameInput.isKeyPressed(KeyEvent.VK_RIGHT)) {
36-
x++;
37-
}
38-
39-
if (GameInput.isKeyPressed(KeyEvent.VK_UP)) {
40-
y--;
41-
} else if (GameInput.isKeyPressed(KeyEvent.VK_DOWN)) {
42-
y++;
43-
}
44-
45-
46-
System.out.println(x);
4746
}
4847

4948
@Override
@@ -57,8 +56,9 @@ public void render() {
5756

5857
// Game Rendering goes here
5958

60-
g.setColor(Color.GREEN);
61-
g.fillRect(x, y, 50, 50);
59+
for(Scene scene : scene_manager) {
60+
scene.render(g);
61+
}
6262

6363
// Game Rendering stops here
6464

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.gustavolr.engine.entity;
2+
3+
import java.awt.Graphics;
4+
5+
public abstract class Entity {
6+
7+
protected Vector position;
8+
protected int width;
9+
protected int height;
10+
11+
public Entity(Vector position, int width, int height) {
12+
this.position = position;
13+
this.width = width;
14+
this.height = height;
15+
}
16+
17+
public Vector getPosition() {
18+
return position;
19+
}
20+
public void setPosition(Vector position) {
21+
this.position = position;
22+
}
23+
public int getWidth() {
24+
return width;
25+
}
26+
public void setWidth(int width) {
27+
this.width = width;
28+
}
29+
public int getHeight() {
30+
return height;
31+
}
32+
public void setHeight(int height) {
33+
this.height = height;
34+
}
35+
36+
public void update() {
37+
38+
}
39+
40+
public void render(Graphics g) {
41+
42+
}
43+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.gustavolr.engine.entity;
2+
3+
public class Vector {
4+
5+
public int x;
6+
public int y;
7+
8+
public Vector() {
9+
this.x = 0;
10+
this.y = 0;
11+
}
12+
13+
public Vector(int x, int y) {
14+
this.x = x;
15+
this.y = y;
16+
}
17+
18+
public Vector add(Vector v) {
19+
Vector tmp = new Vector(this.x, this.y);
20+
21+
tmp.x += v.x;
22+
tmp.y += v.y;
23+
24+
return tmp;
25+
}
26+
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.gustavolr.engine.scene;
2+
3+
import java.awt.Graphics;
4+
5+
public interface Scene {
6+
7+
public void update();
8+
public void render(Graphics g);
9+
}

src/main/java/com/gustavolr/engine/window/GameWindow.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ public final class GameWindow extends Canvas{
2121

2222
public static BufferedImage bufferLayer;
2323

24+
public GameWindow(String frameName) {
25+
this(GameWindowConstants.DEFAULT_WINDOW_WIDTH,
26+
GameWindowConstants.DEFAULT_WINDOW_HEIGHT,
27+
GameWindowConstants.DEFAULT_WINDOW_SCALE,
28+
frameName);
29+
}
30+
31+
public GameWindow(int width, int height, int scale, String frameName) {
32+
GameWindow.width = (short)width;
33+
System.out.println(height);
34+
GameWindow.height = (short)height;
35+
GameWindow.scale = (byte)scale;
36+
37+
this.initWindowFrame(frameName);
38+
}
39+
2440
public static short getWindowWidth() {
2541
return width;
2642
}
@@ -45,22 +61,6 @@ public BufferedImage getBufferedImage() {
4561
return bufferLayer;
4662
}
4763

48-
public GameWindow(String frameName) {
49-
this(GameWindowConstants.DEFAULT_WINDOW_WIDTH,
50-
GameWindowConstants.DEFAULT_WINDOW_HEIGHT,
51-
GameWindowConstants.DEFAULT_WINDOW_SCALE,
52-
frameName);
53-
}
54-
55-
public GameWindow(int width, int height, int scale, String frameName) {
56-
GameWindow.width = (short)width;
57-
System.out.println(height);
58-
GameWindow.height = (short)height;
59-
GameWindow.scale = (byte)scale;
60-
61-
this.initWindowFrame(frameName);
62-
}
63-
6464
public void initWindowFrame(String frameName) {
6565

6666
windowFrame = new JFrame(frameName);
@@ -75,8 +75,6 @@ public void initWindowFrame(String frameName) {
7575

7676
// Centralize the window to the center of the user screen
7777
windowFrame.setLocationRelativeTo(null);
78-
79-
windowFrame.setVisible(true);
8078

8179
bufferLayer = new BufferedImage((int)width,(int)height, BufferedImage.TYPE_INT_RGB);
8280

@@ -85,6 +83,10 @@ public void initWindowFrame(String frameName) {
8583
//windowFrame.setIconImage(icon);
8684
}
8785

86+
public void toggleFrameVisibility(boolean visibility) {
87+
windowFrame.setVisible(visibility);
88+
}
89+
8890
public Graphics getWindowLayer() {
8991
BufferStrategy bs = this.getBufferStrategy();
9092
if ( bs == null ) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.gustavolr.game_entities;
2+
3+
import java.awt.Graphics;
4+
import java.awt.Color;
5+
6+
import com.gustavolr.engine.entity.Entity;
7+
import com.gustavolr.engine.entity.Vector;
8+
import com.gustavolr.listeners.BallListener;
9+
10+
public class Ball extends Entity {
11+
12+
private int speed;
13+
private Color color;
14+
private BallListener listener;
15+
16+
float dx = 1;
17+
float dy = 1;
18+
19+
public Ball(Vector position, int width, int height) {
20+
super(position, width, height);
21+
speed = 1;
22+
}
23+
24+
public void addListener(BallListener b) {
25+
this.listener = b;
26+
}
27+
28+
@Override
29+
public void update() {
30+
31+
position.x += dx * speed;
32+
position.y += dy * speed;
33+
34+
if (listener != null) {
35+
this.listener.ballMoved(position);
36+
}
37+
}
38+
39+
@Override
40+
public void render(Graphics g) {
41+
g.setColor(color);
42+
g.fillOval(position.x, position.y, width, height);
43+
}
44+
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.gustavolr.game_entities;
2+
3+
import java.awt.Graphics;
4+
import java.awt.Color;
5+
6+
import com.gustavolr.engine.entity.Entity;
7+
import com.gustavolr.engine.entity.Vector;
8+
import com.gustavolr.listeners.BallListener;
9+
10+
public class Enemy extends Entity implements BallListener {
11+
12+
private int speed = 1;
13+
private Color color = Color.RED;
14+
15+
public Enemy(Vector position, int width, int height) {
16+
super(position, width, height);
17+
}
18+
19+
@Override
20+
public void render(Graphics g) {
21+
g.setColor(this.color);
22+
g.fillRect(this.position.x, this.position.y, this.width, this.height);
23+
}
24+
25+
@Override
26+
public void ballMoved(Vector ballPosition) {
27+
28+
Vector direction = new Vector();
29+
30+
if (ballPosition.y < position.y) {
31+
direction.y -= speed;
32+
} else {
33+
direction.y += speed;
34+
}
35+
36+
position = position.add(direction);
37+
38+
}
39+
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.gustavolr.game_entities;
2+
3+
import java.awt.Graphics;
4+
import java.awt.event.KeyEvent;
5+
import java.awt.Color;
6+
7+
import com.gustavolr.engine.input.GameInput;
8+
import com.gustavolr.engine.entity.Entity;
9+
import com.gustavolr.engine.entity.Vector;
10+
11+
public class Player extends Entity {
12+
13+
private int speed = 1;
14+
private Color color = Color.WHITE;
15+
16+
public Player(Vector position, int width, int height) {
17+
super(position, width, height);
18+
}
19+
20+
@Override
21+
public void update() {
22+
23+
Vector direction = new Vector();
24+
25+
if (GameInput.isKeyPressed(KeyEvent.VK_UP)) {
26+
direction.y -= speed;
27+
}
28+
else if (GameInput.isKeyPressed(KeyEvent.VK_DOWN)) {
29+
direction.y += speed;
30+
}
31+
32+
this.position = this.position.add(direction);
33+
}
34+
35+
@Override
36+
public void render(Graphics g) {
37+
g.setColor(this.color);
38+
g.fillRect(this.position.x, this.position.y, this.width, this.height);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)