-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ground.java
101 lines (84 loc) · 2.82 KB
/
Ground.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
95
96
97
98
99
100
101
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
import java.util.stream.*;
/**
* Write a description of class ground here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Ground extends Actor
{
/**
* Act - do whatever the ground wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
private GreenfootImage myImage;
/** the size of the bounding box of the image*/
private int xSize = 600;
private int ySize = 200;
/** generated list of x and y positions for reference by tanks */
private int[] xPos;
private int[] yPos;
/** */
private double friction = 0.2;
public Ground() {
myImage = new GreenfootImage(xSize,ySize);
// myImage.drawLine(0,0,599,0);
myImage.setColor(Color.GREEN);
myImage.fillPolygon(createLineX(),createLineY(),602);
//myImage.fill();
setImage(myImage);
xPos = createXPos().stream().mapToInt(i -> i).toArray();
yPos = createYPos().stream().mapToInt(i -> i).toArray();
}
public int MathFcn1(int x) {
int A = 12; //+ 4*Math.random();
return 35 + (int)((int)A*Math.sin(0.05*x));
}
public int MathFcn2(int x) {
return 43 + (int)(0.001*Math.pow(x-300,2));
}
public List<Integer> createXPos() {
List<Integer> xPos = new ArrayList<Integer>();
for (int i = 0; i < 600; i++) xPos.add(i);
return xPos;
}
public int[] createLineX() {
//adds the x vals of the corner points at front and end so that
//full polygon is drawn
List<Integer> xPoints = createXPos();
xPoints.add(0,0);
xPoints.add(599);
return xPoints.stream().mapToInt(i -> i).toArray();
}
public List<Integer> createYPos() {
List<Integer> yPos = new ArrayList<Integer>();
for (int i = 0; i < 600; i++) {
if (i < 300) yPos.add(MathFcn1(i));
else yPos.add(MathFcn2(i));
}
return yPos;
}
public int[] createLineY() {
//adds the y vals of the corner points at front and end so that
//full polygon is drawn
List<Integer> yPoints = createYPos();
yPoints.add(0,200);
yPoints.add(200);
return yPoints.stream().mapToInt(i -> i).toArray();
}
public int getRelativeY(int x) {
int rawY = yPos[x];
//System.out.println(rawY);
return getY() - ySize/2 + rawY;
}
public int getShiftRelY(int x,int shift) {
return getRelativeY(x) - shift;
}
public void act()
{
// Add your action code here.
}
public double getFriction() {return friction;}
}