Trammell Hudson avatar Trammell Hudson committed 0c651af Draft

Move rock creation into separate routine, add rock splitting

Comments (0)

Files changed (1)

 #define STARTING_AMMO 65535
 #define MAX_ROCKS	32
 #define MAX_BULLETS	4
+#define ROCK_VEL	64
 
 static int frame_num = 0;
 
 	point_update(&s->p);
 }
 
+static void
+rock_create(
+	rock_t * const rocks,
+	int16_t x,
+	int16_t y,
+	int16_t size
+)
+{
+	// find a free rock
+	for (uint8_t i = 0 ; i < MAX_ROCKS ; i++)
+	{
+		rock_t * const r = &rocks[i];
+		if (r->size != 0)
+			continue;
+
+fprintf(stderr, "%d: %d,%d\n", i, x, y);
+		r->size = size;
+		r->p.x = x;
+		r->p.y = y;
+		r->p.vx = rand() % ROCK_VEL;
+		r->p.vy = rand() % ROCK_VEL;
+		r->type = rand() % NUM_ROCK_TYPES;
+		return;
+	}
+}
+
 
 static void
 rocks_update(
 			if (collide(&r->p, &b->p, r->size))
 			{
 				fprintf(stderr, "rock %d is dead\n", i);
+
+				uint16_t new_size = r->size / 2;
+				if (new_size > 256)
+				{
+					rock_create(rocks, r->p.x, r->p.y, new_size);
+					rock_create(rocks, r->p.x, r->p.y, new_size);
+					rock_create(rocks, r->p.x, r->p.y, new_size);
+				}
+
 				r->size = 0;
 				b->age = 0;
 				rock_dead = 1;
-				// \todo split into mulitple rocks
+
 				break;
 			}
 		}
 }
 
 
+
 static void
 rocks_init(
 	rock_t * const rocks,
 	for (uint8_t i = 0 ; i < MAX_ROCKS ; i++)
 	{
 		rock_t * const r = &rocks[i];
-		if (i > num)
-		{
-			// empty slot
-			r->size = 0;
-			continue;
-		}
+		r->size = 0;
+	}
 
+	for (uint8_t i = 0 ; i < num ; i++)
+	{
+		// Make sure that there is space around the center
 #define MIN_RADIUS 1024
-
-		// Make sure that there is space around the center
 		int16_t x = rand();
 		int16_t y = rand();
+		uint16_t size = (rand() % 32) * 256 + 512;
 		if (0 <= x)
 			x += MIN_RADIUS;
 		else
 			y += MIN_RADIUS;
 		else
 			y -= MIN_RADIUS;
-			
-		r->p.x = x;
-		r->p.y = y;
-		r->p.vx = rand() % 64;
-		r->p.vy = rand() % 64;
-		r->type = rand() % NUM_ROCK_TYPES;
-		r->size = (rand() % 32) * 256 + 512;
+
+		rock_create(rocks, x, y, size);
 
 	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.