Skip to content

Commit 65721c6

Browse files
committed
primeira versao
1 parent f212b46 commit 65721c6

File tree

13 files changed

+177
-57
lines changed

13 files changed

+177
-57
lines changed

res/sfx/paddle.wav

42.1 KB
Binary file not shown.

res/sfx/score.wav

110 KB
Binary file not shown.

res/sfx/wall.wav

22.9 KB
Binary file not shown.

src/main/java/com/gustavolr/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.gustavolr.game_scenes.MainScene;
55

66
public final class Main {
7+
78
public static void main( String[] args ) {
89
GameEngine gameEngine = new GameEngine();
910

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.awt.Graphics;
44
import java.util.ArrayList;
55
import java.util.List;
6+
import java.util.Random;
67

78
import com.gustavolr.engine.game_loop.GameLoop;
89
import com.gustavolr.engine.game_loop.GameLoopListener;
@@ -17,6 +18,8 @@ public final class GameEngine implements GameLoopListener{
1718

1819
private final List<Scene> scene_manager;
1920

21+
public static Random rng = new Random();
22+
2023
public GameEngine() {
2124

2225
this.scene_manager = new ArrayList<>();

src/main/java/com/gustavolr/engine/entity/Entity.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ public abstract class Entity {
88
protected Vector position;
99
protected int width;
1010
protected int height;
11-
int collision_timer = 0;
12-
int max_timer = 60;
1311

1412
public Entity(Vector position, int width, int height) {
1513
this.position = position;
@@ -37,27 +35,19 @@ public void setHeight(int height) {
3735
}
3836

3937
public void update() {
40-
if(collision_timer < max_timer) {
41-
collision_timer++;
42-
}
43-
System.out.println("collision " + collision_timer);
4438
}
4539

4640
public void render(Graphics g) {
4741

4842
}
4943

5044
public boolean isColidding(Entity e1) {
51-
if (collision_timer < max_timer) {
52-
return false;
53-
}
5445

55-
Rectangle e1Mask = new Rectangle(e1.position.x, e1.position.y, e1.width, e1.height);
56-
Rectangle e2Mask = new Rectangle(this.position.x, this.position.y, this.width, this.height);
46+
47+
Rectangle e1Mask = new Rectangle((int)e1.position.x, (int)e1.position.y, e1.width, e1.height);
48+
Rectangle e2Mask = new Rectangle((int)this.position.x, (int)this.position.y, this.width, this.height);
5749

5850
boolean response = e1Mask.intersects(e2Mask);
59-
if(response)
60-
collision_timer = 0;
6151

6252
return response;
6353
}

src/main/java/com/gustavolr/engine/entity/Vector.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
public class Vector {
44

5-
public int x;
6-
public int y;
5+
public float x;
6+
public float y;
77

88
public Vector() {
99
this.x = 0;
1010
this.y = 0;
1111
}
1212

13-
public Vector(int x, int y) {
13+
public Vector(float x, float y) {
1414
this.x = x;
1515
this.y = y;
1616
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.gustavolr.engine.sound;
2+
3+
import java.io.File;
4+
5+
import javax.sound.sampled.AudioInputStream;
6+
import javax.sound.sampled.AudioSystem;
7+
import javax.sound.sampled.Clip;
8+
9+
public class SoundPlayer {
10+
11+
Clip soundClip;
12+
public String fileName;
13+
14+
boolean soundLoaded;
15+
16+
public SoundPlayer() {
17+
soundLoaded = false;
18+
}
19+
20+
public SoundPlayer(String fileName) {
21+
this.fileName = fileName;
22+
this.loadSFXfromFile(fileName);
23+
}
24+
25+
public void loadSFXfromFile(String fileName) {
26+
27+
File sfxFile = new File(fileName.strip());
28+
29+
try {
30+
31+
AudioInputStream audio = AudioSystem.getAudioInputStream(sfxFile);
32+
this.soundClip = AudioSystem.getClip();
33+
soundClip.open(audio);
34+
35+
this.soundLoaded = true;
36+
} catch (Exception e) {
37+
38+
e.printStackTrace();
39+
soundLoaded = false;
40+
}
41+
}
42+
43+
public void setLoopMode(int loopMode) {
44+
soundClip.loop(loopMode);
45+
}
46+
47+
public void play() {
48+
49+
if (soundLoaded) {
50+
51+
soundClip.setFramePosition(0);
52+
soundClip.start();
53+
} else {
54+
55+
System.err.println("Error! No sound/music was loaded!");
56+
}
57+
}
58+
59+
}

src/main/java/com/gustavolr/engine/ui/Text/Text.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void render(Graphics g) {
5656
g.setFont(labelFont);
5757
metrics = g.getFontMetrics(labelFont);
5858

59-
g.drawString(this.label, this.position.x, this.position.y );
59+
g.drawString(this.label, (int)this.position.x, (int)this.position.y );
6060
}
6161

6262
}

src/main/java/com/gustavolr/game_entities/Ball.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,40 @@
33
import java.awt.Graphics;
44
import java.awt.Color;
55

6+
import com.gustavolr.engine.GameEngine;
67
import com.gustavolr.engine.entity.Entity;
78
import com.gustavolr.engine.entity.Vector;
9+
import com.gustavolr.engine.sound.SoundPlayer;
810
import com.gustavolr.engine.window.GameWindow;
11+
import com.gustavolr.utils.OsUtils;
912

1013
public class Ball extends Entity {
1114

1215
private Color color = Color.WHITE;
1316

1417
public double dx,dy,speed;
1518

19+
private final static SoundPlayer wallSFX = new SoundPlayer(OsUtils.getResource(OsUtils.join("sfx","wall.wav")).getFile());
20+
private final static SoundPlayer paddleSFX = new SoundPlayer(OsUtils.getResource(OsUtils.join("sfx","paddle.wav")).getFile());
21+
1622
public Ball(Vector position, int width, int height) {
1723
super(position, width, height);
18-
speed = 3;
19-
dx = 1;
20-
dy = 1;
24+
speed = 2;
25+
dx = (GameEngine.rng.nextInt(1) == 1 ? -1 : 1) * speed;
26+
dy = 0;
2127
}
2228

2329
@Override
2430
public void update() {
2531

26-
if(position.y < 0 || position.y > GameWindow.getWindowHeight() - this.height)
32+
if((position.y <= 0 && dy < 0) || position.y >= GameWindow.getWindowHeight() - height) {
2733
dy *= -1;
34+
wallSFX.play();
35+
}
2836

2937
position.x += dx;
3038
position.y += dy;
31-
super.update();
39+
3240
}
3341

3442
public void calculateAngleAfterColisionWith(Entity paddle) {
@@ -42,12 +50,15 @@ public void calculateAngleAfterColisionWith(Entity paddle) {
4250
double oldSign = Math.signum(this.dx);
4351
this.dx = newDx * (-1.0 * oldSign);
4452
this.dy = newDy;
53+
54+
speed += 0.1;
55+
paddleSFX.play();
4556
}
4657

4758
@Override
4859
public void render(Graphics g) {
4960
g.setColor(color);
50-
g.fillOval(position.x, position.y, width, height);
61+
g.fillOval((int)position.x, (int)position.y, width, height);
5162
}
5263

5364
}

0 commit comments

Comments
 (0)