From 9d9c1d29dbcd802c89641b70b5574361e775c0e4 Mon Sep 17 00:00:00 2001 From: tom Date: Mon, 23 Nov 2015 17:05:27 -0600 Subject: added collision of trees and map border --- inc/Character.hpp | 4 ++-- inc/Frame.hpp | 4 +++- inc/List.hpp | 5 +++-- inc/Location.hpp | 1 + scripts/map.txt | 22 ++++++++++---------- src/Character.cpp | 62 ++++++++++++++++++++----------------------------------- src/Frame.cpp | 42 +++++++++++++++++++++++++++++-------- src/List.cpp | 7 ++++--- src/main.cpp | 7 +++---- 9 files changed, 82 insertions(+), 72 deletions(-) diff --git a/inc/Character.hpp b/inc/Character.hpp index 1a7dcdd..a1fc7b7 100644 --- a/inc/Character.hpp +++ b/inc/Character.hpp @@ -14,8 +14,8 @@ class Character Character(char nSymbol, int nColor, Location L); void move(Location L){l=L;} void draw(WINDOW * w); - void action(vector men); - bool check(Location L, vector men); + void action(vector men, vector impassable); + bool check(Location L, vector men, vector impassable); Location getLocation(){return l;} char getSymbol(){return symbol;} diff --git a/inc/Frame.hpp b/inc/Frame.hpp index 4e4b57f..26a7364 100644 --- a/inc/Frame.hpp +++ b/inc/Frame.hpp @@ -27,15 +27,17 @@ class Frame void refresh(); void move(int nRow, int nCol); void center(Character &ch); + vector getImpassable(){return impassable;} private: int height, width; int row, col; bool hasSuper; - bool colored = false; + bool filled; WINDOW * w; WINDOW * super; vector m; + vector impassable; }; #endif diff --git a/inc/List.hpp b/inc/List.hpp index ce3f413..447903b 100644 --- a/inc/List.hpp +++ b/inc/List.hpp @@ -11,11 +11,12 @@ using namespace std; class List { public: - List(); + List(vector i); void draw(WINDOW * w); void action(); private: - vector men; + vector men; + vector impassable; }; #endif diff --git a/inc/Location.hpp b/inc/Location.hpp index 5c6f1ea..ed0e2db 100644 --- a/inc/Location.hpp +++ b/inc/Location.hpp @@ -5,6 +5,7 @@ class Location { public: Location(){x=0;y=0;} + Location(int xn, int yn){x=xn;y=yn;} int x; int y; }; diff --git a/scripts/map.txt b/scripts/map.txt index f78ab89..335e62d 100644 --- a/scripts/map.txt +++ b/scripts/map.txt @@ -144,17 +144,17 @@ O.;: ,.., ..:,:;. . ::,,: , ;:.;:;O;.:..;,,,.;,:.. ;;.;,.;. ,;;;; , , ;., ; ; :..::,;;: ,; :.,., :....; ., .,,::;.:;: ;;;:, :O0;,;;; ,;: .,;.;;; , ; ,O . :.: ;.. .,; ,,.;:;:.::;.,,;;.: : .,;; ;..,,.; ;.;;. ;: ,;;.,;: ;.,:; , ; :..:;;:.;.,;;.;.:,: ; .,;;.;;;., O : :; , .,:;.,.,;.::: .; .;,.;;,;.:,..:, ;, :,, ;.,.;.:, O:,..;:;:; .; ;.;:;.:. :::;;;::;.,; ,; ,; ;.;,:.: O. ,, ,,:.;. ;,,.::; : 0::O0 , ,.,..,;.; , ;;,: ;:0;..; :; ;,:,;;; .,;0:;;,: :,;.::, .:,;.,, .:;.;, ; :.,;,.: ,.O,; .,.:;;;,.. ;::., ;,:,:::.; ..;, ;.,:. . ;, :;:,;;O ,,;,.;. ::; ,,,;:; ;.:,. O :. ,;:,: :;..: :. :,::,.O,.:.:,;:.; ; ,: ;;O,, :, ,,,. 0 ,;.,:;:,.:0 : 0; ;O:,. ::, :..: :: .,.,,;.. : ;: . ,.:;: ; .;.;,,: :. :;..;:.;: ;0; O ,:;. : O .::,,: ::,. ;:; ;. ,,, ; : ,,;::..;:,,:; : O; ,.;:,;., ;:; .; : ;,:;,: .,, ., ,:.; .;:.,;,,::,:,,,.:; ;::;:,,:.., ;: : : : ,;, : :;: . ;.,.;.,::0 ::, ;:,::,,. ::,,:; :;::,; .., ,., ;0, ....,;;:; :.;:.;.;,.:.. ,,::;;.;,;;; 0, ;; ..: :;; ..; -; :,:::,.,;::;;, ;0:; ;, :;; ,.,.:.;,. ;, ,,. ,;:,, ;,;: ,,; ::.:,: ,::.:.....;., ;,,;;. .:.::::,;0. ;.:;:. . : : ;O;,;,;. ::; : .;.,, ,::;, :; . ,,,;,. .:; ;,,;.. :.;;.:;.;.,;::,;; :;.. ;; ;; :,:: . :,..;;, ,:, . , :,:. , :. ;; .,;.;, : ;;,..: ;; ,::.,,::;;..;;: ,;:;;.;,:,.:,: ;.:.. :;:,,. -;;., . .:..:; ; ,;:.,;:,; ;,::,.;: : ;:,,.. ,::.,. .:: . 0;;:.. O , . 0;;:; ;: ;:,.;; ,.,:.,;: ;0 ;0. ,,;, , :::,,: .: .,. ; :. :.::,:. ,;..::.:. ,O;,,,:.,,:,, ,,.,: .. ; .;.; ., ; ;;..; , ;:,..O .,;:; ;:. ,,, ;;:: :,. ,,;. ; : ::,.:;,,0: ;.:;; O.;; .;. :,;::,,;. :,; .:: ,:: :; -O.O,,;;..::. , ;;:;, ,.. 0;, ;::: :..;,;, ::.:O:: . . . .. .,::,,:;; .:, : ,., ..: ; ;,,. ;.,.:.;. ;; .,,,. ::; ., ;.;.:::.: ,.:;:,;: .::.;,;:: :;.; .,. , ,:;:.,.;. ; 0;..,: . .;; ; . ;;:.; .::. .;.,0;:;.; :;.0:,.,.::,O:;, .,,;;,.:.,.;.,, :.,; : .:;. :;; ..;;:;; ::.. ;.:;; ;:, -.: ;::. ::,: :; ..,. ::.;;;;;: ;: .;.:, .:::,::.. :,,. :;,: ,:: : ,....,:;:;;.,:.;,:,, ;:;: ;,,. ,;,.,,: :;,,::;:: :;;.;. ;;:,.. :;::. :; :O :;:;, ;:;:.;, ,::, ;;,,. .;. ,.::.;:;,;,: 0, :; : .,;;,. .O ;;:.:: ;.:, , .,;,,::: .,; ,.:::;;.:,;. ;...;;:;:, ,, ;,,: .;.;..,: :,, . , ,.:;;.0. ,.,: -O;,:,..;;:: .: : ;:. :..:;.;;,;;;.:; ,..;:,,.:,:; : ,; ::;;;::,. :, 0,,, .. ;.:,,.:,::,, :.::,,:;;,.; .;::.;,.;, ;;:, O.:. ..: ,;, :, ,,:;,.,, .,.,,:.,: ,.; .;,::;. , ,.;.: O: 0;:;:.;,.;,,:..::,::::;;:;.; ::; , ,;,,:.::; :.:,.00;:,;:;.,,.;.:. ;.,.;,,; ;. :...:,.. .O::.;.,,O;., :;.,;::; : -.: ;. :,. : .:,: ;;.;:,: O:;:, ,; ,::,:O,;: :.: .;: ,;,,:, ,;.,:.:;; ,: .,: ,::,,, :;,.0,.,:;;:;,;,.,;; .;, ;, . .;. .;;. ., :;. .:;;: ....,;;,,,.:;:O;; ;, .:. ::,..:. : ,:;. . ... , .;,,:., .;,.:;, .:.;O.;::, ;:., :: ,::..:;.:.,:. ;;; .:. ; ,:::,;;:O:,.,,:: : ;0 ::,,: ,:.,:., :..;: -,;,, : . ,;..0 O.;:,:.,. ;: , :,;:.:;: .. ,; :. .;,,.,;;,.;.: ,, ;,: ,,, ,:, :;.,.,;.;.:;;; ;,,,,.,. ::; . :.;;,.::;,;. :..: ,:;.; ;O,..:, . ., . :;.,, ;;.;.;: :;,, ,;,,;:::.; ;;::.;, ,: .: ;::.:.:..::.;.,;:. ,,,;: ;, ;.: ::::,: :.;,:.: ;, : .:;, ;.,.:.O;,. , ;,,:;.;, ;;::.. .,.:;,:,::, -:; :, ;:;..;,. :;;:: ;.: : ;.,;.; ;,.,,:; ;.:,,,;.,, :,.,;;;:,;.,;.:,.O;: :,;.;; ,.;;: ,,,... : , .;..:.;;; .:.;0:.;;,;;;,,: ...::, :,,O:; ::, ;,, ; . ,,...;,; .,:;., , :,..,.: ,.O.:0,,,;,;:,;,,; :;.:: :.,.,:,. ;,:.:;:.; ;.:, ,,,,. , , , ; ..,:.. ;.:;:,.,;,:: : ;. .,;:;,.,;,,...:;;,:,, -,, :, . ,..,: , .;;,;;,; .; ;; ;,::: :,. ,., :,,.,: :: ::;:; :.::; ,.::. : :::,.:;;;.;;,:.O . .. ; ;.;; :, :,.:.;;:;;,;.;.: ,:,; :. .: ,::; , :; ,;:.;;;O;.: ,.;:;,::. ,::; .; .;;;,,:;,:. :. ,, :. O ;0,, ;.;. ::;;.: .,; ::.; .; ;.:: .,,..;O;:...;,,,,;,:.:; ,,;;,. ,,. .;.,: .:. -;..,,.;.;.::;;,,: ,:; :;;. . O.,;,. :,,,O: :;.;,:.;..: ..,..,.,,:, ,:. ;,, .,:,.: :,:,,;; ::,,;, , ..,:... ;.;:; ;,.;;;..;.,. ,.:; ,:;, . ; . : ;;.;,,:. ::;;: . ; .::,. ,,, :::.;::,..,.; 0;;,;.,:::,:.:,:;,..;: ,:.;,: ..,O:,.; ;;,;;,.,.::,:,: .:.,:;; ,: ;:,;:, ,:,; ,;. .; :..;;::. ;, :;..;, -,,.0..., ; ,;; .:... .0;;,::. : .;;.,.,;;, ;; ;,.,;,,; ,;,, 0;;: .,:.;;:.;,:.:,.;,;, :. ;,..,: , :..;;:: ,;.;:,,, ,,:,,;, , ..;;: .:;. ,.,;0;:.;,. :O.,,;..,..,;:,:. , :; , ;; :; ,,, , ;.;, ;,;:......; ,,0,:,..:;::;: ;,0 ;;,; ; ,, ,;; ::, ::;,:;, :.:;;,::,.,;.:,....:,,;..:;; :; ,O:,,:;,;..O,. +; :,:::,.,;::;;, ;0:; ;, :;; ,.,.:.;,. ;, ,,. ,;:,, ;,;: ,,; ::.:,: ,::.:.....;., ;,,;;. .:.::::,;0. ;.:;:. . : : ;O;,;0000000000000000000000000000000000000000000.. :.;;.:;.;.,;::,;; :;.. ;; ;; :,:: . :,..;;, ,:, . , :,:. , :. ;; .,;.;, : ;;,..: ;; ,::.,,::;;..;;: ,;:;;.;,:,.:,: ;.:.. :;:,,. +;;., . .:..:; ; ,;:.,;:,; ;,::,.;: : ;:,,.. ,::.,. .:: . 0;;:.. O , . 0;;:; ;: ;:,.;; ,.,:.,;: ;0 ;0. ,,;, , :::,,: .: .,. ; :. :.::,:. ,;..::.:. ,O;,,,:.0,:,, ,,.,: .. ; .;.; ., ; ;;..; , ;:,..O .,;:; ;:. ,,, ;;:: :,. ,,;. ; : ::,.:;,,0: ;.:;; O.;; .;. :,;::,,;. :,; .:: ,:: :; +O.O,,;;..::. , ;;:;, ,.. 0;, ;::: :..;,;, ::.:O:: . . . .. .,::,,:;; .:, : ,., ..: ; ;,,. ;.,.:.;. ;; .,,,. ::; ., ;.;.:::.: ,.:;:,;: .::.;,;:: :;.;0.,. , ,:;:.,.;. ; 0;..,: . .;; ; . ;;:.; .::. .;.,0;:;.; :;.0:,.,.::,O:;, .,,;;,.:.,.;.,, :.,; : .:;. :;; ..;;:;; ::.. ;.:;; ;:, +.: ;::. ::,: :; ..,. ::.;;;;;: ;: .;.:, .:::,::.. :,,. :;,: ,:: : ,....,:;:;;.,:.;,:,, ;:;: ;,,. ,;,.,,: :;,,::;::0000:;;.;. ;;:,.. :;::. :; :O :;:;, ;:;:.;, ,::,0;;,,. .;. ,.::.;:;,;,: 0, :; : .,;;,. .O ;;:.:: ;.:, , .,;,,::: .,; ,.:::;;.:,;. ;...;;:;:, ,, ;,,: .;.;..,: :,, . , ,.:;;.0. ,.,: +O;,:,..;;:: .: : ;:. :..:;.;;,;;;.:; ,..;:,,.:,:; : ,; ::;;;::,. :, 0,,, .. ;.:,,.:,::,, :.::,,:;;,.; .;::.;,.;, ;;:, 0.:. ..: ,;, :, ,,:;,.,, .,.,,:.,: ,0; .;,::;. , ,.;.: O: 0;:;:.;,.;,,:..::,::::;;:;.; ::; , ,;,,:.::; :.:,.00;:,;:;.,,.;.:. ;.,.;,,; ;. :...:,.. .O::.;.,,O;., :;.,;::; : +.: ;. :,. : .:,: ;;.;:,: O:;:, ,; ,::,:O,;: :.: .;: ,;,,:, ,;.,:.:;; ,: .,: ,::,,, :;,.0,.,:;;:;,;,.,;; .;, ;, . 0;. .;;. ., :;. .:;;: ....,;;,,,.:;:O;0 ;, .:. ::,..:. : ,:;. . ... , .;,,:., .;,.:;, .:.;O.;::, ;:., :: ,::..:;.:.,:. ;;; .:. ; ,:::,;;:O:,.,,:: : ;0 ::,,: ,:.,:., :..;: +,;,, : . ,;..0 O.;:,:.,. ;: , :,;:.:;: .. ,; :. .;,,.,;;,.;.: ,, ;,: ,,, ,:, :;.,.,;.;.:;;; ;,,,,.,. ::; . :.;;,.::0,;. :..: ,:;.; ;O,..:, . ., . :;.,, ;0.;.;: :;,, ,;,,;:::.; ;;::.;, ,: .: ;::.:.:..::.;.,;:. ,,,;: ;, ;.: ::::,: :.;,:.: ;, : .:;, ;.,.:.O;,. , ;,,:;.;, ;;::.. .,.:;,:,::, +:; :, ;:;..;,. :;;:: ;.: : ;.,;.; ;,.,,:; ;.:,,,;.,, :,.,;;;:,;.,;.:,.O;: :,;.;; ,.;;: ,,,... : , .;..:.;;; .:.;0:.;0,;;;,,: ...::, :,,O:; ::, ;,, ; . ,,...;,; 0,:;., , :,..,.: ,.O.:0,,,;,;:,;,,; :;.:: :.,.,:,. ;,:.:;:.; ;.:, ,,,,. , , , ; ..,:.. ;.:;:,.,;,:: : ;. .,;:;,.,;,,...:;;,:,, +,, :, . ,..,: , .;;,;;,; .; ;; ;,::: :,. ,., :,,.,: :: ::;:; :.::; ,.::. : :::,.:;;;.;;,:.O . .. ; ;.;; :, :,0:.;;:;;,;.;.: ,:,; :. .: ,::; , :; ,;:.;;;O0.: ,.;:;,::. ,::; .; .;;;,,:;,:. :. ,, :. O ;0,, ;.;. ::;;.: .,; ::.; .; ;.:: .,,..;O;:...;,,,,;,:.:; ,,;;,. ,,. .;.,: .:. +;..,,.;.;.::;;,,: ,:; :;;. . O.,;,. :,,,O: :;.;,:.;..: ..,..,.,,:, ,:. ;,, .,:,.: :,:,,;; ::,,;, , ..,:... ;.;:; ;,.;;0..;.,. ,.:; ,:;, . ; . : ;;.;,,:. ::;;: .0; .::,. ,,, :::.;::,..,.; 0;;,;.,:::,:.:,:;,..;: ,:.;,: ..,O:,.; ;;,;;,.,.::,:,: .:.,:;; ,: ;:,;:, ,:,; ,;. .; :..;;::. ;, :;..;, +,,.0..., ; ,;; .:... .0;;,::. : .;;.,.,;;, ;; ;,.,;,,; ,;,, 0;;: .,:.;;:.;,:.:,.;,;, :. ;,..,: , :..;;:: ,;.;:,,, ,,:0000000000000000000000000000000000000000000000. , :; , ;; :; ,,, , ;.;, ;,;:......; ,,0,:,..:;::;: ;,0 ;;,; ; ,, ,;; ::, ::;,:;, :.:;;,::,.,;.:,....:,,;..:;; :; ,O:,,:;,;..O,. ,0., ,.;:, .,. : , ,: . :::. .: ::: :,; , ,. ,;;,:.,,: :; ,;;:, . ;. :;,:.:.:;, ,;:;,,.: ,;,, :;:. .,.,..;,,.: .::;;,: : : . : ;.;: , , ,:.. ;.. :.,;., ,::,;,:,, ,,O,,;:.. ;O,,;:O,,,,.::; ,;0.:;.,;:: ,,.: ,.:.:.,,, ,;. :.,;;,..;;;, .,,:, ;,:;: ;, ;,;, ,,;,, : .: ;... ,:.;;:,,; :. .;;,,. : ,,; ,;, ;; ;;. ,::::, ;,.: ,, ;:;,:: ,, : ::;..;: ;. ; ;;: .;. ;,:,:; ;:,;,;.:: : ; ,:;.,. ,: .,,:;,;, ,,:. 0 ., ;.:. :0; ;.:;:,,. .; ;.: ::;; .;, . ,;;,.::O;.: ; :.:,;::,;. .;., :,:. ,,:;:,,.:; , .,:: . .:.:;:,:,;, ; ;:;,,::;.;..:.. ,,;:.. :..:, .:;:;,. : ,;, ; .;.;; ;:..;.: .;.,: ,:,;;. : ,; :;;;;,;. ,: ., .: .,;.,.,. . ::::; ; . ::: O.,, ;;; .: ;: . ;.: :: ;,..,;::,;; .;. ,.::;; . .,:; ,:..: , ,,:. ::;; ;::,:;.:: ::, O; ,;: ,,.:..,. ; ;,:. ,;:O:, .:,:, :, .;;; :...0:,.;,.: ,;:,,: ;;, :;,O:;;: ...; :; ,, ;; ; :.,,; ;.: ,:; .,..,:;;.;, ; ,: : ;..;;:,: : :. ;:,O,O :,,;:.;:. diff --git a/src/Character.cpp b/src/Character.cpp index d5f8bc0..24ed475 100644 --- a/src/Character.cpp +++ b/src/Character.cpp @@ -13,65 +13,47 @@ void Character::draw(WINDOW * w) mvwaddch(w,l.x,l.y,symbol); } -void Character::action(vector men) +void Character::action(vector men, vector impassable) { if(order == "wander") - { - Location Lo; + { int r = rand()%15+1; + bool stay = false; + Location L; if(r==1) - { - Lo.x = l.x+1; - Lo.y = l.y; - } + L = Location(l.x+1,l.y); else if(r==2) - { - Lo.x = l.x-1; - Lo.y = l.y; - } + L = Location(l.x-1,l.y); else if(r==3) - { - Lo.x = l.x; - Lo.y = l.y+1; - } + L = Location(l.x,l.y+1); else if(r==4) - { - Lo.x = l.x; - Lo.y = l.y-1; - } + L = Location(l.x,l.y-1); else if(r==5) - { - Lo.x = l.x+1; - Lo.y = l.y+1; - } + L = Location(l.x+1,l.x+1); else if(r==6) - { - Lo.x = l.x-1; - Lo.y = l.y-1; - } + L = Location(l.x-1,l.y-1); else if(r==7) - { - Lo.x = l.x+1; - Lo.y = l.y-1; - } + L = Location(l.x+1,l.y-1); else if(r==8) - { - Lo.x = l.x-1; - Lo.y = l.y+1; - } + L = Location(l.x-1,l.y+1); else - Lo = l; + stay = true; - if(check(Lo,men)) - move(Lo); + if (!stay) + if(check(L,men,impassable)) + move(L); } } -bool Character::check(Location L, vector men) +bool Character::check(Location L, vector men, vector impassable) { for(int i = 0; i < men.size(); i++) if(L.x == men[i].getLocation().x && L.y == men[i].getLocation().y) return false; - + + for(int i = 0; i < impassable.size(); i++) + if(L.x == impassable[i].x && L.y == impassable[i].y) + return false; + return true; } diff --git a/src/Frame.cpp b/src/Frame.cpp index c63aeb4..ca0e41e 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -4,7 +4,7 @@ Frame::Frame(string location, int nRow, int nCol) { hasSuper = FALSE; super = NULL; - + filled = false; int i, j=0; string line; ifstream file (location); @@ -31,6 +31,7 @@ Frame::Frame(string location, int nRow, int nCol) Frame::Frame(Frame &sw, int rows, int cols, int nRow, int nCol) { + filled = false; hasSuper = TRUE; super = sw.getWin(); w = derwin(super,rows,cols,nRow,nCol); @@ -103,28 +104,51 @@ void Frame::move(int nRow, int nCol) void Frame::fillWindow() { - if(!colored) - for(int i = 0; i < width; i++) - for(int j = 0; j < height; j++) + + for(int i = 0; i < width; i++) + for(int j = 0; j < height; j++) + { + if(m[j][i] == 'O' || m[j][i] == '0') { - if(m[j][i] == 'O' || m[j][i] == '0') - wattron(w,COLOR_PAIR(2)); - else - wattron(w,COLOR_PAIR(1)); - mvwaddch(w,j,i,m[j][i]); + wattron(w,COLOR_PAIR(2)); + if(!filled) + { + Location l(j,i); + impassable.push_back(l); + } } + else + wattron(w,COLOR_PAIR(1)); + + mvwaddch(w,j,i,m[j][i]); + } wattron(w,COLOR_PAIR(3)); for(int y = 0; y < height; ++y) { mvwaddch(w, y, 0, '-'); mvwaddch(w, y, width - 1, '-'); + if(!filled) + { + Location l1(y,0); + Location l2(y,width-1); + impassable.push_back(l1); + impassable.push_back(l2); + } } for(int x = 0; x < width; ++x) { mvwaddch(w, 0, x, '|'); mvwaddch(w, height - 1, x, '|'); + if(!filled) + { + Location l1(0,x); + Location l2(height-1,x); + impassable.push_back(l1); + impassable.push_back(l2); + } } wattroff(w,COLOR_PAIR(3)); + filled = true; } diff --git a/src/List.cpp b/src/List.cpp index d4bc796..fefb37d 100644 --- a/src/List.cpp +++ b/src/List.cpp @@ -1,8 +1,8 @@ #include "List.hpp" -List::List() +List::List(vector i) { - for(int i = 0; i < 50; i++) + for(int i = 0; i < 10; i++) { Location L; L.x = 150; @@ -10,6 +10,7 @@ List::List() Character x ('@',4,L); men.push_back(x); } + impassable = i; } void List::draw(WINDOW * w) @@ -21,5 +22,5 @@ void List::draw(WINDOW * w) void List::action() { for(int i = 0; i < men.size(); i++) - men[i].action(men); + men[i].action(men,impassable); } diff --git a/src/main.cpp b/src/main.cpp index c0d59af..8f5208f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,14 +6,13 @@ int main() Frame map("scripts/map.txt",0,0); Frame view(map,s.getHeight(),s.getWidth(),0,0); - List L; - Location l; - l.x =150; - l.y =150; + Location l(150,150); Character cursor('X',3,l); map.fillWindow(); + List L(map.getImpassable()); + L.draw(map.getWin()); cursor.draw(map.getWin()); -- cgit v1.2.3