2018-09-20 01:27:47 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-11-13 21:30:59 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2018-09-20 01:27:47 +00:00
|
|
|
#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;
|
2018-09-24 18:41:09 +00:00
|
|
|
typedef struct scene Scene;
|
2018-10-13 03:35:45 +00:00
|
|
|
typedef struct hyperKaos HyperKaos;
|
2018-09-20 01:27:47 +00:00
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
Player* newPlayer(char* filename, int a, int b)
|
|
|
|
{
|
2018-09-21 19:57:07 +00:00
|
|
|
Player* self = malloc(sizeof(Player));
|
2018-09-20 01:27:47 +00:00
|
|
|
|
|
|
|
self->counter = 0;
|
2019-01-13 20:19:57 +00:00
|
|
|
|
2018-09-20 01:27:47 +00:00
|
|
|
self->spriteSheet = NULL;
|
|
|
|
self->spriteSheet = loadImage(filename);
|
2019-01-13 20:19:57 +00:00
|
|
|
|
|
|
|
self->clip.x = 32;
|
|
|
|
self->clip.y = 0;
|
|
|
|
self->clip.h = 16;
|
|
|
|
self->clip.w = 16;
|
|
|
|
|
2018-09-20 01:27:47 +00:00
|
|
|
self->point.x = a;
|
|
|
|
self->point.y = b;
|
|
|
|
self->bearing.x = 0;
|
|
|
|
self->bearing.y = 0;
|
2019-01-13 20:19:57 +00:00
|
|
|
|
2018-09-20 01:27:47 +00:00
|
|
|
self->boundBox.x = self->point.x - 8;
|
|
|
|
self->boundBox.y = self->point.y - 8;
|
|
|
|
self->boundBox.h = 16;
|
|
|
|
self->boundBox.w = 16;
|
2019-01-13 20:19:57 +00:00
|
|
|
|
2018-09-20 01:27:47 +00:00
|
|
|
self->frontFaceBox.x = self->boundBox.x;
|
|
|
|
self->frontFaceBox.y = self->boundBox.y + 16;
|
|
|
|
self->frontFaceBox.w = 16;
|
|
|
|
self->frontFaceBox.h = 16;
|
2019-01-13 20:19:57 +00:00
|
|
|
|
2018-12-21 03:14:26 +00:00
|
|
|
self->tombStone = 0;
|
2018-09-20 01:27:47 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-10-13 03:35:45 +00:00
|
|
|
if (checkCollision(rightHere, self, rightHere->obstacle))
|
2018-09-20 01:27:47 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
2018-10-13 03:35:45 +00:00
|
|
|
if (checkCollision(rightHere, self, rightHere->obstacle))
|
2018-09-20 01:27:47 +00:00
|
|
|
{
|
|
|
|
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;
|
2019-01-13 20:19:57 +00:00
|
|
|
if (checkKCollision(rightHere, self, rightHere->eventTriggers, &kf, &kt, 0))
|
2018-09-20 01:27:47 +00:00
|
|
|
{
|
|
|
|
if (!kt)
|
|
|
|
{
|
|
|
|
kaosFlag = kf;
|
|
|
|
}
|
|
|
|
else if (actionbutton)
|
|
|
|
{
|
|
|
|
kaosFlag = kf;
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 20:19:57 +00:00
|
|
|
if (checkKCollision(rightHere, self, rightHere->eventTriggers, &kf, &kt, 1))
|
2018-09-20 01:27:47 +00:00
|
|
|
{
|
|
|
|
if (kt)
|
|
|
|
{
|
|
|
|
if (actionbutton)
|
|
|
|
kaosFlag = kf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// warp collision
|
|
|
|
|
|
|
|
int outgoing = 0;
|
|
|
|
if (!captive)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
if (checkWCollision(rightHere, self, rightHere->warps, &outgoing))
|
2018-09-20 01:27:47 +00:00
|
|
|
{
|
2018-12-21 03:14:26 +00:00
|
|
|
nextChunk = rightHere->warps[outgoing]->chunk;
|
2018-09-20 01:27:47 +00:00
|
|
|
|
|
|
|
if (nextChunk != thisChunk)
|
|
|
|
{
|
2018-12-21 03:14:26 +00:00
|
|
|
bufferData(hereNow->warps[outgoing]->chunk);
|
|
|
|
warpto(mapBuffer[rightHere->warps[outgoing]->destination]);
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
2018-12-21 03:14:26 +00:00
|
|
|
else warpto(mapData[rightHere->warps[outgoing]->destination]);
|
|
|
|
self->point.x = hereNow->warps[outgoing]->x;
|
|
|
|
self->point.y = hereNow->warps[outgoing]->y;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//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)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
applySurface(self->point.x - 8, self->point.y - 8, self->spriteSheet, screen, &(self->clip));
|
2018-11-13 21:08:21 +00:00
|
|
|
walkAnim(self);
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void walkAnim(Player* self)
|
|
|
|
{
|
|
|
|
if (self->bearing.y > 0)
|
|
|
|
{
|
|
|
|
if (self->counter == 0)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
self->clip.x = 32;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
if (self->counter == 4)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
self->clip.x = 48;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (self->bearing.y < 0)
|
|
|
|
{
|
|
|
|
if (self->counter == 0)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
self->clip.x = 112;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
if (self->counter == 4)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
self->clip.x = 96;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (self->bearing.x > 0)
|
|
|
|
{
|
|
|
|
if (self->counter == 0)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
self->clip.x = 64;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
if (self->counter == 4)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
self->clip.x = 80;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (self->bearing.x < 0)
|
|
|
|
{
|
|
|
|
if (self->counter == 0)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
self->clip.x = 0;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
if (self->counter == 4)
|
|
|
|
{
|
2019-01-13 20:19:57 +00:00
|
|
|
self->clip.x = 16;
|
2018-09-20 01:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self->counter++;
|
|
|
|
if (self->counter == 8) self->counter = 0;
|
|
|
|
}
|
2018-10-13 03:35:45 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-06 00:52:45 +00:00
|
|
|
// collision helper functions
|
|
|
|
|
2018-10-13 03:35:45 +00:00
|
|
|
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;
|
2019-01-13 20:19:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-10-13 03:35:45 +00:00
|
|
|
}
|