This commit is contained in:
Yakudami 2019-06-13 08:56:12 +02:00
commit fd2f7f494a
17 changed files with 536 additions and 0 deletions

10
include/Chaser.hpp Normal file
View File

@ -0,0 +1,10 @@
#ifndef _CHASER_HPP_
#define _CHASER_HPP_
class Chaser: public Enemy
{
public:
Chaser(int posx, int posy);
virtual void action(int px, int py);
};
#endif

14
include/Enemy.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef _ENEMY_HPP_
#define _ENEMY_HPP_
#include "../include/Entity.hpp"
class Enemy: public Entity
{
public:
virtual void action(int px, int py) = 0;
virtual bool damage(int dmg);
protected:
int hp;
int dmg;
};
#endif

25
include/Entity.hpp Normal file
View File

@ -0,0 +1,25 @@
#ifndef _ENTITY_HPP_
#define _ENTITY_HPP_
using namespace std;
class Entity
{
public:
Entity();
~Entity();
virtual void action(int px, int py) = 0;
virtual bool damage(int dmg) = 0;
static Entity* getById(int id);
int getId();
int x;
int y;
char symbol;
static void InitAction(int px, int py);
protected:
int id;
private:
static int nextId;
static vector<Entity*> entities;
};
#endif

19
include/Game.hpp Normal file
View File

@ -0,0 +1,19 @@
#ifndef _GAME_HPP_
#define _GAME_HPP_
class Map;
class Entity;
class Game
{
public:
Game();
bool run(int key);
void loadMap();
private:
void render();
void uncover();
Player* p;
};
#endif

21
include/Player.hpp Normal file
View File

@ -0,0 +1,21 @@
#ifndef _PLAYER_HPP_
#define _PLAYER_HPP_
class Player: public Entity
{
public:
Player(int posx, int posy);
bool driver(int key);
bool displayStats();
virtual void action(int px, int py){};
virtual bool damage(int dmg);
int x;
int y;
int perc;
int dmg;
private:
int hp;
int max_hp;
};
#endif

16
include/Projectile.hpp Normal file
View File

@ -0,0 +1,16 @@
#ifndef _PROJECTILE_HPP_
#define _PROJECTILE_HPP_
class Projectile: public Entity
{
public:
Projectile(int posx, int posy, int dir, int dam);
virtual void action(int px, int py);
virtual bool damage(int dmg);
int dir;
private:
int dmg;
friend class LiveForm;
};
#endif

BIN
include/Projectile.hpp.gch Normal file

Binary file not shown.

13
include/Sentry.hpp Normal file
View File

@ -0,0 +1,13 @@
#ifndef _SENTRY_HPP_
#define _SENTRY_HPP_
class Sentry: public Enemy
{
public:
Sentry(int posx, int posy);
virtual void action(int px, int py);
private:
int proximity;
};
#endif

38
src/Chaser.cpp Normal file
View File

@ -0,0 +1,38 @@
#include "../include/Chaser.hpp"
extern int map[32][32];
extern bool u_map[32][32];
Chaser::Chaser(int posx, int posy)
{
x = posx;
y = posy;
symbol = 'e';
srand (time(NULL));
hp = (rand() % 30) + 1;
dmg = (rand() % 25) + 1;
map[posx][posy] = id;
}
void Chaser::action(int px, int py)
{
if((px == x + 1 || px == x - 1) && (py == y + 1 || py == y - 1))
Entity::getById(128)->damage(dmg);
else if(px > x && (map[x + 1][y] == 32)){
map[x][y] = ' ';
x++;
map[x][y] = id;
} else if(px < x && (map[x + 1][y] == 32)){
map[x][y] = ' ';
x--;
map[x][y] = id;
} else if(py > y && (map[x][y + 1] == 32)){
map[x][y] = ' ';
y++;
map[x][y] = id;
} else if(px < y && (map[x][y - 1] == 32)){
map[x][y] = ' ';
y--;
map[x][y] = id;
}
}

12
src/Enemy.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "../include/Enemy.hpp"
extern int map[32][32];
extern bool u_map[32][32];
bool Enemy::damage(int dmg)
{
hp = hp - dmg;
if(hp <= 0)
map[x][y] = '#';
return true;
}

3
src/Enemy.d Normal file
View File

@ -0,0 +1,3 @@
src/Enemy.o: src/Enemy.cpp src/../include/Enemy.hpp
src/../include/Enemy.hpp:

42
src/Entity.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "../include/Entity.hpp"
extern int map[32][32];
extern bool u_map[32][32];
Entity::Entity()
{
id = nextId++;
entities.push_back(this);
}
Entity::~Entity()
{
for (size_t i=0; i<entities.size(); i++) {
if (entities[i] == this) {
entities[i] = entities.back();
entities.pop_back();
break;
}
}
}
int Entity::getId(){ return id; }
Entity* Entity::getById(int id)
{
for (Entity* e : entities) {
if (e->getId() == id) return e;
}
return nullptr;
}
void Entity::InitAction(int px, int py)
{
for (Entity* e : entities) {
//if(u_map[e->x][e->y])
e->action(px, py);
}
}
int Entity::nextId = 128;
vector<Entity*> Entity::entities;

125
src/Game.cpp Normal file
View File

