Skip to content

Commit de61c81

Browse files
committed
Added projectile collision
Disabled firing while swimming Slowed down bullet speed.
1 parent 26a1029 commit de61c81

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

src/com/redomar/game/entities/Player.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ public void tick() {
6464
if(fireRate > 0) fireRate--;
6565

6666
if (Game.getMouse().getButton() == 1 && fireRate <= 0){
67-
double dx = Game.getMouse().getX() - 480/2;
68-
double dy = Game.getMouse().getY() - 320/2;
69-
double dir = Math.atan2(dy, dx);
70-
shoot(x, y, dir);
71-
fireRate = Small.FIRE_RATE;
67+
if(!swim.isActive(swimType)){
68+
double dx = Game.getMouse().getX() - 480/2;
69+
double dy = Game.getMouse().getY() - 320/2;
70+
double dir = Math.atan2(dy, dx);
71+
shoot(x, y, dir);
72+
fireRate = Small.FIRE_RATE;
73+
}
7274
}
7375

7476
for (int i = 0; i < projectiles.size(); i++) {

src/com/redomar/game/entities/projectiles/Projectile.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.redomar.game.entities.Entity;
66
import com.redomar.game.level.LevelHandler;
7+
import com.redomar.game.level.tiles.Tile;
78

89
public abstract class Projectile extends Entity{
910

@@ -26,6 +27,56 @@ public Projectile(LevelHandler level, int x, int y, double dir) {
2627

2728
protected abstract void move();
2829

30+
public boolean tileCollision(double xa, double ya, int nx, int ny){
31+
int xMin = 0;
32+
int xMax = 7;
33+
int yMin = 0;
34+
int yMax = 7;
35+
36+
for (int x = xMin; x < xMax; x++) {
37+
if (isSolid((int) xa, (int) ya, x, yMin, nx, ny)) {
38+
return true;
39+
}
40+
}
41+
42+
for (int x = xMin; x < xMax; x++) {
43+
if (isSolid((int) xa, (int) ya, x, yMax, nx, ny)) {
44+
return true;
45+
}
46+
}
47+
48+
for (int y = yMin; y < yMax; y++) {
49+
if (isSolid((int) xa, (int) ya, xMin, y, nx, ny)) {
50+
return true;
51+
}
52+
}
53+
54+
for (int y = yMin; y < yMax; y++) {
55+
if (isSolid((int) xa, (int) ya, xMax, y, nx, ny)) {
56+
return true;
57+
}
58+
}
59+
60+
return false;
61+
}
62+
63+
private boolean isSolid(int xa, int ya, int x, int y, int nx, int ny) {
64+
if (level == null) {
65+
return false;
66+
}
67+
68+
Tile lastTile = level.getTile((nx + x) >> 3,
69+
(ny + y) >> 3);
70+
Tile newtTile = level.getTile((nx + x + xa) >> 3, (ny
71+
+ y + ya) >> 3);
72+
73+
if (!lastTile.equals(newtTile) && newtTile.isSolid()) {
74+
return true;
75+
}
76+
77+
return false;
78+
}
79+
2980
public void remove(){
3081
setRemoved(true);
3182
}

src/com/redomar/game/entities/projectiles/Small.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66

77
public class Small extends Projectile{
88

9-
public static final int FIRE_RATE = 12;
9+
public static final int FIRE_RATE = 7;
1010

1111
public Small(LevelHandler level, int x, int y, double dir) {
1212
super(level, x, y, dir);
13-
range = 50 + life.nextInt(5);
13+
range = 125 - life.nextInt(30);
1414
damage = 20;
15-
speed = 3;
15+
speed = 2;
1616

1717
nx = speed * Math.cos(angle);
1818
ny = speed * Math.sin(angle);
1919
}
2020

2121
public void tick() {
22+
if (tileCollision(x, y,(int) nx,(int) ny)) remove();
2223
move();
2324
}
2425

0 commit comments

Comments
 (0)