From 7f79b2e216617ff74ebd41ac02663b20ef3d0904 Mon Sep 17 00:00:00 2001 From: tom Date: Sat, 21 Nov 2015 12:30:13 -0600 Subject: began adding collision *added a location class for ease *much refractoring needed to fully implement it* --- inc/Character.hpp | 12 +++++++++- inc/Frame.hpp | 4 ++-- inc/List.hpp | 7 +++--- inc/Location.hpp | 11 +++++++++ inc/main.hpp | 1 + src/Character.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++---- src/Frame.cpp | 12 +++++----- src/List.cpp | 35 +++++++--------------------- src/main.cpp | 15 ++++++------ 9 files changed, 117 insertions(+), 50 deletions(-) create mode 100644 inc/Location.hpp diff --git a/inc/Character.hpp b/inc/Character.hpp index f2753a9..1f948f2 100644 --- a/inc/Character.hpp +++ b/inc/Character.hpp @@ -2,18 +2,28 @@ #define character_h #include +#include +#include +#include +#include "Location.hpp" +using namespace std; class Character { public: Character(char nSymbol, int nColor,int nRow, int nCol); - void move(int nRow, int nCol); + void move(Location L); void draw(WINDOW * w); + bool action(vector occupied); + bool check(Location L, vector occupied); int getRow(); int getCol(); + Location getLocation(){return l;} char getSymbol(); private: + string order = "wander"; + Location l; char symbol; int row; int col; diff --git a/inc/Frame.hpp b/inc/Frame.hpp index 1cdf063..89242ff 100644 --- a/inc/Frame.hpp +++ b/inc/Frame.hpp @@ -26,8 +26,8 @@ class Frame void fillWindow(); void refresh(); void move(int nRow, int nCol); - void add(Character &c); - void add(Character &c, int nRow, int nCol); + //void add(Character &c); + //void add(Character &c, int nRow, int nCol); void erase(Character &c); void center(Character &ch); diff --git a/inc/List.hpp b/inc/List.hpp index e997391..931ff8a 100644 --- a/inc/List.hpp +++ b/inc/List.hpp @@ -6,7 +6,7 @@ #include #include #include - +#include "Location.hpp" using namespace std; class List @@ -14,9 +14,10 @@ class List public: List(); void draw(WINDOW * w); - void actions(); + void action(); private: - vector men; + vector men; + vector occupied; }; #endif diff --git a/inc/Location.hpp b/inc/Location.hpp new file mode 100644 index 0000000..458e77e --- /dev/null +++ b/inc/Location.hpp @@ -0,0 +1,11 @@ +#ifndef location_h +#define location_h + +class Location +{ + public: + int x; + int y; +}; + +#endif diff --git a/inc/main.hpp b/inc/main.hpp index 441e0d2..86d786d 100644 --- a/inc/main.hpp +++ b/inc/main.hpp @@ -5,5 +5,6 @@ #include "Character.hpp" #include "Frame.hpp" #include "List.hpp" +#include "Location.hpp" #endif diff --git a/src/Character.cpp b/src/Character.cpp index 7f12035..fd482a9 100644 --- a/src/Character.cpp +++ b/src/Character.cpp @@ -6,18 +6,20 @@ Character::Character(char nSymbol, int nColor, int nRow, int nCol) row = nRow; col = nCol; color = nColor; + l.x = row; + l.y = col; } -void Character::move(int nRow, int nCol) +void Character::move(Location L) { - row = nRow; - col = nCol; + l.x = L.x; + l.y = L.y; } void Character::draw(WINDOW * w) { wattron(w,COLOR_PAIR(color)); - mvwaddch(w,row,col,symbol); + mvwaddch(w,l.x,l.y,symbol); } int Character::getRow() @@ -34,3 +36,63 @@ char Character::getSymbol() { return symbol; } + +bool Character::action(vector occupied) +{ + if(order == "wander") + { + Location L; + int r = rand()%15+1; + if(r==1) + { + L.x = l.x+1; + L.y = l.y; + } + else if(r==2) + { + L.x = l.x-1; + L.y = l.y; + } + else if(r==3) + { + L.x = l.x; L.y = l.y+1; + } + else if(r==4) + { + L.x = l.x; L.y = l.y-1; + } + else if(r==5) + { + L.x = l.x+1; L.y = l.y+1; + } + else if(r==6) + { + L.x = l.x-1; L.y = l.y-1; + } + else if(r==7) + { + L.x = l.x+1; L.y = l.y-1; + } + else if(r==8) + { L.x = l.x-1; L.y = l.y+1; + + } + else + return false; + + if(check(L,occupied)) + move(L); + else + return false; + + return true; + } +} + +bool Character::check(Location L, vector occupied) +{ + for(int i = 0; i < 10; i++) + if(l.x == occupied[i].x && l.y == occupied[i].y) + return true; + return true; +} diff --git a/src/Frame.cpp b/src/Frame.cpp index 4a0df5d..77c7063 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -44,17 +44,17 @@ Frame::~Frame() { delwin(w); } - +/* void Frame::add(Character &c) { mvwaddch(w,c.getRow(),c.getCol(),c.getSymbol()); } - +*/ void Frame::erase(Character &c) { mvwaddch(w,c.getRow(),c.getCol(),' '); } - +/* void Frame::add(Character &c, int nRow, int nCol) { if((nRow >= 0 && nRow < height) && (nCol >= 0 && nCol < width)) @@ -64,7 +64,7 @@ void Frame::add(Character &c, int nRow, int nCol) c.move(nRow,nCol); } } - +*/ void Frame::center(Character &ch) { if(hasSuper) @@ -72,8 +72,8 @@ void Frame::center(Character &ch) int rr = row; int cc = col; int hh, ww; - int r = ch.getRow() - height/2; - int c = ch.getCol() - width/2; + int r = ch.getLocation().x - height/2; + int c = ch.getLocation().y - width/2; getmaxyx(super, hh, ww); diff --git a/src/List.cpp b/src/List.cpp index c0d956f..94506f4 100644 --- a/src/List.cpp +++ b/src/List.cpp @@ -6,8 +6,8 @@ List::List() { Character x ('@',4,150,150+i); men.push_back(x); - } - srand(time(NULL)); + occupied.push_back(x.getLocation()); + } } void List::draw(WINDOW * w) @@ -16,31 +16,12 @@ void List::draw(WINDOW * w) men[i].draw(w); } -void List::actions() +void List::action() { for(int i = 0; i < 10; i++) - { - int x = men[i].getRow(); - int y = men[i].getCol(); - int r = rand()%15+1; - if(r==1) - men[i].move(x+1,y); - else if(r==2) - men[i].move(x-1,y); - else if(r==3) - men[i].move(x,y+1); - else if(r==4) - men[i].move(x,y-1); - else if(r==5) - men[i].move(x+1,y+1); - else if(r==6) - men[i].move(x-1,y-1); - else if(r==7) - men[i].move(x+1,y-1); - else if(r==8) - men[i].move(x-1,y+1); - else - continue; - } - + if(men[i].action(occupied)) + { + //rows[i] = 0; + //cols[i] = 0; + } } diff --git a/src/main.cpp b/src/main.cpp index 93693ee..2a33d65 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,19 +17,20 @@ int main() while(true) { char c = getch(); - + Location l = cursor.getLocation(); if (c == 'h') - cursor.move(cursor.getRow(),cursor.getCol()-1); + l.y = l.y-1; else if (c == 'l') - cursor.move(cursor.getRow(),cursor.getCol()+1); + l.y = l.y+1; else if (c == 'k') - cursor.move(cursor.getRow()-1,cursor.getCol()); + l.x = l.x+1; else if (c == 'j') - cursor.move(cursor.getRow()+1,cursor.getCol()); + l.x = l.x-1; else if (c == 'q') - break; + break; - L.actions(); + cursor.move(l); + L.action(); map.fillWindow(); L.draw(map.getWin()); cursor.draw(map.getWin()); -- cgit v1.2.3