ECE 2036 Project
Dependencies: mbed wave_player 4DGL-uLCD-SE
Diff: wall.cpp
- Revision:
- 2:2042f29de6b7
- Parent:
- 0:cf4396614a79
--- a/wall.cpp Wed Nov 08 21:01:43 2017 +0000 +++ b/wall.cpp Thu Nov 21 16:10:57 2019 +0000 @@ -9,19 +9,19 @@ { // Alocate memory for wall Wall* wall = (Wall*) malloc(sizeof(Wall)); - + // Initialize type info. // Always draw on the first frame wall->type = WALL; wall->should_draw = 1; - + // Set user parameters wall->direction = direction; wall->x = x; wall->y = y; wall->length = length; wall->bounce = bounce; - + return wall; } @@ -29,56 +29,55 @@ { // Debug info // pc.printf("wall: %d %d %d %d\r\n", wall->direction, wall->x, wall->y, wall->length); - + float adj, percent, contact; - if (wall->direction == VERTICAL) - { + if (wall->direction == VERTICAL) { // Step 0: Adjust the wall location based on ball radius adj = wall->x - sgn(curr->vx)*(radius+1); // Step 1: Compute percent of motion before bounce percent = (adj - curr->px) / (curr->vx*delta); - + // Negative percent means we're moving away from the wall // >=100% means we don't need to bounce yet - if (percent >= 0.0 && percent < 1.0) - { + if (percent >= 0.0 && percent < 1.0) { // Compute the bounce location contact = curr->py + curr->vy*percent*delta; - if (in_range(contact, wall->y, wall->y + wall->length)) - { - + if (in_range(contact, wall->y, wall->y + wall->length)) { + // SGFX + redLED = 1; + speaker.PlayNote(100, 0.01, sound); + redLED = 0; + // Reverse velocity and slow down next->vx = -wall->bounce*curr->vx; // Adjust X position. - // This dampens the integration based on how far the ball had + // This dampens the integration based on how far the ball had // to travel before reflecting. next->px = adj - percent*next->vx*delta; } // Draw the wall if we're close by wall->should_draw = 1; } - + // Make sure we aren't inside the wall if (in_range((int)curr->px, wall->x - (radius+1), wall->x + (radius+1)) - && in_range((int)curr->py, wall->y, wall->y + wall->length)) - { + && in_range((int)curr->py, wall->y, wall->y + wall->length)) { next->px = wall->x + sgn(curr->px - wall->x)*(radius+1); wall->should_draw = 1; } - } - else //direction == HORIZONTAL - { + } else { //direction == HORIZONTAL // Same as vertical case, but swap x/y - adj = wall->y - sgn(curr->vy)*(radius+1); + adj = wall->y - sgn(curr->vy)*(radius+1); percent = (adj - curr->py) / (curr->vy*delta); - if (percent >= 0.0 && percent < 1.0) - { + if (percent >= 0.0 && percent < 1.0) { contact = curr->px + curr->vx*percent*delta; - if (in_range(contact, wall->x, wall->x + wall->length)) - { + if (in_range(contact, wall->x, wall->x + wall->length)) { + redLED = 1; + speaker.PlayNote(100, 0.01, sound); + redLED = 0; next->vy = -wall->bounce*curr->vy; next->py = adj - percent*next->vy*delta; } @@ -86,8 +85,7 @@ wall->should_draw = 1; } if (in_range((int)curr->py, wall->y - (radius+1), wall->y + (radius+1)) - && in_range((int)curr->px, wall->x, wall->x + wall->length)) - { + && in_range((int)curr->px, wall->x, wall->x + wall->length)) { next->py = wall->y + sgn(curr->py - wall->y)*(radius+1); wall->should_draw = 1; } @@ -96,16 +94,15 @@ void draw_wall(Wall* wall) { - if (wall->should_draw) - { + if (wall->should_draw) { int wx1, wx2, wy1, wy2; wx1 = wall->x; wy1 = wall->y; wx2 = wx1 + ((wall->direction == HORIZONTAL) ? wall->length : 0); wy2 = wy1 + ((wall->direction == VERTICAL) ? wall->length : 0); uLCD.line(wx1, wy1, wx2, wy2, WHITE); - + // Don't draw again unless requested wall->should_draw = 0; } -} \ No newline at end of file +} \ No newline at end of file