Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentationHS14/src/modules/towerdefense/TowerDefenseController.cc @ 10256

Last change on this file since 10256 was 10181, checked in by maxima, 10 years ago

towerdefenseHS14 merged.

  • Property svn:eol-style set to native
File size: 4.3 KB
RevLine 
[9213]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "TowerDefenseController.h"
30
31#include "util/Math.h"
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "worldentities/pawns/Pawn.h"
[10181]35#include "controllers/WaypointController.h"
[9213]36
37namespace orxonox
38{
[9667]39    RegisterClass(TowerDefenseController);
[9213]40
[9667]41    TowerDefenseController::TowerDefenseController(Context* context) : WaypointController(context)
[9213]42    {
43        RegisterObject(TowerDefenseController);
44
45        this->alertnessradius_ = 500;
46
47        this->patrolTimer_.setTimer(rnd(), true, createExecutor(createFunctor(&TowerDefenseController::searchEnemy, this)));
48    }
49
50    void TowerDefenseController::XMLPort(Element& xmlelement, XMLPort::Mode mode)
51    {
52        SUPER(TowerDefenseController, XMLPort, xmlelement, mode);
53
54        XMLPortParam(TowerDefenseController, "alertnessradius", setAlertnessRadius, getAlertnessRadius, xmlelement, mode).defaultValues(500.0f);
55    }
56
[10181]57
[9213]58    void TowerDefenseController::tick(float dt)
59    {
60        if (!this->isActive())
61            return;
62
63        if (this->target_)
64        {
65            this->aimAtTarget();
66
67            if (this->bHasTargetPosition_)
68                this->moveToTargetPosition();
69
70            if (this->getControllableEntity() && this->isCloseAtTarget(1000) && this->isLookingAtTarget(math::pi / 20.0f))
71                this->getControllableEntity()->fire(0);
72        }
73        else
74        {
75            SUPER(TowerDefenseController, tick, dt);
76        }
77    }
78
79
80    bool sameTeam(ControllableEntity* entity1, ControllableEntity* entity2, Gametype* gametype)
81    {
82        if (entity1 == entity2)
83            return true;
84
85        int team1 = -1;
86        int team2 = -1;
87
88        Controller* controller = 0;
89        if (entity1->getController())
90            controller = entity1->getController();
91        else
92            controller = entity1->getXMLController();
93        if (controller)
94        {
95            FormationController* ac = orxonox_cast<FormationController*>(controller);
96            if (ac)
97                team1 = ac->getTeam();
98        }
99
100        if (entity2->getController())
101            controller = entity2->getController();
102        else
103            controller = entity2->getXMLController();
104        if (controller)
105        {
106            FormationController* ac = orxonox_cast<FormationController*>(controller);
107            if (ac)
108                team2 = ac->getTeam();
109        }
110
111
112
113        return (team1 == team2 && team1 != -1);
114    }
115
116
117
118
119
120    void TowerDefenseController::searchEnemy()
121    {
122        this->patrolTimer_.setInterval(rnd());
123
124        if (!this->getControllableEntity())
125            return;
126
127        Vector3 myposition = this->getControllableEntity()->getPosition();
128        float shortestsqdistance = (float)static_cast<unsigned int>(-1);
129
130        for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it)
131        {
132            if (ArtificialController::sameTeam(this->getControllableEntity(), static_cast<ControllableEntity*>(*it), this->getGametype()))
133                continue;
134
135            float sqdistance = it->getPosition().squaredDistance(myposition);
136            if (sqdistance < shortestsqdistance)
137            {
138                shortestsqdistance = sqdistance;
139                this->target_ = (*it);
140            }
141        }
142
143        if (shortestsqdistance > (this->alertnessradius_ * this->alertnessradius_))
144            this->target_ = 0;
145    }
146}
Note: See TracBrowser for help on using the repository browser.