Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 1 commit
  • 1 file changed
  • 0 comments
  • 1 contributor
117  src/poomonkeys/common/ExplosionController.java
@@ -2,7 +2,8 @@
2 2
 
3 3
 import java.util.ArrayList;
4 4
 
5  
-public class ExplosionController extends Thread {
  5
+public class ExplosionController extends Thread
  6
+{
6 7
 	ArrayList<ArrayList<Dirt>> explosions = new ArrayList<ArrayList<Dirt>>();
7 8
 	Terrain t;
8 9
 
@@ -10,19 +11,23 @@
10 11
 
11 12
 	static ExplosionController instance = null;
12 13
 
13  
-	public static ExplosionController getInstance() {
14  
-		if (instance == null) {
  14
+	public static ExplosionController getInstance()
  15
+	{
  16
+		if (instance == null)
  17
+		{
15 18
 			instance = new ExplosionController();
16 19
 		}
17 20
 		return instance;
18 21
 	}
19 22
 
20  
-	public void init(Terrain t) {
  23
+	public void init(Terrain t)
  24
+	{
21 25
 		this.t = t;
22 26
 	}
23 27
 
24 28
 	// x and y are relative to the bottom left of the terrain
25  
-	public void explode(float x, float y, float r) {
  29
+	public void explode(float x, float y, float r)
  30
+	{
26 31
 		// Helpful square at click point
27 32
 		Dirt test = new Dirt();
28 33
 		test.x = x;
@@ -35,17 +40,22 @@ public void explode(float x, float y, float r) {
35 40
 		int min_index = (int) ((x - r) / t.segmentWidth) + 1;
36 41
 		int max_index = (int) ((x + r) / t.segmentWidth);
37 42
 
38  
-		if (max_index > t.NUM_POINTS - 1) {
  43
+		if (max_index > t.NUM_POINTS - 1)
  44
+		{
39 45
 			max_index = t.NUM_POINTS - 1;
40 46
 		}
41  
-		if (min_index < 0) {
  47
+		if (min_index < 0)
  48
+		{
42 49
 			min_index = 0;
43 50
 		}
44 51
 
45 52
 		// The actual x value at the min and max indexes
46 53
 		float minX = min_index * t.segmentWidth;
47 54
 		float maxX = max_index * t.segmentWidth;
48  
-
  55
+		System.out.println("min_index "+min_index);
  56
+		System.out.println("max_index "+max_index);
  57
+		System.out.println("minX "+minX);
  58
+		System.out.println("maxX "+maxX);
49 59
 		ArrayList<Dirt> dirt;
50 60
 		float totalDirtVolume = 0;
51 61
 
@@ -54,7 +64,8 @@ public void explode(float x, float y, float r) {
54 64
 
55 65
 		// Assign a portion of the removed dirt volume to each dirt point
56 66
 		float individualDirtVolume = totalDirtVolume / dirt.size();
57  
-		for (int d = 0; d < dirt.size(); d++) {
  67
+		for (int d = 0; d < dirt.size(); d++)
  68
+		{
58 69
 			dirt.get(d).volume = individualDirtVolume;
59 70
 		}
60 71
 
@@ -62,7 +73,8 @@ public void explode(float x, float y, float r) {
62 73
 		explosions.add(dirt);
63 74
 
64 75
 		// Start the animation process if it isn't running
65  
-		if (!this.isAlive()) {
  76
+		if (!this.isAlive())
  77
+		{
66 78
 			this.start();
67 79
 		}
68 80
 	}
@@ -70,11 +82,12 @@ public void explode(float x, float y, float r) {
70 82
 	/*
71 83
 	 * Sets terrain heights to the bottom of the explosion circle
72 84
 	 */
73  
-	private float _collapseTerrain(int min_index, int max_index, float x,
74  
-			float y, float r) {
  85
+	private float _collapseTerrain(int min_index, int max_index, float x, float y, float r)
  86
+	{
75 87
 		float dirtVolume = 0;
76 88
 
77  
-		for (int i = min_index; i <= max_index; i++) {
  89
+		for (int i = min_index; i <= max_index; i++)
  90
+		{
78 91
 			float xFromI = i * t.segmentWidth;
79 92
 			float offset = (float) Math.sqrt(r * r - Math.pow(xFromI - x, 2));
80 93
 			float tCircleY = (float) (offset + y);
@@ -83,8 +96,10 @@ private float _collapseTerrain(int min_index, int max_index, float x,
83 96
 			// checks if the land is above the bottom of the circle, otherwise
84 97
 			// don't mess with it.
85 98
 
86  
-			if (t.points[i] > bCircleY) {
87  
-				if (t.points[i] > tCircleY) {
  99
+			if (t.points[i] > bCircleY)
  100
+			{
  101
+				if (t.points[i] > tCircleY)
  102
+				{
88 103
 					dirtVolume += t.points[i] - tCircleY;
89 104
 				}
90 105
 
@@ -97,26 +112,42 @@ private float _collapseTerrain(int min_index, int max_index, float x,
97 112
 		return dirtVolume;
98 113
 	}
99 114
 
100  
-	private ArrayList<Dirt> _generateDirtpoints(float minX, float maxX,
101  
-			float x, float y, float r) {
  115
+	private ArrayList<Dirt> _generateDirtpoints(float minX, float maxX, float x, float y, float r)
  116
+	{
102 117
 		ArrayList<Dirt> dirt = new ArrayList<Dirt>();
103 118
 
104 119
 		float explosion_width = Math.abs(maxX - minX);
  120
+		System.out.println("Explosion width " + explosion_width);
105 121
 		int num_dirt_columns = (int) ((explosion_width + t.segmentWidth) / (2 * DIRT_SIZE));
  122
+		System.out.println("Dirt columns " + num_dirt_columns);
106 123
 		float gapTotal = (explosion_width + t.segmentWidth) % (2 * DIRT_SIZE);
  124
+		System.out.println("gap total " + gapTotal);
107 125
 		float gap = gapTotal / num_dirt_columns;
108  
-		for (float col_x = minX - t.segmentWidth / 2; col_x <= maxX + t.segmentWidth / 2 + gap / 2; col_x += DIRT_SIZE
109  
-				* 2 + gap) {
110  
-
  126
+		System.out.println("gap " + gap);
  127
+		System.out.println();
  128
+		float col_x = minX - t.segmentWidth/2 + gap/2 + DIRT_SIZE;
  129
+		float end_x = maxX + t.segmentWidth/2 - gap/2 - DIRT_SIZE;
  130
+		float EPSILON = .0001f;
  131
+		for (; col_x <= end_x + EPSILON; col_x += DIRT_SIZE * 2 + gap)
  132
+		{
111 133
 			_generateDirtColumn(col_x, x, y, r, gap, dirt);
112 134
 		}
113 135
 
114 136
 		return dirt;
115 137
 	}
116 138
 
117  
-	private float _generateDirtColumn(float col_x, float x, float y, float r,
118  
-			float gap, ArrayList<Dirt> dirt) {
119  
-		float offset = (float) Math.sqrt(r * r - Math.pow(col_x - x, 2));
  139
+	private float _generateDirtColumn(float col_x, float x, float y, float r, float gap, ArrayList<Dirt> dirt)
  140
+	{
  141
+		float thing = (float) (r * r - Math.pow(col_x - x, 2));
  142
+		float offset = 0;
  143
+		if(thing > 0)
  144
+		{
  145
+			offset = (float) Math.sqrt(thing);
  146
+		}
  147
+		else
  148
+		{
  149
+			offset = 0;
  150
+		}
120 151
 		float tCircleY = (float) (offset + y);
121 152
 
122 153
 		int iFromX = (int) (col_x / t.segmentWidth);
@@ -125,9 +156,11 @@ private float _generateDirtColumn(float col_x, float x, float y, float r,
125 156
 		float p1y = t.points[iFromX];
126 157
 		float p2y = t.points[iFromX + 1];
127 158
 		float top = p1y + (p2y - p1y) * xPercent;
128  
-		if (top > tCircleY) {
  159
+		if (top > tCircleY)
  160
+		{
129 161
 			float d = 0;
130  
-			while (tCircleY + d <= top) {
  162
+			while (tCircleY + d <= top)
  163
+			{
131 164
 				Dirt dirtPoint = new Dirt();
132 165
 				dirtPoint.x = col_x;
133 166
 				dirtPoint.y = tCircleY + d;
@@ -145,26 +178,30 @@ private float _generateDirtColumn(float col_x, float x, float y, float r,
145 178
 	/**
146 179
 	 * Animates the dirt points and handles adding them back to the terrain
147 180
 	 */
148  
-	public void run() {
149  
-		while (!explosions.isEmpty()) {
150  
-			for (int e = 0; e < explosions.size(); e++) {
  181
+	public void run()
  182
+	{
  183
+		while (!explosions.isEmpty())
  184
+		{
  185
+			for (int e = 0; e < explosions.size(); e++)
  186
+			{
151 187
 				ArrayList<Dirt> dirt = explosions.get(e);
152  
-				for (int i = 0; i < dirt.size(); i++) {
  188
+				for (int i = 0; i < dirt.size(); i++)
  189
+				{
153 190
 					Dirt d = dirt.get(i);
154 191
 					d.vy += -.01;
155 192
 					d.x += d.vx;
156 193
 					d.y += d.vy;
157 194
 
158 195
 					int iFromX = (int) (d.x / t.segmentWidth);
159  
-					double percent = 1 - (d.x % t.segmentWidth)
160  
-							/ t.segmentWidth;
161  
-					double landYatX = t.points[iFromX]
162  
-							+ (t.points[iFromX + 1] - t.points[iFromX])
163  
-							* percent;
164  
-					if (d.y < landYatX) {
165  
-						if (t.points[iFromX + 1] < t.points[iFromX]) {
  196
+					double percent = 1 - (d.x % t.segmentWidth) / t.segmentWidth;
  197
+					double landYatX = t.points[iFromX] + (t.points[iFromX + 1] - t.points[iFromX]) * percent;
  198
+					if (d.y < landYatX)
  199
+					{
  200
+						if (t.points[iFromX + 1] < t.points[iFromX])
  201
+						{
166 202
 							t.points[iFromX + 1] += d.volume;
167  
-						} else {
  203
+						} else
  204
+						{
168 205
 							t.points[iFromX] += d.volume;
169 206
 						}
170 207
 						t.unregisterDrawable(d);
@@ -177,9 +214,11 @@ public void run() {
177 214
 
178 215
 			t.buildGeometry(t.width, t.height);
179 216
 			t.finalizeGeometry();
180  
-			try {
  217
+			try
  218
+			{
181 219
 				Thread.currentThread().sleep(159999);
182  
-			} catch (InterruptedException e) {
  220
+			} catch (InterruptedException e)
  221
+			{
183 222
 				e.printStackTrace();
184 223
 			}
185 224
 		}

No commit comments for this range

Something went wrong with that request. Please try again.