@ -0,0 +1,125 @@
#include "../include/Game.hpp"
extern int map[32][32];
extern bool u_map[32][32];
Game::Game()
{
p = new Player(1, 15);
loadMap();
uncover();
render();
}
void Game::loadMap()
{
char buf;
stringstream ss;
fstream indexfile;
int map_count = 0;
int map_num;
// READ NUMBER OF MAPS
indexfile.open("../maps/_index", ios::in);
string bufs;
while(!indexfile.eof()) {
map_count++;
getline(indexfile, bufs);
}
indexfile.close();
// RANDOMIZING MAP CHOICE
srand (time(NULL));
map_num = rand() % (map_count - 1);
// READ MAP FROM FILE
fstream file;
ss << "../maps/map" << map_num;
file.open(ss.str(), ios::in);
for(int y=0; y<32; y++){
for(int x=0; x<32; x++){
file.get(buf);
switch(buf){
case 'e':
srand (time(NULL));
//if((rand() % 2))
new Sentry(x, y);
//else
//new Chaser(x, y);
break;
default:
map[x][y] = buf;
break;
}
}
file.get(buf);
}
file.close();
// ZEROING UNCOVER MAP (SETTING WHOLE MAP AS UNDISCOVERED)
for(int y=0; y<32; y++){
for(int x=0; x<32; x++)
u_map[x][y] = false;
}
}
void Game::uncover()
{
int startx, endx, starty, endy;
startx = p->x - p->perc;
if(startx < 0)
startx = 0;
starty = p->y - p->perc;
if(startx < 0)
startx = 0;
endx = p->x + p->perc;
if(endx > 32)
endx = 32;
endy = p->y + p->perc;
if(endy > 32)
endy = 32;
for(int y = starty; y < endy; y++)
for(int x = startx; x< endx; x++)
u_map[x][y] = true;
}
void Game::render()
{
// CLEANING SCREEN
system("cls");
for(int y=0; y<32; y++){
for(int x=0; x<32; x++)
if(!u_map[x][y])
cout << '*';
else if(map[x][y] < 128)
cout << char(map[x][y]);
else {
cout << Entity::getById(map[x][y])->symbol;
}
cout << '\n';
}
}
bool Game::run(int key)
{
if(p->driver(key)){
Entity::InitAction(p->x, p->y);
uncover();
render();
if(!p->displayStats())
return false;
}
if(p->x == 0 || p->x == 31 || p->y == 0 || p->y == 31){
loadMap();
if(p->x == 0)
p->x = 30;
else if(p->y == 0)
p->y = 30;
else if(p->x == 31)
p->x = 1;
else
p->y = 1;
}
return true;
}

82
src/Player.cpp Normal file
View File

@ -0,0 +1,82 @@
#include "../include/Player.hpp"
extern int map[32][32];
extern bool u_map[32][32];
Player::Player(int posx, int posy)
{
x = posx;
y = posy;
hp = 100;
max_hp = 100;
perc = 7;
dmg = 10;
map[x][y] = id;
symbol = 'O';
}
bool Player::displayStats()
{
if(hp > 0){
cout << "HP: " << hp << '/' << max_hp << '\n';
return true;
} else return false;
}
bool Player::damage(int dmg){
hp = hp - dmg;
return true;
}
bool Player::driver(int key)
{
switch(key){
case 'w':
if(map[x][y-1] == 32){
map[x][y] = 32;
y--;
map[x][y] = id;
}
else return false;
break;
case 's':
if(map[x][y+1] == 32){
map[x][y] = 32;
y++;
map[x][y] = id;
}
else return false;
break;
case 'a':
if(map[x-1][y] == 32){
map[x][y] = 32;
x--;
map[x][y] = id;
}
else return false;
break;
case 'd':
if(map[x+1][y] == 32){
map[x][y] = 32;
x++;
map[x][y] = id;
}
else return false;
break;
case 'i':
new Projectile(x, y, 0, dmg);
break;
case 'k':
new Projectile(x, y, 1, dmg);
break;
case 'j':
new Projectile(x, y, 2, dmg);
break;
case 'l':
new Projectile(x, y, 3, dmg);
break;
}
return true;
}

57
src/Projectile.cpp Normal file
View File

@ -0,0 +1,57 @@
#include "../include/Entity.hpp"
#include "../include/Projectile.hpp"
extern int map[32][32];
extern bool u_map[32][32];
Projectile::Projectile(int posx, int posy, int _dir, int _dmg)
{
x = posx;
y = posy;
dmg = _dmg;
dir = _dir;
switch(dir){
case 0:
symbol = 'A';
break;
case 1:
symbol = 'V';
break;
case 2:
symbol = '<';
break;
case 3:
symbol = '>';
break;
}
map[x][y] = id;
}
void Projectile::action(int x, int y)
{
map[x][y] = 32;
switch(dir){
case 0:
y--;
break;
case 1:
y++;
break;
case 2:
x--;
break;
case 3:
x++;
break;
}
if(map[x][y] != 32){
if(map[x][y] > 128)
if(Entity::getById(map[x][y])->damage(dmg))
delete Entity::getById(map[x][y]);
delete this;
} else
map[x][y] = id;
return;
}
bool Projectile::damage(int dmg) { return true; };

23
src/Sentry.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "../include/Sentry.hpp"
extern int map[32][32];
extern bool u_map[32][32];
Sentry::Sentry(int posx, int posy)
{
x = posx;
y = posy;
symbol = '=';
srand (time(NULL));
hp = (rand() % 30) + 1;
dmg = (rand() % 25) + 1;
proximity = (rand() % 3) + 1;
map[posx][posy] = id;
}
void Sentry::action(int px, int py)
{
if(abs(px - x) < proximity && abs(y - py) < proximity)
Entity::getById(128)->damage(dmg);
}

36
src/main.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <stdlib.h>
#include <ncurses.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "Entity.cpp"
#include "Projectile.cpp"
#include "Player.cpp"
#include "Enemy.cpp"
#include "Sentry.cpp"
#include "Chaser.cpp"
#include "Game.cpp"
using namespace std;
int map[32][32];
bool u_map[32][32];
int main()
{
initscr();
Game g;
int key;
for(;;){
key = getch();
if(key == 27 || !g.run(key)){
endwin();
return 0;
}
}
}