forked from jaildesigner-jailgames/jaildoctors_dilemma
Añadida la clase item_tracker
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
<tileset firstgid="1" source="../../resources/tilesets/room1.tsx"/>
|
||||
<layer id="1" name="Capa de patrones 1" width="32" height="16">
|
||||
<data encoding="csv">
|
||||
23,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,
|
||||
|
||||
@@ -76,4 +76,11 @@ void Item::pick()
|
||||
SDL_Rect &Item::getCollider()
|
||||
{
|
||||
return collider;
|
||||
}
|
||||
|
||||
// Obtiene su ubicación
|
||||
SDL_Point Item::getPos()
|
||||
{
|
||||
const SDL_Point p = {sprite->getPosX(), sprite->getPosY()};
|
||||
return p;
|
||||
}
|
||||
@@ -58,6 +58,9 @@ public:
|
||||
|
||||
// Obtiene el rectangulo de colision del objeto
|
||||
SDL_Rect &getCollider();
|
||||
|
||||
// Obtiene su ubicación
|
||||
SDL_Point getPos();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
67
source/item_tracker.cpp
Normal file
67
source/item_tracker.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "item_tracker.h"
|
||||
|
||||
// Constructor
|
||||
Item_tracker::Item_tracker()
|
||||
{
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Item_tracker::~Item_tracker()
|
||||
{
|
||||
}
|
||||
|
||||
// Comprueba si el objeto ya ha sido cogido
|
||||
bool Item_tracker::hasBeenPicked(std::string name, SDL_Point pos)
|
||||
{
|
||||
}
|
||||
|
||||
// Añade el objeto a la lista de objetos cogidos
|
||||
void Item_tracker::addItem(std::string name, SDL_Point pos)
|
||||
{
|
||||
// Primero busca si ya hay una entrada con ese nombre
|
||||
const int index = findByName(name);
|
||||
if (index != -1)
|
||||
{
|
||||
}
|
||||
|
||||
// En caso contrario crea la entrada
|
||||
else
|
||||
{
|
||||
item_tracker_t item;
|
||||
item.name = name;
|
||||
item.pos.push_back(pos);
|
||||
list.push_back(item);
|
||||
}
|
||||
}
|
||||
|
||||
// Busca una entrada en la lista por nombre
|
||||
int Item_tracker::findByName(std::string name)
|
||||
{
|
||||
const int c = -1;
|
||||
|
||||
for (int i = 0; i < list.size(); i++)
|
||||
{
|
||||
if (list[i].name == name)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
// Busca una entrada en la lista por posición
|
||||
int Item_tracker::findByPos(int index, SDL_Point pos)
|
||||
{
|
||||
const int c = -1;
|
||||
|
||||
for (int i = 0; i < list[index].pos.size(); i++)
|
||||
{
|
||||
if ((list[index].pos[i].x == pos.x) && (list[index].pos[i].y == pos.y))
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
42
source/item_tracker.h
Normal file
42
source/item_tracker.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
#include "ifdefs.h"
|
||||
#include "utils.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef ITEM_TRACKER_H
|
||||
#define ITEM_TRACKER_H
|
||||
|
||||
struct item_tracker_t
|
||||
{
|
||||
std::string name; // Nombre de la habitación donde se encuentra el objeto
|
||||
std::vector<SDL_Point> pos; // Lista de objetos cogidos de la habitación
|
||||
};
|
||||
|
||||
// Clase Item_tracker
|
||||
class Item_tracker
|
||||
{
|
||||
private:
|
||||
std::vector<item_tracker_t> list; // Lista con todos los objetos recogidos
|
||||
|
||||
// Busca una entrada en la lista por nombre
|
||||
int findByName(std::string name);
|
||||
|
||||
// Busca una entrada en la lista por posición
|
||||
int findByPos(int index, SDL_Point pos);
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Item_tracker();
|
||||
|
||||
// Destructor
|
||||
~Item_tracker();
|
||||
|
||||
// Comprueba si el objeto ya ha sido cogido
|
||||
bool hasBeenPicked(std::string name, SDL_Point pos);
|
||||
|
||||
// Añade el objeto a la lista de objetos cogidos
|
||||
void addItem(std::string name, SDL_Point pos);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -479,18 +479,17 @@ bool Room::enemyCollision(SDL_Rect &rect)
|
||||
}
|
||||
|
||||
// Indica si hay colision con un objeto a partir de un rectangulo
|
||||
bool Room::itemCollision(SDL_Rect &rect)
|
||||
SDL_Point Room::itemCollision(SDL_Rect &rect)
|
||||
{
|
||||
bool collision = false;
|
||||
|
||||
SDL_Point p = {-1, -1};
|
||||
for (auto item : item_list)
|
||||
{
|
||||
collision |= checkCollision(rect, item->getCollider());
|
||||
if (collision)
|
||||
if (checkCollision(rect, item->getCollider()))
|
||||
{
|
||||
item->pick();
|
||||
p = item->getPos();
|
||||
}
|
||||
}
|
||||
|
||||
return collision;
|
||||
return p;
|
||||
}
|
||||
@@ -98,7 +98,7 @@ public:
|
||||
bool enemyCollision(SDL_Rect &rect);
|
||||
|
||||
// Indica si hay colision con un objeto a partir de un rectangulo
|
||||
bool itemCollision(SDL_Rect &rect);
|
||||
SDL_Point itemCollision(SDL_Rect &rect);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user