katbug/Pickups.c
2019-02-04 13:24:24 -08:00

144 lines
No EOL
2.4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>
#include "config.h"
#include "Engine.h"
#include "Catbug.h"
#include "Pickups.h"
#include "Timer.h"
#include "extern.h"
Pickup* newPickup()
{
int pType;
srand(rand());
pType = rand()%10;
Pickup* self = malloc(sizeof(Pickup));
self->y = 16 + rand()%148;
self->x = 320 + rand()%120;
self->vX = -4 - rand()%maxHP;
self->vY = 0;
switch (pType)
{
case 0:
case 1:
case 2:
case 3:
case 4:
self->sprite = loadImage("assets/sugarpeas.png");
self->points = 1;
break;
case 5:
case 6:
case 7:
self->sprite = loadImage("assets/peanutbuttersquare.png");
self->points = 2;
break;
case 8:
case 9:
self->sprite = loadImage("assets/taco.png");
self->points = 3;
break;
}
return self;
}
void managePickups()
{
int i;
int genSpeed;
srand(rand());
if (points > 10)
genSpeed = rand()%points;
else genSpeed = rand()%10;
for (i = 0; i < 10; i++)
{
if (stuff[i] != NULL)
{
drawPickup(stuff[i]);
movePickup(stuff[i]);
if (stuff[i]->x < -16)
{
invalidatePickup(stuff[i]);
player->HP--;
stuff[i] = NULL;
}
else
{
SDL_Rect box;
box.x = stuff[i]->x - 18;
box.y = stuff[i]->y - 18;
box.w = 36;
box.h = 36;
if (isInRect(player, &box))
{
getPickup(stuff[i]);
stuff[i] = NULL;
}
}
}
else
{
if ( (stuff[i] == NULL) &&
((points >= 40 && genSpeed*genSpeed >= points*4) ||
(points <= 40 && genSpeed*genSpeed >= threshold*2)) )
{
if (ticker != time(NULL)){
stuff[i] = newPickup();
ticker = time(NULL);}
}
}
}
}
void movePickup(Pickup* self)
{
self->x += self->vX;
}
void drawPickup(Pickup* self)
{
applySurface(self->x - 16, self->y - 16, self->sprite, screen, NULL);
}
void invalidatePickup(Pickup* self)
{
SDL_FreeSurface(self->sprite);
free(self);
}
void getPickup(Pickup* self)
{
points += self->points;
if (points >= threshold)
{
threshold *= 2;
maxHP++;
player->HP = maxHP;
}
invalidatePickup(self);
}
void cleanPickups()
{
int i;
for (i = 0; i < 10; i++)
{
if (stuff[i] != NULL)
{
invalidatePickup(stuff[i]);
stuff[i] = NULL;
}
}
}