Skip to content

Commit 1606303

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

File tree

5 files changed

+127
-83
lines changed

5 files changed

+127
-83
lines changed

DirectedPix.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
public class DirectedPix extends Pix
3+
{
4+
private int direction; // (N E S W)
5+
6+
public DirectedPix()
7+
{
8+
super();
9+
direction = 0;
10+
}
11+
public DirectedPix(int r, int g, int b, int direction)
12+
{
13+
super(r,g,b);
14+
this.direction = direction;
15+
}
16+
17+
public void setDir(int dir)
18+
{
19+
direction = dir;
20+
}
21+
22+
/**
23+
* Updates this specific pix, moving it in it's direction
24+
*/
25+
public void update(int i, int j)
26+
{
27+
//int direction = (int)(Math.random() * 4);
28+
//System.out.println(direction);
29+
moveDir(Pixelife.getGrid().getGrid(), direction, i, j);
30+
}
31+
32+
/**
33+
* Transitions a Pix from one location to another
34+
* If the Pix is white, the Pix is moved. Otherwise, one Pix interacts with the other
35+
* @param ix inital x location
36+
* @param ix inital y location
37+
* @param x ending x location
38+
* @param y ending y location
39+
*/
40+
public void trans(Pix[][] grid, int ix, int iy, int x, int y)
41+
{
42+
// Didn't try to move out of bounds
43+
if (!(x < 0 || x >= grid.length || y < 0 || y >= grid[0].length))
44+
{
45+
movePixel(grid, ix, iy, x, y);
46+
}
47+
}
48+
49+
/**
50+
* Moves a Pix
51+
* @param ix inital x location
52+
* @param ix inital y location
53+
* @param x ending x location
54+
* @param y ending y location
55+
*/
56+
public void movePixel(Pix[][] grid, int ix, int iy, int x, int y)
57+
{
58+
// Moves it along
59+
Spawner.spawnXY( grid[ix][iy].getClass(), x, y );
60+
grid[x][y].setPix(grid[ix][iy]);
61+
62+
// Duplicates upward and downward
63+
//Spawner.spawnXY( grid[ix][iy].getClass(), x, y-1 );
64+
//Spawner.spawnXY( grid[ix][iy].getClass(), x, y+1 );
65+
//grid[x][y-1].setPix(grid[ix][iy]);
66+
//grid[x][y+1].setPix(grid[ix][iy]);
67+
//((DirectedPix)grid[x][y-1]).setDir(0);
68+
//((DirectedPix)grid[x][y+1]).setDir(2);
69+
//Uncomment to remove spread
70+
//grid[ix][iy] = new Pix(255,255,255);
71+
}
72+
}

Pix.java

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

261261
}
262262

263-
/**
263+
/**
264264
* Transitions a Pix from one location to another
265265
* If the Pix is white, the Pix is moved. Otherwise, one Pix interacts with the other
266266
* @param ix inital x location
@@ -282,19 +282,25 @@ public void trans(Pix[][] grid, int ix, int iy, int x, int y)
282282
{
283283
grid[ix][iy].interact(grid[x][y]);
284284
}
285-
286-
287285
}
288286
}
289287

290288
/**
291-
* Loops through all non-white Pix and transitions them in a random direction (N S E W)
289+
* Updates this specific pix, moving it randomly
292290
*/
293-
public void update(Pix[][] grid, int i, int j)
291+
public void update(int i, int j)
294292
{
295293
int direction = (int)(Math.random() * 4);
296294
//System.out.println(direction);
297-
switch(direction)
295+
moveDir(Pixelife.getGrid().getGrid(), direction, i, j);
296+
}
297+
298+
/**
299+
* Moves the direction given (N S E W)
300+
*/
301+
public void moveDir(Pix[][] grid, int dirNum, int i, int j)
302+
{
303+
switch(dirNum)
298304
{
299305
case 0:
300306
trans(grid, i,j,i+1,j);
@@ -322,7 +328,6 @@ public void update(Pix[][] grid, int i, int j)
322328
*/
323329
public void movePixel(Pix[][] grid, int ix, int iy, int x, int y)
324330
{
325-
//grid[x][y] = grid[ix][iy].getClass().newInstance();
326331
Spawner.spawnXY( grid[ix][iy].getClass(), x, y );
327332
grid[x][y].setPix(grid[ix][iy]);
328333
//Uncomment to remove spread

PixGrid.java

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,6 @@ public void generate_blank_world()
6464
}
6565
}
6666

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-
// }
78-
79-
// }
80-
81-
// =======
82-
// >>>>>>> e1f460e674cfa724fd2c081b6fa5e59ab85077cf
8367
/**
8468
* Returns the grid
8569
* @return grid
@@ -89,37 +73,6 @@ public Pix[][] getGrid()
8973
return grid;
9074
}
9175

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-
// }
116-
117-
118-
// }
119-
// }
120-
121-
// =======
122-
// >>>>>>> e1f460e674cfa724fd2c081b6fa5e59ab85077cf
12376
/**
12477
* Loops through all non-white Pix and transitions them in a random direction (N S E W)
12578
*/
@@ -131,28 +84,7 @@ public void update()
13184
{
13285
if( !grid[i][j].isWhite() )
13386
{
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
87+
grid[i][j].update( i , j );
15688
}
15789
}
15890
}

