-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.java
89 lines (76 loc) · 2.1 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
package com.example.gamepbo;
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()
{
positionX = Math.random() * (600 - 35);
positionY = Math.random() * (400 - 35);
velocityX = 0;
velocityY = 0;
}
public void setImage(Image i)
{
image = i;
width = i.getWidth();
height = i.getHeight();
}
public void setImage(String filename)
{
Image i = new Image(filename);
setImage(i);
}
public void setPosition(double x, double y)
{
positionX = x;
positionY = y;
}
public void setVelocity(double x, double y)
{
velocityX = x;
velocityY = y;
}
public void addVelocity(double x, double y)
{
velocityX += x;
velocityY += y;
}
public void update(double time)
{
//biar mantul
if (positionX + velocityX * time > 0 && positionX + velocityX * time < 600 - width)
positionX += velocityX * time;
else
velocityX = -velocityX;
if (positionY + velocityY * time > 0 && positionY + velocityY * time < 400 - height)
positionY += velocityY * time;
else
velocityY = -velocityY;
}
public void render(GraphicsContext gc)
{
gc.drawImage( image, positionX, positionY );
}
public Rectangle2D getBoundary()
{
return new Rectangle2D(positionX,positionY,width,height);
}
public boolean intersects(Sprite s)
{
return s.getBoundary().intersects( this.getBoundary() );
}
public String toString()
{
return " Position: [" + positionX + "," + positionY + "]"
+ " Velocity: [" + velocityX + "," + velocityY + "]";
}
}