hyperkaos/Player.c

266 lines
No EOL
5.3 KiB
C

#include <stdio.h>
#include "config.h"
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_mixer.h"
#include "enum.h"
#include "Engine.h"
#include "Player.h"
#include "Room.h"
#include "WorldData.h"
typedef struct TTF_Font TTF_Font;
typedef struct timer Timer;
typedef struct textBox TextBox;
typedef struct kaos Kaos;
typedef struct scene Scene;
typedef struct hyperKaos HyperKaos;
#include "extern.h"
Player* newPlayer(char* filename, int a, int b)
{
Player* self = malloc(sizeof(Player));
self->counter = 0;
self->spriteSheet = NULL;
self->spriteSheet = loadImage(filename);
self->clip.x = 32;
self->clip.y = 0;
self->clip.h = 16;
self->clip.w = 16;
self->point.x = a;
self->point.y = b;
self->bearing.x = 0;
self->bearing.y = 0;
self->boundBox.x = self->point.x - 8;
self->boundBox.y = self->point.y - 8;
self->boundBox.h = 16;
self->boundBox.w = 16;
self->frontFaceBox.x = self->boundBox.x;
self->frontFaceBox.y = self->boundBox.y + 16;
self->frontFaceBox.w = 16;
self->frontFaceBox.h = 16;
self->tombStone = 0;
return self;
}
void killPlayer(Player* self)
{
SDL_FreeSurface(self->spriteSheet);
free(self);
}
void movePlayer(Player* self, Room* hereNow)
{
self->point.x += self->bearing.x;
self->boundBox.x = self->point.x - 8;
// obstacle collision
if (checkCollision(rightHere, self, rightHere->obstacle))
{
self->point.x -= self->bearing.x;
self->boundBox.x = self->point.x - 8;
}
self->point.y += self->bearing.y;
self->boundBox.y = self->point.y - 8;
if (checkCollision(rightHere, self, rightHere->obstacle))
{
self->point.y -= self->bearing.y;
self->boundBox.y = self->point.y - 8;
}
// event trigger collision
if (self->bearing.y > 0)
{
self->frontFaceBox.x = self->boundBox.x;
self->frontFaceBox.y = self->boundBox.y + 16;
}
if (self->bearing.y < 0)
{
self->frontFaceBox.x = self->boundBox.x;
self->frontFaceBox.y = self->boundBox.y - 16;
}
if (self->bearing.x > 0)
{
self->frontFaceBox.x = self->boundBox.x + 16;
self->frontFaceBox.y = self->boundBox.y;
}
if (self->bearing.x < 0)
{
self->frontFaceBox.x = self->boundBox.x - 16;
self->frontFaceBox.y = self->boundBox.y;
}
int kt = 0;
int kf = 0;
if (checkKCollision(rightHere, self, rightHere->eventTriggers, &kf, &kt, 0))
{
if (!kt)
{
kaosFlag = kf;
}
else if (actionbutton)
{
kaosFlag = kf;
}
}
if (checkKCollision(rightHere, self, rightHere->eventTriggers, &kf, &kt, 1))
{
if (kt)
{
if (actionbutton)
kaosFlag = kf;
}
}
// warp collision
int outgoing = 0;
if (!captive)
{
if (checkWCollision(rightHere, self, rightHere->warps, &outgoing))
{
nextChunk = rightHere->warps[outgoing]->chunk;
if (nextChunk != thisChunk)
{
bufferData(hereNow->warps[outgoing]->chunk);
warpto(mapBuffer[rightHere->warps[outgoing]->destination]);
}
else warpto(mapData[rightHere->warps[outgoing]->destination]);
self->point.x = hereNow->warps[outgoing]->x;
self->point.y = hereNow->warps[outgoing]->y;
}
}
//stay onscreen
if (self->point.x < 1) self->point.x = 1;
if (self->point.x > 320 - 1) self->point.x = 320 - 1;
if (self->point.y < 1) self->point.y = 1;
if (self->point.y > 180) self->point.y = 180 - 1;
}
//
// graphics
//
void drawPlayer(Player* self)
{
applySurface(self->point.x - 8, self->point.y - 8, self->spriteSheet, screen, &(self->clip));
walkAnim(self);
}
void walkAnim(Player* self)
{
if (self->bearing.y > 0)
{
if (self->counter == 0)
{
self->clip.x = 32;
}
if (self->counter == 4)
{
self->clip.x = 48;
}
}
if (self->bearing.y < 0)
{
if (self->counter == 0)
{
self->clip.x = 112;
}
if (self->counter == 4)
{
self->clip.x = 96;
}
}
if (self->bearing.x > 0)
{
if (self->counter == 0)
{
self->clip.x = 64;
}
if (self->counter == 4)
{
self->clip.x = 80;
}
}
if (self->bearing.x < 0)
{
if (self->counter == 0)
{
self->clip.x = 0;
}
if (self->counter == 4)
{
self->clip.x = 16;
}
}
self->counter++;
if (self->counter == 8) self->counter = 0;
}
int playerFaces(Player* self, char dir)
{
switch (dir)
{
case 'n':
if (self->frontFaceBox.y < self->boundBox.y)
return 1;
break;
case 's':
if (self->frontFaceBox.y > self->boundBox.y)
return 1;
break;
case 'e':
if (self->frontFaceBox.x > self->boundBox.x)
return 1;
break;
case 'w':
if (self->frontFaceBox.x < self->boundBox.x)
return 1;
break;
}
return 0;
}
int playerIsInRect(Player* self, SDL_Rect* rect)
{
SDL_Rect* pBox = &(self->boundBox);
int playerX = pBox->x + (pBox->w)/2;
int playerY = pBox->y + (pBox->h)/2;
if ((playerX >= rect->x && playerX <= rect->x + rect->w)
&& (playerY >= rect->y && playerY <= rect->y + rect->h))
return 1;
else return 0;
}
int playerFacesRect(Player* self, SDL_Rect* rect)
{
SDL_Rect* pBox = &(self->frontFaceBox);
int playerX = pBox->x + (pBox->w)/2;
int playerY = pBox->y + (pBox->h)/2;
if ((playerX >= rect->x && playerX <= rect->x + rect->w)
&& (playerY >= rect->y && playerY <= rect->y + rect->h))
return 1;
else return 0;
}