-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.java
94 lines (78 loc) · 2.03 KB
/
Sprite.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
91
92
93
94
package br.com.API4CC;
import javafx.scene.image.Image;
import javafx.scene.canvas.GraphicsContext;
import javafx.geometry.Rectangle2D;
public class Sprite
{
private Image image;
private double positionX;
private double positionY;
private double velocityX;
private double velocityY;
private double width;
private double height;
public Sprite()
{
// posi��o e velocidade inicial
positionX = 0;
positionY = 0;
velocityX = 0;
velocityY = 0;
}
public void setImage(Image i)
{
// define a imagem e o seu tamanho
image = i;
width = i.getWidth();
height = i.getHeight();
}
public void setImage(String filename)
{
// pega o caminho da imagem
Image i = new Image(filename);
setImage(i);
}
public void setPosition(double x, double y)
{
// define a posi��o no jogo
positionX = x;
positionY = y;
}
public void setVelocity(double x, double y)
{
velocityX = x;
velocityY = y;
}
public void addVelocity(double x, double y)
{
// define a velocidade para andar no jogo
velocityX += x;
velocityY += y;
}
public void update(double time)
{
// cria a atualiza��o da posi��o e o tempo
positionX += velocityX * time;
positionY += velocityY * time;
}
public void render(GraphicsContext gc)
{
//cria a imagem
gc.drawImage( image, positionX, positionY );
}
public Rectangle2D getBoundary()
{
// retorna a imagem
return new Rectangle2D(positionX,positionY,width,height);
}
public boolean intersects(Sprite s)
{
// cria a inteseccao entre o personagem e o alvo
return s.getBoundary().intersects( this.getBoundary() );
}
public String toString()
{
return " Position: [" + positionX + "," + positionY + "]"
+ " Velocity: [" + velocityX + "," + velocityY + "]";
}
}