Agora ele olha pra frente deitado
[Projeto-PCG.git] / weaponmanager.cpp
blob0d1e974fd80a44c4a4ffa59602edf2001274fbd4
1 #include "weaponmanager.h"
2 #include "luaenv.h"
3 #include "weapon.h"
4 #include "weaponitem.h"
5 #include "game.h"
6 #include "shotmanager.h"
7 #include "shot.h"
8 #include "usereventtype.h"
10 WeaponManager::WeaponManager(Game* g) {
11 lstate = NULL;
12 game = g;
15 std::list<Weapon*> *_weapons = NULL;
16 Game *_game = NULL;
17 std::map<std::string, Polygon> *_collision = NULL;
19 static int regweapon (lua_State *L) {
20 Ponto r,l,tip;
21 r.x = lua_tonumber(L, 1);
22 r.y = lua_tonumber(L, 2);
23 l.x = lua_tonumber(L, 3);
24 l.y = lua_tonumber(L, 4);
25 Weapon* newweapon = new Weapon(_game,L);
26 newweapon->name = lua_tostring (L, 5);
27 newweapon->fireRate = lua_tonumber(L, 6);
28 tip.x = lua_tonumber(L, 7);
29 tip.y = lua_tonumber(L, 8);
30 newweapon->setRightHand(r);
31 newweapon->setLeftHand(l);
32 newweapon->setTip(tip);
33 lua_pushlightuserdata(L, newweapon);
34 _weapons->push_front(newweapon);
35 return 1;
38 static int regfirefunction (lua_State *L) {
39 lua_settable(L, LUA_REGISTRYINDEX);
42 static int regspriteline (lua_State *L) {
43 Weapon* w = (Weapon*)lua_touserdata(L, 1);
44 Ponto a,b;
45 a.x = lua_tonumber(L, 2);
46 a.y = lua_tonumber(L, 3);
47 b.x = lua_tonumber(L, 4);
48 b.y = lua_tonumber(L, 5);
49 Linha linha(a,b);
50 linha.color[0] = lua_tonumber(L, 6);
51 linha.color[1] = lua_tonumber(L, 7);
52 linha.color[2] = lua_tonumber(L, 8);
53 w->sprite.addLinha(linha);
54 return 0;
57 static int regspritelineshot (lua_State *L) {
58 Shot* w = (Shot*)lua_touserdata(L, 1);
59 Ponto a,b;
60 a.x = lua_tonumber(L, 2);
61 a.y = lua_tonumber(L, 3);
62 b.x = lua_tonumber(L, 4);
63 b.y = lua_tonumber(L, 5);
64 Linha linha(a,b);
65 linha.color[0] = lua_tonumber(L, 6);
66 linha.color[1] = lua_tonumber(L, 7);
67 linha.color[2] = lua_tonumber(L, 8);
68 w->sprite.addLinha(linha);
69 return 0;
72 static void deleteShotFunc(void* param) {
73 Shot* shot = (Shot*) param;
74 delete shot;
77 static Uint32 deleteShotCallback(Uint32 interval, void *param) {
78 SDL_Event event;
79 SDL_UserEvent userevent;
80 userevent.type = SDL_USEREVENT;
81 userevent.code = FUNCTIONCALL;
82 userevent.data1 = (void*)deleteShotFunc;
83 userevent.data2 = (void*)param;
85 event.type = SDL_USEREVENT;
86 event.user = userevent;
88 SDL_PushEvent(&event);
90 return 0;
94 static int createshot (lua_State *L) {
95 double x,y,angle,rate,gravity,speed;
96 int damage;
97 Weapon* weapon;
98 x = lua_tonumber(L, 1);
99 y = lua_tonumber(L, 2);
100 angle = lua_tonumber(L, 3);
101 rate = lua_tonumber(L, 4);
102 weapon = (Weapon*)lua_touserdata(L, 5);
103 gravity = lua_tonumber(L, 6);
104 speed = lua_tonumber(L, 7);
105 damage = lua_tonumber(L, 8);
107 x += weapon->position.x;
108 y += weapon->position.y;
110 Shot* newshot = new Shot(x,y,angle,speed,gravity,weapon,damage);
111 weapon->game->shotManager->addShot(newshot);
112 SDL_AddTimer(rate*_game->rate,deleteShotCallback,newshot);
113 lua_pushlightuserdata(L, newshot);
114 return 1;
117 static int regcollision(lua_State *L) {
118 const char* nome = lua_tostring(L, 1);
119 double x1, y1, x2, y2;
120 x1 = lua_tonumber(L, 2);
121 y1 = lua_tonumber(L, 3);
122 x2 = lua_tonumber(L, 4);
123 y2 = lua_tonumber(L, 5);
124 Linha l(x1, y1, x2, y2);
125 if (_collision->find(nome) == _collision->end()) {
126 Polygon p;
127 (*_collision)[nome] = p;
129 (*_collision)[nome].addLinha(l);
130 return 0;
133 Weapon* WeaponManager::getWeapon(std::string name) {
134 std::list<Weapon*>::iterator it;
135 for (it = weapons.begin(); it != weapons.end(); it++) {
136 if ((*it)->name == name)
137 return (*it);
139 return NULL;
142 Polygon WeaponManager::getCollision(std::string name) {
143 return collision[name];
146 WeaponItem* WeaponManager::getItem(std::string name) {
147 return new WeaponItem(getWeapon(name), getCollision(name));
150 void WeaponManager::loadWeapons() {
151 if (lstate != NULL)
152 lua_close(lstate);
153 std::list<Weapon*>::iterator it;
154 for (it = weapons.begin(); it != weapons.end(); it++)
155 delete (*it);
156 weapons.clear();
157 _weapons = &weapons;
158 _game = game;
159 collision.clear();
160 _collision = &collision;
161 lstate = newState();
162 registerFunction(lstate,"regweapon",regweapon);
163 registerFunction(lstate,"regspriteline",regspriteline);
164 registerFunction(lstate,"regspritelineshot",regspritelineshot);
165 registerFunction(lstate,"regfirefunction",regfirefunction);
166 registerFunction(lstate,"createshot",createshot);
167 registerFunction(lstate,"regcollision",regcollision);
168 doLuaFile(lstate,"weaponmanager.lua");
169 doLuaFile(lstate,"weapons.lua");