source:
last-defense/main/Location.java@
fb4fc67
Last change on this file since fb4fc67 was fb4fc67, checked in by , 6 years ago | |
---|---|
|
|
File size: 1.3 KB |
Line | |
---|---|
1 | package main; |
2 | |
3 | public class Location { |
4 | private double x; |
5 | private double y; |
6 | |
7 | public Location(final double x, final double y) { |
8 | this.x = x; |
9 | this.y = y; |
10 | } |
11 | |
12 | public Location(final Location copy) { |
13 | this.x = copy.x; |
14 | this.y = copy.y; |
15 | } |
16 | |
17 | public int getX() { |
18 | return (int)Math.round(this.x); |
19 | } |
20 | |
21 | public int getY() { |
22 | return (int)Math.round(this.y); |
23 | } |
24 | |
25 | public void setLocation(final double x, final double y) { |
26 | this.x = x; |
27 | this.y = y; |
28 | } |
29 | |
30 | public void setLocation(final Location loc) { |
31 | this.x = loc.x; |
32 | this.y = loc.y; |
33 | } |
34 | |
35 | public boolean moveToTarget(final double x, final double y, final double distance) { |
36 | final double totalDist = Math.sqrt(Math.pow(x - this.x, 2.0) + Math.pow(y - this.y, 2.0)); |
37 | double ratio = 1.0; |
38 | boolean arrived = false; |
39 | if (distance < totalDist) { |
40 | ratio = distance / totalDist; |
41 | } else { |
42 | arrived = true; |
43 | } |
44 | this.x += (x - this.x) * ratio; |
45 | this.y += (y - this.y) * ratio; |
46 | return arrived; |
47 | } |
48 | |
49 | public double distance(final Location loc) { |
50 | return Math.sqrt(Math.pow(this.x - loc.x, 2.0) + Math.pow(this.y - loc.y, 2.0)); |
51 | } |
52 | } |
Note:
See TracBrowser
for help on using the repository browser.