Pixelife.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
* Pixelife class
1212
* Manages GUI and pixel painting
1313
* @author Nic Manoogian <[email protected]>
14-
* @author Mike Lyons
14+
* @author Mike Lyons <[email protected]>
1515
*/
1616
public class Pixelife extends JPanel
1717
{
1818
private BufferedImage canvas;
19-
private PixGrid myGrid;
19+
private static PixGrid myGrid;
20+
//private Spawner line_spawner;
2021
private Spawner spawner;
2122
private int width;
2223
private int height;
@@ -34,6 +35,7 @@ public Pixelife(int w, int h, int n)
3435
height = h;
3536

3637
myGrid = new PixGrid(w, h, n);
38+
//line_spawner = new Spawner(DirectedPix.class, myGrid, 0, h/2);
3739
spawner = new Spawner(PulsePix.class, myGrid);
3840
spawner.spawn(10);
3941
}
@@ -58,6 +60,7 @@ public void run()
5860
while(true)
5961
{
6062
myGrid.update();
63+
//line_spawner.update();
6164
spawner.update();
6265

6366
draw();
@@ -113,4 +116,9 @@ public static void main(String [] args)
113116
plife.run();
114117
}
115118

119+
public static PixGrid getGrid()
120+
{
121+
return myGrid;
122+
}
123+
116124
}

Spawner.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@
1212
public class Spawner
1313
{
1414
private boolean needRespawn;
15+
1516
private int respawnCounter;
1617
private int respawnMax;
18+
1719
private int numSpawn;
18-
private int maxNumSpawn;
20+
private int numSpawnMax;
21+
22+
/*
23+
* Default x and y, if none then == -1
24+
*/
25+
private int defaultX;
26+
private int defaultY;
27+
1928
private Class<?> spawnClass;
2029
private static PixGrid grid;
2130

@@ -28,7 +37,9 @@ public Spawner()
2837
respawnCounter = 0;
2938
respawnMax = 100;
3039
numSpawn = 10;
31-
maxNumSpawn = 10;
40+
numSpawnMax = 10;
41+
defaultX = -1;
42+
defaultY = -1;
3243
spawnClass = Pix.class;
3344
}
3445

@@ -53,14 +64,30 @@ public Spawner(Class<?> c, PixGrid grid)
5364
this.spawnClass = c;
5465
this.grid = grid;
5566
}
67+
public Spawner(Class<?> c, PixGrid grid, int dx, int dy)
68+
{
69+
this(c, grid);
70+
this.spawnClass = c;
71+
this.grid = grid;
72+
73+
this.defaultX = dx;
74+
this.defaultY = dy;
75+
}
5676

5777
public void spawn()
5878
{
5979
if(Pix.class.isAssignableFrom(spawnClass))
6080
{
6181
try {
62-
grid.getGrid()[(int)(Math.random() * grid.getGrid().length)][(int)(Math.random() *
63-
grid.getGrid()[0].length)] = (Pix)spawnClass.newInstance();
82+
if( defaultX != -1 && defaultY != -1 )
83+
{
84+
grid.getGrid()[defaultX][defaultY] = (Pix)spawnClass.newInstance();
85+
}
86+
else
87+
{
88+
grid.getGrid()[(int)(Math.random() * grid.getGrid().length)][(int)(Math.random() *
89+
grid.getGrid()[0].length)] = (Pix)spawnClass.newInstance();
90+
}
6491
} catch(Exception e) {
6592
e.printStackTrace();
6693
}
@@ -126,7 +153,7 @@ public void update()
126153
numSpawn--;
127154
if( numSpawn == 0 )
128155
{
129-
numSpawn = maxNumSpawn;
156+
numSpawn = numSpawnMax;
130157
needRespawn = false;
131158
}
132159
}

0 commit comments

Comments
 (0)