ECE2035 Project 2

Dependencies:   mbed mbed-rtos SDFileSystem

fruit.cpp

Committer:
kwengryn3
Date:
2021-04-20
Revision:
10:1994adcfc86f
Parent:
9:f1d34ef049c5

File content as of revision 10:1994adcfc86f:

//=================================================================
// Implementation of fruit module.
//
// Copyright 2020 Georgia Tech.  All rights reserved.
// The materials provided by the instructor in this course are for
// the use of the students currently enrolled in the course.
// Copyrighted course materials may not be further disseminated.
// This file must not be made publicly available anywhere.
//==================================================================

#include "fruit_private.h"
#include "doubly_linked_list.h"

int fruit_tick=0;

//Create a DLL for fruits
DLinkedList* fruitDLL = NULL;


void fruit_init(void)
{
//Create a new doubly linked list of fruits
    fruitDLL = create_dlinkedlist();
}

void fruit_generator(void){
    //pc.printf("fruit: fruit_generator\n");
    fruit_tick++;
    // only fire the fruit at certain ticks
    if ((fruit_tick % FRUIT_INTERVAL)==0 || fruit_tick==0){
        //printf("fruit_create()");
        fruit_create();
    }        
    // update the fruits and draw them
    fruit_update_position();
}

void fruit_create(void){
    //pc.printf("Alloc memory\n");
    
    FRUIT* M = (FRUIT*)malloc(sizeof(FRUIT));
    // M->y = 0;
    //each fruit has its own tick
    //pc.printf("rand\n");
    M->type = rand() % 4;
    switch (M->type)
    {
    case 0:
        M->draw = draw_bomb;
        break;
    case 1:
        M->draw = draw_orange;
        break;
    case 2:
        M->draw = draw_banana;
        break;
    case 3:
        M->draw = draw_apple;
    default:
        break;
    }
   
    M->tick = 0;
    //set a random source for the fruit
    M->direction = rand() % 3;
    if (M->direction == 0){
        //pc.printf("0\n");
        M->source = rand() % (SIZE_X - FRUIT_SIZE - PLAYER_SPACE);
    //set a random target for the fruit
        M->target = rand() % (SIZE_X - FRUIT_SIZE - PLAYER_SPACE);
    //the fruit starts at its source
        M->box.topLeft.x = M->source + PLAYER_SPACE;
        M->box.topLeft.y = 0; // = {M->source + PLAYER_SPACE, 0};
        M->box.bottomRight.x = M->source + FRUIT_SIZE + PLAYER_SPACE;
        M->box.bottomRight.y = FRUIT_SIZE;
        //M->box.bottomRight = {M->source + FRUIT_SIZE + PLAYER_SPACE, FRUIT_SIZE};
        double diagnal = sqrt((M->source - M->target)*(M->source - M->target) + SIZE_Y*SIZE_Y);
        M->delta_x = (M->target - M->source) / diagnal;
        M->delta_y = fabs(SIZE_Y / diagnal);
    }
    else if(M->direction == 1){
        
       
        M->source = rand() % (SIZE_Y - FRUIT_SIZE);
    //set a random target for the fruit
        M->target = rand() % (SIZE_Y - FRUIT_SIZE);
        M->box.topLeft.x = PLAYER_SPACE;
        M->box.topLeft.y = M->source;
        //M->box.topLeft = {PLAYER_SPACE, M->source};
        
        M->box.bottomRight.x = PLAYER_SPACE + FRUIT_SIZE;
        M->box.bottomRight.y = M->source + FRUIT_SIZE;
        //M->box.bottomRight = {PLAYER_SPACE + FRUIT_SIZE, M->source + FRUIT_SIZE};
        
        double diagnal = sqrt((M->source - M->target)*(M->source - M->target) + (SIZE_X - PLAYER_SPACE)*(SIZE_X - PLAYER_SPACE));
        M->delta_x = (SIZE_X - PLAYER_SPACE) / diagnal; 
        
        M->delta_y = fabs((M->target - M->source) / diagnal);
        
       
    }else{
        //pc.printf("else\n");
        M->source = rand() % (SIZE_Y - FRUIT_SIZE);
    //set a random target for the fruit
        M->target = rand() % (SIZE_Y - FRUIT_SIZE);
        M->box.topLeft.x = PLAYER_SPACE + SIZE_X - FRUIT_SIZE;
        M->box.topLeft.y = M->source;
        //M->box.topLeft = {PLAYER_SPACE + SIZE_X - FRUIT_SIZE, M->source};
        M->box.bottomRight.x = PLAYER_SPACE + SIZE_X;
        M->box.bottomRight.y = M->source + FRUIT_SIZE;
        //M->box.bottomRight = {PLAYER_SPACE + SIZE_X, M->source + FRUIT_SIZE};
        double diagnal = sqrt((M->source - M->target)*(M->source - M->target) + (SIZE_X - PLAYER_SPACE)*(SIZE_X - PLAYER_SPACE));
        M->delta_x = (PLAYER_SPACE - SIZE_X) / diagnal; 
        M->delta_y = fabs((M->target - M->source) / diagnal);
    }
    
    
    
    M->status = FRUIT_ACTIVE;
    //pc.printf("new fruit coords= (%d, %d)\n", M->box.topLeft.x, M->box.topLeft.y  );
    
    insertHead(fruitDLL, M);
}

void fruit_update_position(void){

    //pc.printf("fruit: fruit_update_position");
    //controls how fast the fruit will move
    const int rate = FRUIT_SPEED;
    //delta_x and delta_y account for the slope of the fruit
    DrawFunc draw = NULL;
    LLNode* current = fruitDLL->head;
    FRUIT* newFruit;
    //iterate over all fruits
    int count = 0;
    while(count < fruitDLL->size)
    {   
        
        newFruit = (FRUIT*) current->data;
        //pc.printf("Fruit %d: - (%d, %d),(%d, %d)\n", count, newFruit->box.topLeft.x, newFruit->box.topLeft.y, newFruit->box.bottomRight.x, newFruit->box.bottomRight.y);
        if(newFruit->status == FRUIT_SLICED ||
            newFruit->box.topLeft.x > 127 ||
            newFruit->box.topLeft.y > 120 || 
            newFruit->box.topLeft.x <= 0)
        {
            //cover the last fruit location
            draw_nothing(newFruit->box);
            // clear the fruit on the screen
            draw = NULL;           
            // Remove it from the list
            //pc.printf("deleting fruit node...\n");
            LLNode* toDelete = current;
            current = current->next;
            deleteNode(fruitDLL, toDelete);
            count++;
            continue;
           
            //pc.printf("fruit node deleted.\n");
        }
        else 
        {
            //cover the last fruit location
            
            draw_nothing(newFruit->box);

            // update fruit position
            
            //pc.printf("%f, %f\n", newFruit->delta_x, newFruit->delta_y);
            
            newFruit->box.topLeft.x += rate*newFruit->delta_x;
            newFruit->box.topLeft.y += rate*newFruit->delta_y;
            newFruit->box.bottomRight.x += rate*newFruit->delta_x;
            newFruit->box.bottomRight.y += rate*newFruit->delta_y;
            //pc.printf(" %f, %f", newFruit->delta_x, newFruit->delta_y);
            // draw fruit
           
            draw = newFruit->draw;
            //update fruit's internal tick
            newFruit->tick++;
           
            //current->data = (void*) newFruit;            
        }       
        // Advance the loop
        if(draw){
           
            draw(newFruit->box);
        } 
       
        current = current->next;
        
        count++;
    }
}

DLinkedList* get_fruit_list() {
    return fruitDLL;

}