-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathCoin.java
90 lines (76 loc) · 2.41 KB
/
Coin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Coin
{
private double x;
private double y;
private int w = 16;
private int h = 16;
private double SPEED = 0.25;
private double GRAVITY = 0.1;
private BufferedImage goombaImage;
int velocity_x = 0;
double velocity_y = 0;
double old_velocity_y = 0;
//1 going right, -1 for going left
int direction = 1;
public Sprite[] sprites = new Sprite[1];
public Coin(double _x, double _y)
{
this.x = _x;
this.y = _y;
try {
goombaImage = ImageIO.read(new File("Sprites/coin.png"));
} catch (IOException e) {
e.printStackTrace();
}
Sprite s = new Sprite(x, y, w, h, goombaImage);
sprites[0] = s;
}
// Adds the sprites to the GameArena
public void addTo(GameArena arena) {
for (int i = 0; i < sprites.length; i++)
arena.addSprite(sprites[i]);
}
public void move(double dx, double dy, GameArena arena) {
//Move sprite by adding to x and y
x = x + dx;
y = y + dy;
//Move all goomba sprites(idfk how move works i cant lie to you joe is very smart)
for (int i = 0; i < sprites.length; i++)
sprites[i].move(dx, dy);
}
public void update(GameArena arena, Tiles tiles)
{
int stop_x = 0;
int stop_y = 0;
for (int i = 0; i < tiles.tilesSize; i++) {
if (sprites[0].collides(tiles.tiles[i])) {
if (tiles.tiles[i].getXPosition() <= x && tiles.tiles[i].getYPosition() <= y + 16) {
stop_x = -1;
direction = -direction;
}
if (tiles.tiles[i].getXPosition() >= x && tiles.tiles[i].getYPosition() <= y + 16) {
stop_x = 1;
direction = -direction;
}
if (tiles.tiles[i].getYPosition() <= y) {
stop_y = -1;
}
if (tiles.tiles[i].getYPosition() >= y) {
stop_y = 1;
}
}
}
// Gravity
if (y < arena.getHeight() / 3 - 32 - h) {
velocity_y += GRAVITY;
} else {
velocity_y = 0;
}
move(direction * SPEED, velocity_y, arena);
}
}