timesynk/old/player.c

130 lines
4.1 KiB
C

/****** player.c
This file contains the functions for initializing and commanding the player.
TODO: all player* functions should use a player ID for multi-player support.
******/
#include <string.h> // for strcat
#include "player.h"
#include "game.h"
#include "stubs.h"
#include "message.h" // for MESSAGE*
/**** playerSetCommand
Modifes the player_commands global array, making the given command_id hash in the array point to the passed function.
Arguments: int command_hash, void(*function)
Globals Modified: player_commands
Returns: void
****/
void playerSetCommand(int command_id, void(*function)) {
player_commands[command_id] = function;
}
/**** playerMove
Attempts to move the player in a given direction and given distance. Checks for collisions at target location, and if clear, moves the Tile.
Arguments: int direction, int distance
Returns: void
****/
void playerMove(int direction, int distance) {
switch(direction) {
case NORTH:
if (!gameCollision(player->x, (player->y-distance))) {
gameMoveTile(player, player->x, player->y-distance);
}
break;
case NORTHWEST:
if (!gameCollision(player->x-distance, (player->y-distance))) {
gameMoveTile(player, player->x-distance, player->y-distance);
}
break;
case NORTHEAST:
if (!gameCollision(player->x+distance, (player->y-distance))) {
gameMoveTile(player, player->x+distance, player->y-distance);
}
break;
case SOUTH:
if (!gameCollision(player->x, player->y+distance)) {
gameMoveTile(player, player->x, player->y+distance);
}
break;
case SOUTHWEST:
if (!gameCollision(player->x-distance, player->y+distance)) {
gameMoveTile(player, player->x-distance, player->y+distance);
}
break;
case SOUTHEAST:
if (!gameCollision(player->x+distance, player->y+distance)) {
gameMoveTile(player, player->x+distance, player->y+distance);
}
break;
case EAST:
if (!gameCollision(player->x+distance, player->y)) {
gameMoveTile(player, player->x+distance, player->y);
}
break;
case WEST:
if (!gameCollision(player->x-distance, player->y)) {
gameMoveTile(player, player->x-distance, player->y);
}
break;
}
}
/**** playerLook
Looks at given x and y coordinate, generates a "," delimited list of items, appends it to MESSAGE_LOOK_SUCCESS, and shows it to the player.
Arguments: int x, int y
Returns: void
TODO: messageTile should likely be called here, rather than interfacePrint directly. Also x and y coordinates should be limited by a visibility map.
****/
void playerLook(int x, int y) {
char string[64];
string[0] = '\0';
struct Tile* debug_tile;
if ((x < current_map->width && x >= 0) && (y < current_map->height && y >= 0)) {
if (current_map->matrix[x][y].tid) {
strcat(string, MESSAGE_LOOK_SUCCESS);
int chain_count = 0;
debug_tile = &current_map->matrix[x][y];
while(debug_tile != NULL) {
strcat(string, ((struct BasicTile*)debug_tile->data)->name);
if (debug_tile->next) {
strcat(string, ", ");
}
debug_tile = debug_tile->next;
chain_count++;
}
interfacePrint(string);
}
} else {
interfacePrint("You gaze into the void...");
}
}
/**** playerActivate
Gets the top-most tile of a Tile Chain and attempts to activate it.
Arguments: int x, int y
Returns: void
****/
void playerActivate(int x, int y) {
activateTile(getTopTile(current_map, x, y), player);
}
/**** playerPickup
Gets the previous Tile from the player's position in the player's Tile Chain and if it is an ITEM, removes it from its position in the Chain and adds it to the player's inventory while also sending a message to the player of its pickup.
Returns: void
****/
void playerPickup() {
struct Tile* tile = player->prev;
if (tile->tid == ITEM || tile->tid == EQUIP) {
removeFromChain(tile);
addToInventory(&((struct PlayerTile*)player->data)->inventory, tile);
char string[64];
sprintf(string, MESSAGE_PICKUP, ((struct BasicTile*)tile->data)->name);
messageTile(player, player, string);
}
}