1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY |
---|
17 | |
---|
18 | #include "laser_power_up.h" |
---|
19 | #include "factory.h" |
---|
20 | #include "state.h" |
---|
21 | #include "network_game_manager.h" |
---|
22 | |
---|
23 | #include "primitive_model.h" |
---|
24 | |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | CREATE_FACTORY(LaserPowerUp, CL_LASER_POWER_UP); |
---|
28 | |
---|
29 | LaserPowerUp::LaserPowerUp () : PowerUp(0.0, 1.0, 0.0) |
---|
30 | { |
---|
31 | this->init(); |
---|
32 | } |
---|
33 | |
---|
34 | LaserPowerUp::LaserPowerUp(const TiXmlElement* root) : PowerUp(0.0, 1.0, 0.0) |
---|
35 | { |
---|
36 | this->init(); |
---|
37 | |
---|
38 | this->loadParams(root); |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | LaserPowerUp::~LaserPowerUp () |
---|
43 | { |
---|
44 | delete this->sphereModel; |
---|
45 | delete this->sphereMaterial; |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | void LaserPowerUp::init() |
---|
50 | { |
---|
51 | this->setClassID(CL_LASER_POWER_UP, "LaserPowerUp"); |
---|
52 | this->loadModel("models/guns/test_gun.obj", 2.0); |
---|
53 | |
---|
54 | this->sphereModel = new PrimitiveModel(PRIM_SPHERE, 7, 5); |
---|
55 | this->sphereMaterial = new Material; |
---|
56 | this->sphereMaterial->setTransparency(.1); |
---|
57 | this->sphereMaterial->setDiffuse(.7, .7, .1); |
---|
58 | |
---|
59 | this->rotation = Vector(0,1,0); |
---|
60 | this->cycle = (float)rand()/RAND_MAX*M_2_PI; |
---|
61 | this->shiftDir(Quaternion((float)rand()/RAND_MAX*M_2_PI, this->rotation)); |
---|
62 | } |
---|
63 | |
---|
64 | |
---|
65 | void LaserPowerUp::loadParams(const TiXmlElement* root) |
---|
66 | { |
---|
67 | static_cast<PowerUp*>(this)->loadParams(root); |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | /** |
---|
73 | * this function is called, when two entities collide |
---|
74 | * @param entity: the world entity with whom it collides |
---|
75 | * |
---|
76 | * Implement behaviour like damage application or other miscellaneous collision stuff in this function |
---|
77 | */ |
---|
78 | void LaserPowerUp::collidesWith(WorldEntity* entity, const Vector& location) |
---|
79 | { |
---|
80 | // PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z); |
---|
81 | if (entity->isA(CL_PLAYABLE)) |
---|
82 | this->toList(OM_DEAD); |
---|
83 | } |
---|
84 | |
---|
85 | /** |
---|
86 | * this method is called every frame |
---|
87 | * @param time: the time in seconds that has passed since the last tick |
---|
88 | * |
---|
89 | * Handle all stuff that should update with time inside this method (movement, animation, etc.) |
---|
90 | */ |
---|
91 | void LaserPowerUp::tick(float dt) |
---|
92 | { |
---|
93 | this->shiftDir(Quaternion(dt, this->rotation)); |
---|
94 | this->cycle+=dt; |
---|
95 | |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * the entity is drawn onto the screen with this function |
---|
100 | * |
---|
101 | * This is a central function of an entity: call it to let the entity painted to the screen. |
---|
102 | * Just override this function with whatever you want to be drawn. |
---|
103 | */ |
---|
104 | void LaserPowerUp::draw() const |
---|
105 | { glMatrixMode(GL_MODELVIEW); |
---|
106 | glPushMatrix(); |
---|
107 | /* translate */ |
---|
108 | glTranslatef (this->getAbsCoor ().x, |
---|
109 | this->getAbsCoor ().y + cos(this->cycle*3.0)*2.0, |
---|
110 | this->getAbsCoor ().z); |
---|
111 | /* rotate */ |
---|
112 | Vector tmpRot = this->getAbsDir().getSpacialAxis(); |
---|
113 | glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
114 | this->getModel()->draw(); |
---|
115 | |
---|
116 | this->sphereMaterial->select(); |
---|
117 | this->sphereModel->draw(); |
---|
118 | glPopMatrix(); |
---|
119 | } |
---|
120 | |
---|
121 | int LaserPowerUp::writeBytes( const byte * data, int length, int sender ) |
---|
122 | { |
---|
123 | setRequestedSync( false ); |
---|
124 | setIsOutOfSync( false ); |
---|
125 | |
---|
126 | SYNCHELP_READ_BEGIN(); |
---|
127 | |
---|
128 | SYNCHELP_READ_FKT( PowerUp::writeState ); |
---|
129 | |
---|
130 | return SYNCHELP_READ_N; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | int LaserPowerUp::readBytes( byte * data, int maxLength, int * reciever ) |
---|
136 | { |
---|
137 | if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() ) |
---|
138 | { |
---|
139 | (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() ); |
---|
140 | setRequestedSync( true ); |
---|
141 | } |
---|
142 | |
---|
143 | int rec = this->getRequestSync(); |
---|
144 | if ( rec > 0 ) |
---|
145 | { |
---|
146 | *reciever = rec; |
---|
147 | |
---|
148 | SYNCHELP_WRITE_BEGIN(); |
---|
149 | |
---|
150 | SYNCHELP_WRITE_FKT( PowerUp::readState ); |
---|
151 | |
---|
152 | return SYNCHELP_WRITE_N; |
---|
153 | } |
---|
154 | |
---|
155 | *reciever = 0; |
---|
156 | return 0; |
---|
157 | } |
---|
158 | |
---|