Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/invader/InvaderShip.cc @ 9939

Last change on this file since 9939 was 9939, checked in by jo, 10 years ago

presentationHS13 branch merged into trunk

File size: 5.2 KB
Line 
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 *      Florian Zinggeler
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file InvaderShip.cc
31    @brief Implementation of the InvaderShip class.
32*/
33
34#include "InvaderShip.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38#include "Invader.h"
39
40namespace orxonox
41{
42    RegisterClass(InvaderShip);
43
44    InvaderShip::InvaderShip(Context* context) : SpaceShip(context)
45    {
46        RegisterObject(InvaderShip);
47
48        speed = 500;
49        isFireing = false;
50        damping = 10;
51    }
52
53    void InvaderShip::tick(float dt)
54    {
55        Vector3 pos = getPosition();
56
57        //Movement calculation
58        lastTimeFront += dt * damping;
59        lastTimeLeft += dt * damping;
60        lastTime += dt;
61
62        velocity.x = interpolate(clamp(lastTimeLeft, 0.0f, 1.0f), desiredVelocity.x, 0.0f);
63        velocity.y = interpolate(clamp(lastTimeFront, 0.0f, 1.0f), desiredVelocity.y, 0.0f);
64
65        //Execute movement
66        if (this->hasLocalController())
67        {
68            float dist_y = velocity.y * dt;
69            float dist_x = velocity.x * dt;
70            if(dist_y + posforeward > -42*3 && dist_y + posforeward < 42*6)
71                posforeward += dist_y;
72            else
73            {
74                velocity.y = 0;
75                // restart if game ended
76                if (getGame())
77                    if (getGame()->bEndGame)
78                    {
79                        getGame()->start();
80                        return;
81                    }
82            }
83            if (pos.z + dist_x > 42*2.5 || pos.z + dist_x < -42*3)
84                velocity.x = 0;
85            pos += Vector3(1000 + velocity.y, 0, velocity.x) * dt;
86        }
87
88        // shoot!
89        if (isFireing)
90            ControllableEntity::fire(0);
91
92        // Camera
93        WeakPtr<Camera> camera = this->getCamera();
94        if (camera != NULL)
95        {
96            camera->setPosition(Vector3(-pos.z, -posforeward, 0));
97            camera->setOrientation(Vector3::UNIT_Z, Degree(90));
98        }
99
100
101
102        // bring back on track!
103        if(pos.y != 0)
104            pos.y = 0;
105
106        setPosition(pos);
107        setOrientation(Vector3::UNIT_Y, Degree(270));
108
109        // Level up!
110        if (pos.x > 42000)
111        {
112            updateLevel();
113            setPosition(Vector3(0, 0, pos.z)); // pos - Vector3(30000, 0, 0)
114        }
115
116        SUPER(InvaderShip, tick, dt);
117    }
118
119    void InvaderShip::updateLevel()
120    {
121        lastTime = 0;
122        if (getGame())
123            getGame()->levelUp();
124    }
125
126    void InvaderShip::moveFrontBack(const Vector2& value)
127    {
128        lastTimeLeft = 0;
129        desiredVelocity.x = -value.x * speed;
130    }
131
132    void InvaderShip::moveRightLeft(const Vector2& value)
133    {
134        lastTimeFront = 0;
135        desiredVelocity.y = value.y * speed * 42;
136    }
137    void InvaderShip::boost(bool bBoost)
138    {
139        isFireing = bBoost;
140    }
141    inline bool InvaderShip::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
142    {
143        // orxout() << "touch!!! " << endl; //<< otherObject << " at " << contactPoint;
144        WeakPtr<InvaderEnemy> enemy = orxonox_cast<InvaderEnemy*>(otherObject);
145        WeakPtr<Projectile> shot = orxonox_cast<Projectile*>(otherObject);
146        // ensure that this gets only called once per enemy.
147        if (enemy != NULL && lastEnemy != enemy)
148        {
149            lastEnemy = enemy;
150
151            removeHealth(20);
152            if (getGame())
153            {
154                getGame()->multiplier = 1;                   
155            }
156        }
157        // was shot, decrease multiplier
158        else if (shot != NULL  && lastShot != shot)
159        {
160            if (getGame() && orxonox_cast<InvaderEnemy*>(shot->getShooter()) != NULL)
161            {
162                if (getGame()->multiplier > 1)
163                {
164                    lastShot = shot;
165                    getGame()->multiplier -= 1;     
166                }
167            }
168        }
169        return false;
170        // SUPER(InvaderShip, collidesAgainst, otherObject, contactPoint);
171    }
172
173    WeakPtr<Invader> InvaderShip::getGame()
174    {
175        if (game == NULL)
176        {
177            for (ObjectList<Invader>::iterator it = ObjectList<Invader>::begin(); it != ObjectList<Invader>::end(); ++it)
178                game = *it;
179        }
180        return game;
181    }
182
183    void InvaderShip::death()
184    {
185        getGame()->costLife();
186        SpaceShip::death();
187    }
188}
Note: See TracBrowser for help on using the repository browser.