Skip to content

Commit 10d114f

Browse files
committed
Merge branch 'master' of git://github.com/frenchie4111/PixelifeJava into frenchie4111-master
Conflicts: PixGrid.java Spawner.java
2 parents 32212b8 + e1f460e commit 10d114f

File tree

4 files changed

+223
-109
lines changed

4 files changed

+223
-109
lines changed

Pix.java

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,75 @@ public void interact(Pix p)
260260

261261
}
262262

263+
/**
264+
* Transitions a Pix from one location to another
265+
* If the Pix is white, the Pix is moved. Otherwise, one Pix interacts with the other
266+
* @param ix inital x location
267+
* @param ix inital y location
268+
* @param x ending x location
269+
* @param y ending y location
270+
*/
271+
public void trans(Pix[][] grid, int ix, int iy, int x, int y)
272+
{
273+
// Didn't try to move out of bounds
274+
if (!(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length))
275+
{
276+
// If trans location is white, move Pix
277+
if (grid[x][y].isWhite())
278+
{
279+
movePixel(grid, ix, iy, x, y);
280+
}
281+
else
282+
{
283+
grid[ix][iy].interact(grid[x][y]);
284+
}
285+
286+
287+
}
288+
}
289+
290+
/**
291+
* Loops through all non-white Pix and transitions them in a random direction (N S E W)
292+
*/
293+
public void update(Pix[][] grid, int i, int j)
294+
{
295+
int direction = (int)(Math.random() * 4);
296+
//System.out.println(direction);
297+
switch(direction)
298+
{
299+
case 0:
300+
trans(grid, i,j,i+1,j);
301+
break;
302+
case 1:
303+
trans(grid, i,j,i,j+1);
304+
break;
305+
case 2:
306+
trans(grid, i,j,i-1,j);
307+
break;
308+
case 3:
309+
trans(grid, i,j,i,j-1);
310+
break;
311+
default:
312+
break;
313+
}
314+
}
315+
316+
/**
317+
* Moves a Pix
318+
* @param ix inital x location
319+
* @param ix inital y location
320+
* @param x ending x location
321+
* @param y ending y location
322+
*/
323+
public void movePixel(Pix[][] grid, int ix, int iy, int x, int y)
324+
{
325+
//grid[x][y] = grid[ix][iy].getClass().newInstance();
326+
Spawner.spawnXY( grid[ix][iy].getClass(), x, y );
327+
grid[x][y].setPix(grid[ix][iy]);
328+
//Uncomment to remove spread
329+
//grid[ix][iy] = new Pix(255,255,255);
330+
}
331+
263332
/**
264333
* Returns RGB integer representation of a Pix
265334
* @return integer RGB of Pix
@@ -298,24 +367,11 @@ public boolean equal(Object o)
298367
if( o instanceof Pix )
299368
{
300369
Pix p = (Pix)o;
301-
if( Math.abs( red - p.getRed() ) > COMPARE_THRESHOLD )
302-
{
303-
return false;
304-
}
305-
if( Math.abs( blue - p.getBlue() ) > COMPARE_THRESHOLD )
306-
{
307-
return false;
308-
}
309-
if( Math.abs( green - p.getGreen() ) > COMPARE_THRESHOLD )
310-
{
311-
return false;
312-
}
313-
return true;
370+
return !( Math.abs( red - p.getRed() ) > COMPARE_THRESHOLD ||
371+
Math.abs( blue - p.getBlue() ) > COMPARE_THRESHOLD ||
372+
Math.abs( green - p.getGreen() ) > COMPARE_THRESHOLD);
314373
}
315-
else
316-
{
317-
return false;
318-
}
374+
return false;
319375
}
320376

321377
}

PixGrid.java

Lines changed: 64 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,22 @@ public void generate_blank_world()
6464
}
6565
}
6666

67-
/**
68-
* Creates a number of random-location Pix of a certain type
69-
* @param number number of Pix objects to create
70-
*/
71-
public void generate_world(int number)
72-
{
73-
for(int i = 0; i < number; i ++)
74-
{
75-
grid[(int)(Math.random() * grid.length)][(int)(Math.random() * grid[0].length)] = new PulsePix();
76-
}
67+
// <<<<<<< HEAD
68+
// /**
69+
// * Creates a number of random-location Pix of a certain type
70+
// * @param number number of Pix objects to create
71+
// */
72+
// public void generate_world(int number)
73+
// {
74+
// for(int i = 0; i < number; i ++)
75+
// {
76+
// grid[(int)(Math.random() * grid.length)][(int)(Math.random() * grid[0].length)] = new PulsePix();
77+
// }
7778

78-
}
79+
// }
7980

