diff --git a/Makefile b/Makefile index dbb5328..13ec6e7 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ ############################################################################# # Makefile for building: aliendefender -# Generated by qmake (2.01a) (Qt 4.6.2) on: ?? ???? 28 22:43:44 2010 +# Generated by qmake (2.01a) (Qt 4.6.2) on: ?? ???? 29 09:41:29 2010 # Project: aliendefender.pro # Template: app # Command: /usr/bin/qmake-qt4 -spec /usr/share/qt4/mkspecs/linux-g++ -unix CONFIG+=debug -o Makefile aliendefender.pro @@ -49,12 +49,16 @@ SOURCES = map.cpp \ towers.cpp \ main.cpp \ splashes.cpp \ - adcore.cpp moc_map.cpp \ + adcore.cpp \ + player.cpp \ + settreader.cpp moc_map.cpp \ moc_game_data.cpp \ moc_aliens.cpp \ moc_towers.cpp \ moc_splashes.cpp \ - moc_adcore.cpp + moc_adcore.cpp \ + moc_player.cpp \ + qrc_images.cpp OBJECTS = map.o \ game_data.o \ aliens.o \ @@ -62,12 +66,16 @@ OBJECTS = map.o \ main.o \ splashes.o \ adcore.o \ + player.o \ + settreader.o \ moc_map.o \ moc_game_data.o \ moc_aliens.o \ moc_towers.o \ moc_splashes.o \ - moc_adcore.o + moc_adcore.o \ + moc_player.o \ + qrc_images.o DIST = /usr/share/qt4/mkspecs/common/g++.conf \ /usr/share/qt4/mkspecs/common/unix.conf \ /usr/share/qt4/mkspecs/common/linux.conf \ @@ -167,7 +175,7 @@ qmake: FORCE dist: @$(CHK_DIR_EXISTS) .tmp/aliendefender1.0.0 || $(MKDIR) .tmp/aliendefender1.0.0 - $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/aliendefender1.0.0/ && $(COPY_FILE) --parents base_types.h map.h game_data.h aliens.h towers.h splashes.h adcore.h .tmp/aliendefender1.0.0/ && $(COPY_FILE) --parents map.cpp game_data.cpp aliens.cpp towers.cpp main.cpp splashes.cpp adcore.cpp .tmp/aliendefender1.0.0/ && (cd `dirname .tmp/aliendefender1.0.0` && $(TAR) aliendefender1.0.0.tar aliendefender1.0.0 && $(COMPRESS) aliendefender1.0.0.tar) && $(MOVE) `dirname .tmp/aliendefender1.0.0`/aliendefender1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/aliendefender1.0.0 + $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/aliendefender1.0.0/ && $(COPY_FILE) --parents base_types.h map.h game_data.h aliens.h towers.h splashes.h adcore.h player.h settreader.h .tmp/aliendefender1.0.0/ && $(COPY_FILE) --parents images.qrc .tmp/aliendefender1.0.0/ && $(COPY_FILE) --parents map.cpp game_data.cpp aliens.cpp towers.cpp main.cpp splashes.cpp adcore.cpp player.cpp settreader.cpp .tmp/aliendefender1.0.0/ && (cd `dirname .tmp/aliendefender1.0.0` && $(TAR) aliendefender1.0.0.tar aliendefender1.0.0 && $(COMPRESS) aliendefender1.0.0.tar) && $(MOVE) `dirname .tmp/aliendefender1.0.0`/aliendefender1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/aliendefender1.0.0 clean:compiler_clean @@ -186,47 +194,88 @@ mocclean: compiler_moc_header_clean compiler_moc_source_clean mocables: compiler_moc_header_make_all compiler_moc_source_make_all -compiler_moc_header_make_all: moc_map.cpp moc_game_data.cpp moc_aliens.cpp moc_towers.cpp moc_splashes.cpp moc_adcore.cpp +compiler_moc_header_make_all: moc_map.cpp moc_game_data.cpp moc_aliens.cpp moc_towers.cpp moc_splashes.cpp moc_adcore.cpp moc_player.cpp compiler_moc_header_clean: - -$(DEL_FILE) moc_map.cpp moc_game_data.cpp moc_aliens.cpp moc_towers.cpp moc_splashes.cpp moc_adcore.cpp + -$(DEL_FILE) moc_map.cpp moc_game_data.cpp moc_aliens.cpp moc_towers.cpp moc_splashes.cpp moc_adcore.cpp moc_player.cpp moc_map.cpp: base_types.h \ + settreader.h \ map.h /usr/bin/moc-qt4 $(DEFINES) $(INCPATH) map.h -o moc_map.cpp -moc_game_data.cpp: map.h \ +moc_game_data.cpp: player.h \ + map.h \ base_types.h \ + settreader.h \ game_data.h /usr/bin/moc-qt4 $(DEFINES) $(INCPATH) game_data.h -o moc_game_data.cpp moc_aliens.cpp: game_data.h \ + player.h \ map.h \ base_types.h \ + settreader.h \ aliens.h /usr/bin/moc-qt4 $(DEFINES) $(INCPATH) aliens.h -o moc_aliens.cpp moc_towers.cpp: game_data.h \ + player.h \ map.h \ base_types.h \ + settreader.h \ towers.h /usr/bin/moc-qt4 $(DEFINES) $(INCPATH) towers.h -o moc_towers.cpp moc_splashes.cpp: game_data.h \ + player.h \ map.h \ base_types.h \ + settreader.h \ splashes.h /usr/bin/moc-qt4 $(DEFINES) $(INCPATH) splashes.h -o moc_splashes.cpp moc_adcore.cpp: aliens.h \ game_data.h \ + player.h \ map.h \ base_types.h \ + settreader.h \ towers.h \ splashes.h \ adcore.h /usr/bin/moc-qt4 $(DEFINES) $(INCPATH) adcore.h -o moc_adcore.cpp -compiler_rcc_make_all: +moc_player.cpp: map.h \ + base_types.h \ + settreader.h \ + player.h + /usr/bin/moc-qt4 $(DEFINES) $(INCPATH) player.h -o moc_player.cpp + +compiler_rcc_make_all: qrc_images.cpp compiler_rcc_clean: + -$(DEL_FILE) qrc_images.cpp +qrc_images.cpp: images.qrc \ + images/Aliens/Al_00_12.png \ + images/Aliens/Al_00_6.png \ + images/Aliens/Al_00_2.png \ + images/Aliens/Al_00_17.png \ + images/Aliens/Al_00_13.png \ + images/Aliens/Al_00_7.png \ + images/Aliens/Al_00_3.png \ + images/Aliens/Al_00_18.png \ + images/Aliens/Al_00_10.png \ + images/Aliens/Al_00_14.png \ + images/Aliens/Al_00_8.png \ + images/Aliens/Al_00_4.png \ + images/Aliens/Al_00_0.png \ + images/Aliens/Al_00_19.png \ + images/Aliens/Al_00_11.png \ + images/Aliens/Al_00_15.png \ + images/Aliens/Al_00_9.png \ + images/Aliens/Al_00_5.png \ + images/Aliens/Al_00_1.png \ + images/Aliens/Al_00_16.png + /usr/bin/rcc -name images images.qrc -o qrc_images.cpp + compiler_image_collection_make_all: qmake_image_collection.cpp compiler_image_collection_clean: -$(DEL_FILE) qmake_image_collection.cpp @@ -240,49 +289,77 @@ compiler_yacc_impl_make_all: compiler_yacc_impl_clean: compiler_lex_make_all: compiler_lex_clean: -compiler_clean: compiler_moc_header_clean +compiler_clean: compiler_moc_header_clean compiler_rcc_clean ####### Compile map.o: map.cpp map.h \ - base_types.h + base_types.h \ + settreader.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o map.o map.cpp game_data.o: game_data.cpp game_data.h \ + player.h \ map.h \ - base_types.h + base_types.h \ + settreader.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o game_data.o game_data.cpp aliens.o: aliens.cpp aliens.h \ game_data.h \ + player.h \ map.h \ - base_types.h + base_types.h \ + settreader.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o aliens.o aliens.cpp towers.o: towers.cpp towers.h \ game_data.h \ + player.h \ map.h \ - base_types.h + base_types.h \ + settreader.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o towers.o towers.cpp -main.o: main.cpp +main.o: main.cpp adcore.h \ + aliens.h \ + game_data.h \ + player.h \ + map.h \ + base_types.h \ + settreader.h \ + towers.h \ + splashes.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp splashes.o: splashes.cpp splashes.h \ game_data.h \ + player.h \ map.h \ - base_types.h + base_types.h \ + settreader.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o splashes.o splashes.cpp adcore.o: adcore.cpp adcore.h \ aliens.h \ game_data.h \ + player.h \ map.h \ base_types.h \ + settreader.h \ towers.h \ splashes.h $(CXX) -c $(CXXFLAGS) $(INCPATH) -o adcore.o adcore.cpp +player.o: player.cpp player.h \ + map.h \ + base_types.h \ + settreader.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o player.o player.cpp + +settreader.o: settreader.cpp settreader.h + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o settreader.o settreader.cpp + moc_map.o: moc_map.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_map.o moc_map.cpp @@ -301,6 +378,12 @@ moc_splashes.o: moc_splashes.cpp moc_adcore.o: moc_adcore.cpp $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_adcore.o moc_adcore.cpp +moc_player.o: moc_player.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o moc_player.o moc_player.cpp + +qrc_images.o: qrc_images.cpp + $(CXX) -c $(CXXFLAGS) $(INCPATH) -o qrc_images.o qrc_images.cpp + ####### Install install: FORCE diff --git a/adcore.cpp b/adcore.cpp index b222f8f..98a94af 100644 --- a/adcore.cpp +++ b/adcore.cpp @@ -4,13 +4,24 @@ adCore::adCore(QObject *parent) : QThread(parent) { step = 0; - admap = new Map(1,QSize(100,100)); + admap = new Map(1); addata = new Game_Data(admap); + Player * pl1 = new Player(0); + Player * pl2 = new Player(0); + addata->players.push_back(pl1); + addata->players.push_back(pl2); adaliens = new Aliens(addata); adtowers = new Towers(addata); adsplashes = new Splashes(addata); + connect(admap,SIGNAL(RecreateAlienPath(bool*)),adaliens,SLOT(retrace(bool*))); + adaliens->AddAlien(0); + adaliens->AddAlien(0); + adaliens->AddAlien(0); + for (int i=0; imapsize.width(),admap->mapsize.height()); i++) + adtowers->AddTower(0,0,QPoint(i,i)); } + void adCore::next() { adtowers->update(); diff --git a/adcore.h b/adcore.h index 466e7b0..d0df86a 100644 --- a/adcore.h +++ b/adcore.h @@ -14,17 +14,17 @@ Q_OBJECT public: explicit adCore(QObject *parent = 0); -signals: - -public slots: - void next(); -private: Map *admap; Game_Data *addata; Aliens *adaliens; Towers *adtowers; Splashes *adsplashes; int step; +signals: + +public slots: + void next(); +private: }; #endif // ADCORE_H diff --git a/aliendefender.pro b/aliendefender.pro index 069029b..3a8f4f1 100644 --- a/aliendefender.pro +++ b/aliendefender.pro @@ -4,12 +4,19 @@ HEADERS += base_types.h \ aliens.h \ towers.h \ splashes.h \ - adcore.h + adcore.h \ + player.h \ + settreader.h SOURCES += map.cpp \ game_data.cpp \ aliens.cpp \ towers.cpp \ main.cpp \ splashes.cpp \ - adcore.cpp + adcore.cpp \ + player.cpp \ + settreader.cpp RESOURCES += images.qrc +OTHER_FILES += map.conf \ + example16x16.map \ + example16x12.map diff --git a/aliendefender.pro.user b/aliendefender.pro.user index 2f63b65..7a5b14d 100644 --- a/aliendefender.pro.user +++ b/aliendefender.pro.user @@ -1,111 +1,254 @@ - ProjectExplorer.Project.ActiveTarget + RunConfiguration0-BaseEnvironmentBase + 2 + + + RunConfiguration0-CommandLineArguments + + + + RunConfiguration0-ProFile + aliendefender.pro + + + RunConfiguration0-RunConfiguration.name + aliendefender + + + RunConfiguration0-UseDyldImageSuffix + false + + + RunConfiguration0-UseTerminal + false + + + RunConfiguration0-UserEnvironmentChanges + + + + RunConfiguration0-UserSetName + false + + + RunConfiguration0-UserSetWorkingDirectory + false + + + RunConfiguration0-UserWorkingDirectory + + + + RunConfiguration0-type + Qt4ProjectManager.Qt4RunConfiguration + + + activeRunConfiguration 0 - ProjectExplorer.Project.EditorSettings + activebuildconfiguration + Debug + + + buildConfiguration-Debug - System + Debug + 2 + 0 + 2 - ProjectExplorer.Project.Target.0 + buildconfiguration-Debug-buildstep0 - Настольный компьютер - Qt4ProjectManager.Target.DesktopTarget - 0 - 0 - - - - QtProjectManager.QMakeBuildStep - - - - - Qt4ProjectManager.MakeStep - false - - - - 2 - - - Qt4ProjectManager.MakeStep - true - - clean - - - - 1 - false - - Debug - Qt4ProjectManager.Qt4BuildConfiguration - 2 - /home/peri4/pprojects/aliendefender-build-desktop - 2 - 0 - true - - - - - QtProjectManager.QMakeBuildStep - - - - - Qt4ProjectManager.MakeStep - false - - - - 2 - - - Qt4ProjectManager.MakeStep - true - - - - 1 - false - - Release - Qt4ProjectManager.Qt4BuildConfiguration - 0 - /home/peri4/pprojects/aliendefender-build-desktop - 2 - 0 - true - - 2 - - aliendefender - Qt4ProjectManager.Qt4RunConfiguration - 2 - - aliendefender.pro - false - false - - false - false - - - 1 + Debug + + DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-oJVJLhEuxX,guid=dd326939f06256af8e013dce4c50f54e + DEFAULTS_PATH=/usr/share/gconf/gnome.default.path + DESKTOP_SESSION=gnome + DISPLAY=:0.0 + GDMSESSION=gnome + GDM_KEYBOARD_LAYOUT=us + GDM_LANG=ru_RU.UTF-8 + GNOME_DESKTOP_SESSION_ID=this-is-deprecated + GNOME_KEYRING_CONTROL=/tmp/keyring-UECX54 + GTK_MODULES=canberra-gtk-module + HOME=/home/andrey + LANG=ru_RU.UTF-8 + LD_LIBRARY_PATH=/usr/lib/qtcreator: + LOGNAME=andrey + MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path + ORBIT_SOCKETDIR=/tmp/orbit-andrey + PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games + PWD=/home/andrey + QTDIR=/usr/share/qt4 + SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1144,unix/buull-eeePC:/tmp/.ICE-unix/1144 + SHELL=/bin/bash + SPEECHD_PORT=6561 + SSH_AGENT_PID=1178 + SSH_AUTH_SOCK=/tmp/keyring-UECX54/ssh + USER=andrey + USERNAME=andrey + XAUTHORITY=/var/run/gdm/auth-for-andrey-SArBRZ/database + XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg + XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ + XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1280374094.93008-1317282182 + + + /home/andrey/AD/aliendefender/aliendefender.pro + -spec + linux-g++ + -r + CONFIG+=debug + + /usr/bin/qmake-qt4 + false + /home/andrey/AD/aliendefender - ProjectExplorer.Project.TargetCount - 1 + buildconfiguration-Debug-buildstep1 + + Debug + + DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-oJVJLhEuxX,guid=dd326939f06256af8e013dce4c50f54e + DEFAULTS_PATH=/usr/share/gconf/gnome.default.path + DESKTOP_SESSION=gnome + DISPLAY=:0.0 + GDMSESSION=gnome + GDM_KEYBOARD_LAYOUT=us + GDM_LANG=ru_RU.UTF-8 + GNOME_DESKTOP_SESSION_ID=this-is-deprecated + GNOME_KEYRING_CONTROL=/tmp/keyring-UECX54 + GTK_MODULES=canberra-gtk-module + HOME=/home/andrey + LANG=ru_RU.UTF-8 + LD_LIBRARY_PATH=/usr/lib/qtcreator: + LOGNAME=andrey + MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path + ORBIT_SOCKETDIR=/tmp/orbit-andrey + PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games + PWD=/home/andrey + QTDIR=/usr/share/qt4 + SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1144,unix/buull-eeePC:/tmp/.ICE-unix/1144 + SHELL=/bin/bash + SPEECHD_PORT=6561 + SSH_AGENT_PID=1178 + SSH_AUTH_SOCK=/tmp/keyring-UECX54/ssh + USER=andrey + USERNAME=andrey + XAUTHORITY=/var/run/gdm/auth-for-andrey-SArBRZ/database + XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg + XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ + XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1280374094.93008-1317282182 + + false + + -w + + /usr/bin/make + true + /home/andrey/AD/aliendefender + - ProjectExplorer.Project.Updater.FileVersion - 4 + buildconfiguration-Debug-cleanstep0 + + Debug + + DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-oJVJLhEuxX,guid=dd326939f06256af8e013dce4c50f54e + DEFAULTS_PATH=/usr/share/gconf/gnome.default.path + DESKTOP_SESSION=gnome + DISPLAY=:0.0 + GDMSESSION=gnome + GDM_KEYBOARD_LAYOUT=us + GDM_LANG=ru_RU.UTF-8 + GNOME_DESKTOP_SESSION_ID=this-is-deprecated + GNOME_KEYRING_CONTROL=/tmp/keyring-UECX54 + GTK_MODULES=canberra-gtk-module + HOME=/home/andrey + LANG=ru_RU.UTF-8 + LD_LIBRARY_PATH=/usr/lib/qtcreator: + LOGNAME=andrey + MANDATORY_PATH=/usr/share/gconf/gnome.mandatory.path + ORBIT_SOCKETDIR=/tmp/orbit-andrey + PATH=/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games + PWD=/home/andrey + QTDIR=/usr/share/qt4 + SESSION_MANAGER=local/buull-eeePC:@/tmp/.ICE-unix/1144,unix/buull-eeePC:/tmp/.ICE-unix/1144 + SHELL=/bin/bash + SPEECHD_PORT=6561 + SSH_AGENT_PID=1178 + SSH_AUTH_SOCK=/tmp/keyring-UECX54/ssh + USER=andrey + USERNAME=andrey + XAUTHORITY=/var/run/gdm/auth-for-andrey-SArBRZ/database + XDG_CONFIG_DIRS=/etc/xdg/xdg-gnome:/etc/xdg + XDG_DATA_DIRS=/usr/share/gnome:/usr/local/share/:/usr/share/ + XDG_SESSION_COOKIE=f0649decca52f2beb55c9ea74ad1d028-1280374094.93008-1317282182 + + true + + clean + -w + + /usr/bin/make + true + /home/andrey/AD/aliendefender + true + + clean + + + + + buildconfigurations + + Debug + + + + buildstep0 + + + + + + + buildstep1 + + + + + + buildsteps + + trolltech.qt4projectmanager.qmake + trolltech.qt4projectmanager.make + + + + cleanstep0 + + + true + + + + cleansteps + + trolltech.qt4projectmanager.make + + + + defaultFileEncoding + System + + + project + diff --git a/aliens.cpp b/aliens.cpp index 4259690..3201228 100644 --- a/aliens.cpp +++ b/aliens.cpp @@ -1,13 +1,126 @@ #include "aliens.h" +#include + Aliens::Aliens(Game_Data *gd, QObject *parent) : - QObject(parent) + QObject(parent) { gameData = gd; + nextId = 0; + Alien al; + al.speed = 0.1f; + al.health = 10; + al.type = 0; + gameData->srcAliens.push_back(al); +} + + +void Aliens::AddAlien(int srcId) +{ + if (srcId < 0 || srcId >= gameData->srcAliens.size()) qDebug("ERROR out of aliens range"),exit(-1); + Alien al = gameData->srcAliens.at(srcId); + al.Id = nextId; + al.finish = gameData->map->finishs.at(qrand()%gameData->map->finishs.size()); + qDebug()<map->finishs.size()<map->finishs.size(); + al.pos = QPointF(gameData->map->starts.at(qrand()%gameData->map->starts.size())); + al.path = gameData->map->CreatePath(al.pos.toPoint(),al.finish); + al.pathIndex = 1; + if (al.path.isEmpty()) qDebug("ERROR create path"),exit(-1); + qDebug() << al.path; + gameData->map->printMap(); + nextId++; + gameData->curAliens.push_back(al); +} + + +void Aliens::retrace(bool *OK) +{ + qDebug("retarce!"); + for (int i=0; icurAliens.size(); i++) + { + Alien al = gameData->curAliens.at(i); + al.path = gameData->map->CreatePath(al.pos.toPoint(),al.finish); + al.pathIndex = 1; + if (al.path.isEmpty()) *OK = false; + } +} + + +void Aliens::DelAlien(int index) +{ + gameData->curAliens.remove(index); +} + + +void Aliens::DelAlienById(int Id) +{ + DelAlien(AlienIndex(Id)); +} + + +int Aliens::AlienIndex(int alienId) +{ + int i=-1; + for (i=0; icurAliens.size(); i++) + if (gameData->curAliens.at(i).Id==alienId) return i; + return i; } void Aliens::update() { - + QVector missIndex; + for (int i=0; icurAliens.size(); i++) + { + Alien al = gameData->curAliens.at(i); + qDebug() << i; + //if (al.health < 0) emit AlienKill(); + float tmpdx,tmpdy,angl,arctg = 0; + qDebug() << "path index" << al.pathIndex; + tmpdx = al.pos.x() - al.path.at(al.pathIndex).x(); + tmpdy = al.pos.y() - al.path.at(al.pathIndex).y(); + while (std::sqrt(tmpdx*tmpdx +tmpdy*tmpdy) < 2*al.speed) + { + al.pathIndex++; + if (al.pathIndex >= al.path.size()) + { + qDebug("Missing alien!"); + missIndex.push_back(al.Id); + break; + } + /*{ + PathIndex = 0; + position = game->start*game->cellsize; + }*/ + tmpdx = al.pos.x() - al.path.at(al.pathIndex).x(); + tmpdy = al.pos.y() - al.path.at(al.pathIndex).y(); + //qDebug() << "next"; + } + arctg = std::atan(tmpdx/tmpdy); + if (tmpdy < 0) arctg=arctg+M_PI; + angl = 180.0f*(-arctg)/M_PI; + /*if (PathIndex > 1) + { + if ((Position.angle-angl < -5 || Position.angle-angl > 5) && angl < 175 && angl > -175) + { + if (angl > Position.angle) Position.angle += 5; + else Position.angle -= 5; + } + else Position.angle = angl; + } + else*/ + al.angle = angl; + //qDebug() << "[" << PathIndex << ";" << PicIndex << "]" << "angle:" << Position.angle << "arctg:" << arctg << "Pos:" << Position.pnt; + al.pos.setX(al.pos.x() + -al.speed*std::sin(arctg)); + al.pos.setY(al.pos.y() + -al.speed*std::cos(arctg)); + //return true; + gameData->curAliens[i] = al; + } + for (int j=0; jmissingAliens++; + DelAlienById(missIndex.at(j)); + } } diff --git a/aliens.h b/aliens.h index 7113ac5..1e41018 100644 --- a/aliens.h +++ b/aliens.h @@ -12,20 +12,21 @@ Q_OBJECT public: explicit Aliens(Game_Data * gd, QObject *parent = 0); void AddAlien(int srcId); - void DelAlienById(int Id); - void DelAlien(int index); - void retrace(); void nextWave(); void update(); + int AlienIndex(int alienId); int size() const {return gameData->curAliens.size();} signals: - void AlienFinish(); + public slots: - //void getDamage(float dmg); + void DelAlienById(int Id); + void DelAlien(int index); + void retrace(bool * OK); private: Game_Data * gameData; QPointF globStart; QPointF globFinish; + int nextId; }; #endif // ALIENS_H diff --git a/base_types.h b/base_types.h index 514ef60..1d9ab22 100644 --- a/base_types.h +++ b/base_types.h @@ -14,6 +14,7 @@ struct Alien float health; QPoint finish; QVector path; + int pathIndex; unsigned int gold; }; @@ -23,10 +24,11 @@ struct Tower int Id; int type; int AlienId; // aim alien + int PlayerId; // tower's owner QPoint pos; // not QPointF because tower fixed on grid float angle; // -180 .. 180 float range; - float speed; // shots per tick (e.g. speed=1 is 60 shots by 1sec for 60fps e.g. ) + float reload; // time for reload in ticks }; diff --git a/example16x12.map b/example16x12.map new file mode 100644 index 0000000..7394c70 Binary files /dev/null and b/example16x12.map differ diff --git a/example16x16.map b/example16x16.map new file mode 100644 index 0000000..c57917a Binary files /dev/null and b/example16x16.map differ diff --git a/game_data.cpp b/game_data.cpp index 596b9e8..8b2c801 100644 --- a/game_data.cpp +++ b/game_data.cpp @@ -4,4 +4,6 @@ Game_Data::Game_Data(Map *mapp, QObject *parent) : QObject(parent) { map = mapp; + curWave = 0; + missingAliens = 0; } diff --git a/game_data.h b/game_data.h index 53d99f2..4f23646 100644 --- a/game_data.h +++ b/game_data.h @@ -3,7 +3,7 @@ #include -#include "map.h" +#include "player.h" class Game_Data : public QObject { @@ -12,19 +12,16 @@ public: explicit Game_Data(Map * map, QObject *parent = 0); QVector srcAliens; QVector curAliens; - QVector srcTowers; QVector curTowers; - QVector srcSplashes; QVector curSplashes; QVector waves; - QVector adMoney; // vector because of many players - QVector selectAlienId; // same reason + QVector players; Map * map; int curWave; + int missingAliens; signals: public slots: - }; #endif // GAME_DATA_H diff --git a/main.cpp b/main.cpp index b9fbe58..6bfee11 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "adcore.h" @@ -7,5 +8,8 @@ int main(int argc, char *argv[]) { adCore core; QApplication a(argc, argv); + QTimer * timer = new QTimer(); + QObject::connect(timer,SIGNAL(timeout()),&core,SLOT(next())); + timer->start(100); return a.exec(); } diff --git a/map.conf b/map.conf new file mode 100644 index 0000000..4544e54 --- /dev/null +++ b/map.conf @@ -0,0 +1,3 @@ +cells path = example16x12.map +max players = 2 + diff --git a/map.cpp b/map.cpp index 27e9458..0428110 100644 --- a/map.cpp +++ b/map.cpp @@ -1,7 +1,374 @@ #include "map.h" -Map::Map(int Id, QSize size, QObject *parent) : - QObject(parent) +Map::Map(int Id, QObject *parent) : + QObject(parent) { mapId=Id; + ReadSettings(); + //CreateMapExample(); + for (int i=0; igetValue("cells path"); + maxPlayers = sreader->getValue("max players","1").toInt(); + QFile * f = new QFile(str); + f->open(QIODevice::ReadOnly); + QDataStream s(f); + s >> Cells; + if (Cells.size()>0) + { + mapsize.setWidth(Cells.size()); + mapsize.setHeight(Cells[0].size()); + qDebug() << "Map size" << mapsize; + printMap(); + } else qDebug("invalid size"); + delete f; + delete sreader; +} + + +void Map::printMap() +{ + for (int i=0; i maxPlayers) return false; + QRect r(QPoint(),mapsize); + if (!r.contains(pos)) return false; + if (Cells[pos.x()][pos.y()]==Player+playerId) + { + Cells[pos.x()][pos.y()]=PlayerTower-playerId; + return true; + } + if (Cells[pos.x()][pos.y()]==PlayerAlien+playerId) + { + Cells[pos.x()][pos.y()]=PlayerTower-playerId; + bool ok=true; + for (int i=0; iopen(QIODevice::ReadWrite); + QDataStream s(f); + s << Cells; + delete f; +} + + +QVector Map::CreatePath(QPoint start, QPoint finish) +{ + QPointF tp; + QVector srcPath; + QVector tmpPath; + QVector path; + //PathIndex = 1; + if (Cells[start.x()][start.y()] < 0) qDebug("ERROR invalid start"),exit(-1); + if (Cells[finish.x()][finish.y()] < 0) qDebug("ERROR invalid finish"),exit(-1); + srcPath = InvWaveTrace(finish,WaveTrace(start,finish)); + for (int i=0; i 0 && x < 1000) x = x+1000; + Cells[srcPath.at(i).x()][srcPath.at(i).y()] = x; + tmpPath.push_back(QPointF(srcPath.at(i))); + } + srcPath.clear(); + //qDebug() << tmpPath.size(); + if (!tmpPath.isEmpty()) + { + for (int j=0; j<4; j++) + { + path.clear(); + tp = tmpPath.at(0); + path.push_back(tp); + for (int i = 0; i < tmpPath.size() - 1; i++) + { + //if (j > 1) path.push_back(tmpPath[i]); + tp.setX((tmpPath.at(i).x() + tmpPath.at(i + 1).x()) / 2.0); + tp.setY((tmpPath.at(i).y() + tmpPath.at(i + 1).y()) / 2.0); + path.push_back(tp); + } + tp = tmpPath.at(tmpPath.size() - 1); + path.push_back(tp); + tmpPath = path; + } + tmpPath.clear(); + if (path.size() >= 6) + { + path.remove(1); + path.remove(1); + path.remove(path.size()-2); + path.remove(path.size()-2); + } + return path; + } + path.clear(); + return path; +} + + +int Map::WaveTrace(QPoint start, QPoint finish) +{ + bool stop = false; + int step = 2; + QPoint cp, tp; + QRect fr(QPoint(), mapsize); + QVector tmpp, curp; + TmpCells << Cells; + for (int i = 0; i < Cells.size(); i++) + { + for (int j = 0; j < Cells.at(i).size(); j++) + { + if (Cells[i][j] < 0 ) TmpCells[i][j] = -1; + else TmpCells[i][j] = 0; + } + } + cp = start; + curp.push_back(cp); + TmpCells[cp.x()][cp.y()] = 1; + //qDebug() << "trace"; + while (!stop) { + tmpp = curp; + curp.clear(); + stop = true; + for (int i = 0; i < tmpp.size(); i++) { + cp = tmpp.at(i); + if (cp == finish) { + TmpCells[cp.x()][cp.y()] = step; + //qDebug() << "trace done!"; + return step; + } + tp.setX(cp.x() - 1); + tp.setY(cp.y()); + if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { + TmpCells[tp.x()][tp.y()] = step; + curp.push_back(tp); + stop = false; + } + tp.setX(cp.x() + 1); + if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { + TmpCells[tp.x()][tp.y()] = step; + curp.push_back(tp); + stop = false; + } + tp.setX(cp.x()); + tp.setY(cp.y() - 1); + if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { + TmpCells[tp.x()][tp.y()] = step; + curp.push_back(tp); + stop = false; + } + tp.setY(cp.y() + 1); + if (fr.contains(tp) && TmpCells[tp.x()][tp.y()] == 0) { + TmpCells[tp.x()][tp.y()] = step; + curp.push_back(tp); + stop = false; + } + } + step++; + } + //qDebug() << "trace false"; + qDebug() << TmpCells; + return -1; +} + + +QVector Map::InvWaveTrace(QPoint finish, int cnt) +{ + QPoint wp, Ppnt; + QVector alpath; + if (cnt < 2) return alpath; + int Ind, c, xpp, ypp, xnn, ynn; + unsigned char chk; + Ppnt = wp = finish; + xnn=0; + xpp=0; + ynn=0; + ypp=0; + cnt--; + alpath.push_front(Ppnt); + while (cnt > 1) + { + cnt--; + chk = 0; + Ind = 0; + c = 0; + if (wp.x() - 1 >= 0 && TmpCells[wp.x()-1][wp.y()] == cnt) + { + chk = chk | 0x01; + c++; + } + if (wp.x() + 1 < mapsize.width() && TmpCells[wp.x()+1][wp.y()] == cnt) + { + chk = chk | 0x02; + c++; + } + if (wp.y() - 1 >= 0 && TmpCells[wp.x()][wp.y()-1] == cnt) + { + chk = chk | 0x04; + c++; + } + if (wp.y() + 1 < mapsize.height() && TmpCells[wp.x()][wp.y()+1] == cnt) + { + chk = chk | 0x08; + c++; + } + if (c == 0 || chk == 0) qDebug("ERROR in InvWaveTrace"),exit(-1); + if (c > 1) + { + if ((chk & 0x01)==0x01 && (chk & 0x04)==0x04) + { + if (xnn <= ynn && Ind == 0){ + wp.rx()--; + xnn++; + if (xnn == ynn) xnn++; + Ind = 1; + } else if (Ind == 0) { + wp.ry()--; + ynn++; + ynn++; + Ind = 1; + } + } + if ((chk & 0x02)==0x02 && (chk & 0x04)==0x04) + { + if (xpp <= ynn && Ind == 0){ + wp.rx()++; + xpp++; + if (xpp == ynn) xpp++; + Ind = 1; + } else if (Ind == 0) { + wp.ry()--; + ynn++; + ynn++; + Ind = 1; + } + } + if ((chk & 0x01)==0x01 && (chk & 0x08)==0x08) + { + if (xnn <= ypp && Ind == 0){ + wp.rx()--; + xnn++; + if (xnn == ypp) xnn++; + Ind = 1; + } else if (Ind == 0) { + wp.ry()++; + ypp++; + ypp++; + Ind = 1; + } + } + if ((chk & 0x02)==0x02 && (chk & 0x08)==0x08) + { + if (xpp <= ypp && Ind == 0){ + wp.rx()++; + xpp++; + if (xpp == ypp) xpp++; + Ind = 1; + } else if (Ind == 0) { + wp.ry()++; + ypp++; + ypp++; + Ind = 1; + } + } + } + if (c == 1 || Ind == 0) + { + xnn=0; + xpp=0; + ynn=0; + ypp=0; + if ((chk & 0x01)==0x01) { + wp.rx()--; + xnn++; + } + else if ((chk & 0x02)==0x02) { + wp.rx()++; + xpp++; + } + else if ((chk & 0x04)==0x04) { + wp.ry()--; + ynn++; + } + else if ((chk & 0x08)==0x08) { + wp.ry()++; + ypp++; + } + } + Ppnt = wp; + alpath.push_front(Ppnt); + } + return alpath; +} + diff --git a/map.h b/map.h index ad6a07c..143eb6d 100644 --- a/map.h +++ b/map.h @@ -5,26 +5,62 @@ #include #include #include +#include +#include #include "base_types.h" - +#include "settreader.h" class Map : public QObject { Q_OBJECT public: - explicit Map(int Id, QSize size, QObject *parent = 0); + + enum CellType + { + Player = 1, + PlayerAlien = 1001, + PlayerTower = -1, + Wall = -1000, + Start = 9999, + Finish = 8888 + }; + // 1 - free for player 1 + // 2 - free for player 2 + // 3 - -/- + // 1001 - player 1, free, but it is alien path (for optimization) + // 1002 - -/- + // 99999 - start field or finish field + // -1 - player 1 tower + // -2 - player 2 tower + // -3 - -/- + // -1000 - it is wall or some place where you can't build + + QVector < QVector > Cells; + QVector < QVector > TmpCells; + QVector starts; + QVector finishs; + QSize mapsize; + int maxPlayers; + + explicit Map(int Id, QObject *parent = 0); int Id() const {return mapId;} - int addTowerOnMap(); - int delTowerOnMap(); + bool addTowerOnMap(int playerId, QPoint pos); + void delTowerOnMap(QPoint pos); QVector CreatePath(QPoint start, QPoint finish); + QSize size() const {return mapsize;} + void ReadSettings(); signals: - void RecreateAlienPath(); + void RecreateAlienPath(bool * pathOK); public slots: + void printMap(); private: - int **Cells; int mapId; + + QVector InvWaveTrace(QPoint finish, int cnt); + int WaveTrace(QPoint start, QPoint finish); + void CreateMapExample(); }; #endif // MAP_H diff --git a/player.cpp b/player.cpp new file mode 100644 index 0000000..0a29f9a --- /dev/null +++ b/player.cpp @@ -0,0 +1,8 @@ +#include "player.h" + +Player::Player(int Id, QObject *parent) : + QObject(parent) +{ + playerId = Id; + adMoney = 100; +} diff --git a/player.h b/player.h new file mode 100644 index 0000000..94b712a --- /dev/null +++ b/player.h @@ -0,0 +1,25 @@ +#ifndef PLAYER_H +#define PLAYER_H + +#include + +#include "map.h" + +class Player : public QObject +{ +Q_OBJECT +public: + explicit Player(int Id, QObject *parent = 0); + unsigned long int adMoney; + int selectAlienId; + QVector srcTowers; + QVector srcSplashes; + int playerId; +signals: + +public slots: + +private: +}; + +#endif // PLAYER_H diff --git a/settreader.cpp b/settreader.cpp new file mode 100644 index 0000000..e647e7a --- /dev/null +++ b/settreader.cpp @@ -0,0 +1,254 @@ +#include + +using namespace std; + + +SettingsReader::SettingsReader(const QString & fname) { + isFile = true; + file = new QFile(fname); + file->open(QIODevice::ReadWrite); + stream = new QTextStream(file); + parse(); +} + + +SettingsReader::SettingsReader(QString * string) { + isFile = false; + str = string; + stream = new QTextStream(string); + parse(); +} + + +SettingsReader::~SettingsReader() { + settname.clear(); + settval.clear(); + settcom.clear(); + setttab.clear(); + settlines.clear(); + setttype.clear(); + all.clear(); + stream->setDevice(0); + delete stream; + if (isFile) { + file->close(); + delete file; + } +} + + +QString SettingsReader::getValue(QString vname, QString def, bool * exist) { + for (int i = 0; i < settname.size(); i++) { + if (settname.at(i) == vname) { + if (exist != 0) *exist = true; + return settval.at(i); + } + } + if (exist != 0) *exist = false; + return def; +} + + +void SettingsReader::addLine(const QString & name, const QString & value, const QString & type) { + if (setttab.size() > 0 && all.size() > 0) *stream << setttab.at(setttab.size() - 1) << name << " = " << value << " #" << type << endl; + else *stream << name << " = " << value << " #" << type << endl; + settname.append(name); + settval.append(value); + settcom.append(""); + setttype.append(type); + if (setttab.size() > 0) setttab.append(setttab.at(setttab.size() - 1)); + else setttab.append(" "); + settlines.push_back(all.size()); + all.append(name + " = " + value + " #" + type); + stream->flush(); + if (isFile) file->flush(); +} + + +void SettingsReader::insertLine(int number, const QString & name, const QString & value, const QString & type) { + if (number >= settname.size()) { + addLine(name, value, type); + return; + } + settname.insert(number, name); + settval.insert(number, value); + settcom.insert(number, ""); + setttab.insert(number, setttab.at(number)); + setttype.insert(number, type); + settlines.insert(number, settlines.at(number)); + for (int i = number + 1; i < settlines.size(); i++) settlines[i]++; + all.insert(settlines.at(number), name + " = " + value + " #" + type); + stream->flush(); + if (isFile) file->flush(); + writeAll(); +} + + +int SettingsReader::numValues() { + return settval.size(); +} + + +QString SettingsReader::getValue(int number) { + return settval.at(number); +} + + +QString SettingsReader::getName(int number) { + return settname.at(number); +} + + +QString SettingsReader::getComment(int number) { + return settcom.at(number); +} + + +char SettingsReader::getType(int number) { + return setttype.at(number)[0].cell(); +} + + +int SettingsReader::getNumber(const QString & name) { + for (int i = 0; i < settname.size(); i++) { + if (settname.at(i) == name) { + return i; + } + } + return -1; +} + + +void SettingsReader::setValue(int number, const QString & value, bool write) { + QString tmp = settname.at(number) + " = " + value + " #" + setttype.at(number) + " " + settcom.at(number); + settval.replace(number, value); + all.replace(settlines[number], tmp); + if (write) writeAll(); +} + + +void SettingsReader::setValue(const QString & name, const QString & value, bool write) { + int number = getNumber(name); + if (number == -1) return; + QString tmp = settname.at(number) + " = " + value + " #" + setttype.at(number) + " " + settcom.at(number); + settval.replace(number, value); + all.replace(settlines[number], tmp); + if (write) writeAll(); +} + + +void SettingsReader::setName(int number, const QString & name) { + QString tmp = name + " = " + settval.at(number) + " #" + setttype.at(number) + " " + settcom.at(number); + settname.replace(number, name); + all.replace(settlines[number], tmp); + writeAll(); +} + + +void SettingsReader::setType(int number, const QString & type) { + QString tmp = settname.at(number) + " = " + settval.at(number) + " #" + type + " " + settcom.at(number); + setttype.replace(number, type); + all.replace(settlines[number], tmp); + writeAll(); +} + + +void SettingsReader::setComment(int number, const QString & comment) { + QString tmp = settname.at(number) + " = " + settval.at(number) + " #" + setttype.at(number) + " " + comment; + settcom.replace(number, comment); + all.replace(settlines[number], tmp); + writeAll(); +} + + +void SettingsReader::deleteLine(const QString & name) { + bool exist = false; + int i; + for (i = 0; i < settname.size(); i++) { + if (settname.at(i) == name) { + exist = true; + break; + } + } + if (!exist) return; + deleteLine(i); +} + + +void SettingsReader::deleteLine(int number) { + settname.remove(number); + settval.remove(number); + settcom.remove(number); + setttab.remove(number); + setttype.remove(number); + all.remove(settlines[number]); + for (int i = number; i < settlines.size(); i++) settlines[i]--; + settlines.remove(number); + writeAll(); +} + + +void SettingsReader::writeAll() { + if (isFile) file->resize(0); + else *str = ""; + stream->seek(0); + int c = 0; + for (int i = 0; i < all.size(); i++) { + if (c < settlines.size() && c < setttab.size()) { + if (settlines[c] == i) { + *stream << setttab.at(c); + c++; + } + } + *stream << all.at(i) << endl; + } + if (isFile) file->flush(); + readAll(); +} + + +void SettingsReader::readAll() { + settname.clear(); + settval.clear(); + settcom.clear(); + setttab.clear(); + settlines.clear(); + setttype.clear(); + all.clear(); + if (isFile) file->flush(); + parse(); +} + + +void SettingsReader::parse() { + QString str, tab, comm; + int ind, sind; + stream->seek(0); + lines = 0; + if (isFile) if (!file->exists()) return; + while (!stream->atEnd()) { + str = stream->readLine(); + tab = str.left(str.indexOf(str.trimmed().left(1))); + str = str.trimmed(); + all.append(str); + ind = str.indexOf('='); + if ((ind > 0) && !(str.left(1) == "#")) { + sind = str.indexOf('#'); + if (sind > 0) { + comm = str.right(str.length() - sind - 1).trimmed(); + setttype.append(comm.left(1)); + comm = comm.right(comm.length() - 1).trimmed(); + settcom.append(comm); + str.truncate(sind); + } else { + setttype.append("s"); + settcom.append(""); + } + settname.append((str.left(ind)).trimmed()); + settval.append((str.right(str.length() - ind - 1)).trimmed()); + setttab.append(tab); + settlines.push_back(lines); + } + lines++; + } +} diff --git a/settreader.h b/settreader.h new file mode 100644 index 0000000..953bbcb --- /dev/null +++ b/settreader.h @@ -0,0 +1,54 @@ +#ifndef __SETT_READER_H__ +#define __SETT_READER_H__ + +#include +#include +#include +#include +#include +#include + +class SettingsReader +{ +public: + SettingsReader(const QString & fname); + SettingsReader(QString * string); + ~SettingsReader(); + + QString getValue(QString vname, QString def = "", bool * exist = 0); + QString getValue(int number); + QString getName(int number); + QString getComment(int number); + char getType(int number); + int getNumber(const QString & name); + void setValue(int number, const QString & value, bool write = true); + void setValue(const QString & name, const QString & value, bool write = true); + void setName(int number, const QString & name); + void setType(int number, const QString & type); + void setComment(int number, const QString & comment); + int numValues(); + void addLine(const QString & name, const QString & value, const QString & type = "s"); + void insertLine(int number, const QString & name, const QString & value, const QString & type = "s"); + void deleteLine(const QString & name); + void deleteLine(int number); + void readAll(); + void writeAll(); + +private: + void parse(); + + bool isFile; + int lines; + QFile * file; + QString * str; + QTextStream * stream; + QVector settname; + QVector settval; + QVector settcom; + QVector setttab; + QVector setttype; + QVector all; + QVector settlines; +}; + +#endif diff --git a/towers.cpp b/towers.cpp index bc5d340..221bcce 100644 --- a/towers.cpp +++ b/towers.cpp @@ -4,8 +4,49 @@ Towers::Towers(Game_Data *gd, QObject *parent) : QObject(parent) { gameData = gd; + nextId = 0; + Tower tw; + for (int i=0; iplayers.size(); i++) + gameData->players[i]->srcTowers.push_back(tw); } +bool Towers::AddTower(int playerId, int srcId, QPoint pos) +{ + if (playerId < 0 || playerId >= gameData->players.size()) qDebug("ERROR out of players range"),exit(-1); + if (srcId < 0 || srcId >= gameData->players.at(playerId)->srcTowers.size()) qDebug("ERROR out of towers range"),exit(-1); + Tower tw = gameData->players.at(playerId)->srcTowers.at(srcId); + tw.Id = nextId; + if (gameData->map->addTowerOnMap(playerId,pos)) + { + tw.pos = pos; + gameData->map->printMap(); + nextId++; + gameData->curTowers.push_back(tw); + return true; + } + return false; +} + + +void Towers::DelTower(int index) +{ + gameData->curTowers.remove(index); +} + + +void Towers::DelTowerById(int Id) +{ + DelTower(TowerIndex(Id)); +} + + +int Towers::TowerIndex(int towerId) +{ + int i=-1; + for (i=0; icurTowers.size(); i++) + if (gameData->curTowers.at(i).Id==towerId) return i; + return i; +} void Towers::update() { diff --git a/towers.h b/towers.h index 00e0593..8940bed 100644 --- a/towers.h +++ b/towers.h @@ -10,9 +10,11 @@ class Towers : public QObject Q_OBJECT public: explicit Towers(Game_Data * gd, QObject *parent = 0); - void AddTower(int srcId); + bool AddTower(int playerId, int srcId, QPoint pos); void DelTowerById(int Id); + void DelTowerByPos(QPoint pos); void DelTower(int index); + int TowerIndex(int towerId); void update(); int size() const {return gameData->curTowers.size();} signals: @@ -21,6 +23,8 @@ public slots: private: Game_Data * gameData; + + int nextId; }; #endif // TOWERS_H