Commits

Trammell Hudson committed 5a13021 Draft

Bullets work and take out rocks

  • Participants
  • Parent commits 3d05bcc

Comments (0)

Files changed (1)

File spacerocks.c

 #define rand() lrand48()
 
 #define STARTING_FUEL 65535
+#define STARTING_AMMO 65535
 #define MAX_ROCKS	32
 #define MAX_BULLETS	4
 
 	int16_t ax;
 	int16_t ay;
 	uint16_t fuel;
-	uint8_t shots;
+	uint16_t shots;
 	uint8_t angle;
 	uint8_t dead;
 } ship_t;
 collide(
 	const point_t * const p,
 	const point_t * const q,
-	const uint8_t radius
+	const int16_t radius
 )
 {
 	int16_t dx = p->x - q->x;
 	int16_t dy = p->y - q->y;
 
+	if (0) fprintf(stderr, "%d,%d -> %d,%d => %d,%d\n",
+		p->x, p->y,
+		q->x, q->y,
+		dx, dy
+	);
+
 	if (-radius < dx && dx < radius
 	&&  -radius < dy && dy < radius)
 		return 1;
 		return;
 
 #define BULLET_RANGE 255
-#define BULLET_VEL 16
+#define BULLET_VEL 8
 
 	b->age = BULLET_RANGE;
 	b->p.x = s->p.x;
 	b->p.vx = s->ax * BULLET_VEL + s->p.vx; // in the direction of the ship
 	b->p.vy = s->ay * BULLET_VEL + s->p.vy; // in the direction of the ship
 
+	fprintf(stderr, "fire: vx=%d vy=%d\n", b->p.vx, b->p.vy);
+
 	s->shots--;
 }
 
 		uint8_t rock_dead = 0;
 
 		// check for bullet collision
+//fprintf(stderr, "Bullet check\n");
 		for (uint8_t j = 0 ; j < MAX_BULLETS ; j++)
 		{
-			bullet_t * const b = &bullets[i];
+			bullet_t * const b = &bullets[j];
 			if (b->age == 0)
 				continue;
 			if (collide(&r->p, &b->p, r->size))
 			{
+				fprintf(stderr, "rock %d is dead\n", i);
 				r->size = 0;
+				b->age = 0;
 				rock_dead = 1;
 				// \todo split into mulitple rocks
 				break;
 			}
 		}
 
-		// check for ship collision if this rock wasn't destroyed
-		if (!rock_dead && collide(&r->p, &s->p, r->size))
+		if (rock_dead)
+			continue;
+
+//fprintf(stderr, "Ship check\n");
+		 if (collide(&r->p, &s->p, r->size))
 			s->dead = 1;
 	}
 }
 	{
 		bullet_t * const b = &bullets[i];
 		if (b->age != 0)
+		{
 			point_update(&b->p);
-		else
+			fprintf(stderr, "%d,%d\n", b->p.x/256, b->p.y/256);
+		} else
 		if (fire)
 		{
 			// We can try to fire this one
 			fire = 0;
 		}
 	}
+
+	if (fire)
+		fprintf(stderr, "no bullets\n");
 }
 
 
 	s->angle = rand();
 	s->dead = 0;
 	s->fuel = STARTING_FUEL;
+	s->shots = STARTING_AMMO;
 	s->ax = cos_lookup(s->angle);
 	s->ay = sin_lookup(s->angle);
 }
 )
 {
 	int8_t path[] = {
-		0, 0,
-		b->p.vx, b->p.vy,
+		-1, -1,
+		-1, +1,
+		+1, +1,
+		+1, -1,
+		-1, -1,
+		//b->p.vx/64, b->p.vy/64,
 	};
 
-	draw_path(b->p.x / 256 + 128, b->p.y / 256 + 128, path, 2);
+	draw_path(b->p.x / 256 + 128, b->p.y / 256 + 128, path, 5);
 }