81+
// =======
82+
// >>>>>>> e1f460e674cfa724fd2c081b6fa5e59ab85077cf
8083
/**
8184
* Returns the grid
8285
* @return grid
@@ -86,34 +89,37 @@ public Pix[][] getGrid()
8689
return grid;
8790
}
8891

89-
/**
90-
* Transitions a Pix from one location to another
91-
* If the Pix is white, the Pix is moved. Otherwise, one Pix interacts with the other
92-
* @param ix inital x location
93-
* @param ix inital y location
94-
* @param x ending x location
95-
* @param y ending y location
96-
*/
97-
public void trans(int ix, int iy, int x, int y)
98-
{
99-
// Didn't try to move out of bounds
100-
if (!(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length))
101-
{
102-
if (grid[x][y].isWhite())
103-
{
104-
// If trans location is white, move Pix
105-
movePixel(ix, iy, x, y);
106-
}
107-
else
108-
{
109-
// Otherwise interact with given location
110-
grid[ix][iy].interact(grid[x][y]);
111-
}
92+
// <<<<<<< HEAD
93+
// /**
94+
// * Transitions a Pix from one location to another
95+
// * If the Pix is white, the Pix is moved. Otherwise, one Pix interacts with the other
96+
// * @param ix inital x location
97+
// * @param ix inital y location
98+
// * @param x ending x location
99+
// * @param y ending y location
100+
// */
101+
// public void trans(int ix, int iy, int x, int y)
102+
// {
103+
// // Didn't try to move out of bounds
104+
// if (!(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length))
105+
// {
106+
// if (grid[x][y].isWhite())
107+
// {
108+
// // If trans location is white, move Pix
109+
// movePixel(ix, iy, x, y);
110+
// }
111+
// else
112+
// {
113+
// // Otherwise interact with given location
114+
// grid[ix][iy].interact(grid[x][y]);
115+
// }
112116

113117

114-
}
115-
}
118+
// }
119+
// }
116120

121+
// =======
122+
// >>>>>>> e1f460e674cfa724fd2c081b6fa5e59ab85077cf
117123
/**
118124
* Loops through all non-white Pix and transitions them in a random direction (N S E W)
119125
*/
@@ -125,51 +131,33 @@ public void update()
125131
{
126132
if( !grid[i][j].isWhite() )
127133
{
128-
int direction = (int)(Math.random() * 4);
129-
switch(direction)
130-
{
131-
case 0:
132-
trans(i,j,i+1,j);
133-
break;
134-
case 1:
135-
trans(i,j,i,j+1);
136-
break;
137-
case 2:
138-
trans(i,j,i-1,j);
139-
break;
140-
case 3:
141-
trans(i,j,i,j-1);
142-
break;
143-
default:
144-
break;
145-
}
134+
// <<<<<<< HEAD
135+
// int direction = (int)(Math.random() * 4);
136+
// switch(direction)
137+
// {
138+
// case 0:
139+
// trans(i,j,i+1,j);
140+
// break;
141+
// case 1:
142+
// trans(i,j,i,j+1);
143+
// break;
144+
// case 2:
145+
// trans(i,j,i-1,j);
146+
// break;
147+
// case 3:
148+
// trans(i,j,i,j-1);
149+
// break;
150+
// default:
151+
// break;
152+
// }
153+
// =======
154+
grid[i][j].update(grid, i , j );
155+
// >>>>>>> e1f460e674cfa724fd2c081b6fa5e59ab85077cf
146156
}
147157
}
148158
}
149159
}
150160

151-
/**
152-
* Moves a Pix
153-
* @param ix inital x location
154-
* @param ix inital y location
155-
* @param x ending x location
156-
* @param y ending y location
157-
*/
158-
public void movePixel(int ix, int iy, int x, int y)
159-
{
160-
try
161-
{
162-
grid[x][y] = grid[ix][iy].getClass().newInstance();
163-
grid[x][y].setPix(grid[ix][iy]);
164-
}
165-
catch (Exception e)
166-
{
167-
e.printStackTrace();
168-
}
169-
//Uncomment to remove spread
170-
//grid[ix][iy] = new NonconformingPix(255,255,255);
171-
}
172-
173161
/**
174162
* Returns a String representation of a PixGrid
175163
* @return PixGrid String representation

Pixelife.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public Pixelife(int w, int h, int n)
3434
height = h;
3535

3636
myGrid = new PixGrid(w, h, n);
37-
spawner = new Spawner(PulsePix.class);
38-
spawner.spawn(myGrid);
37+
spawner = new Spawner(PulsePix.class, myGrid);
38+
spawner.spawn(10);
3939
}
4040

4141
public Dimension getPreferredSize()
@@ -58,7 +58,8 @@ public void run()
5858
while(true)
5959
{
6060
myGrid.update();
61-
61+
spawner.update();
62+
6263
draw();
6364

6465
// Limit framerate

0 commit comments

Comments
 (0)