Agora funcionando.
[Projeto-PCG.git] / weaponmanager.cpp
blobe35cd3aec4cfb093a7148926b06e6d51ea763e10
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 Weapon* weapon;
97 x = lua_tonumber(L, 1);
98 y = lua_tonumber(L, 2);
99 angle = lua_tonumber(L, 3);
100 rate = lua_tonumber(L, 4);
101 weapon = (Weapon*)lua_touserdata(L, 5);
102 gravity = lua_tonumber(L, 6);
103 speed = lua_tonumber(L, 7);
105 x += weapon->position.x;
106 y += weapon->position.y;
108 Shot* newshot = new Shot(x,y,angle,speed,gravity,weapon);
109 weapon->game->shotManager->addShot(newshot);
110 SDL_AddTimer(rate*_game->rate,deleteShotCallback,newshot);
111 lua_pushlightuserdata(L, newshot);
112 return 1;
115 static int regcollision(lua_State *L) {
116 const char* nome = lua_tostring(L, 1);
117 double x1, y1, x2, y2;
118 x1 = lua_tonumber(L, 2);
119 y1 = lua_tonumber(L, 3);
120 x2 = lua_tonumber(L, 4);
121 y2 = lua_tonumber(L, 5);
122 Linha l(x1, y1, x2, y2);
123 if (_collision->find(nome) == _collision->end()) {
124 Polygon p;
125 (*_collision)[nome] = p;
127 (*_collision)[nome].addLinha(l);
128 return 0;
131 Weapon* WeaponManager::getWeapon(std::string name) {
132 std::list<Weapon*>::iterator it;
133 for (it = weapons.begin(); it != weapons.end(); it++) {
134 if ((*it)->name == name)
135 return (*it);
137 return NULL;
140 Polygon WeaponManager::getCollision(std::string name) {
141 return collision[name];
144 WeaponItem* WeaponManager::getItem(std::string name) {
145 return new WeaponItem(getWeapon(name), getCollision(name));
148 void WeaponManager::loadWeapons() {
149 if (lstate != NULL)
150 lua_close(lstate);
151 std::list<Weapon*>::iterator it;
152 for (it = weapons.begin(); it != weapons.end(); it++)
153 delete (*it);
154 weapons.clear();
155 _weapons = &weapons;
156 _game = game;
157 collision.clear();
158 _collision = &collision;
159 lstate = newState();
160 registerFunction(lstate,"regweapon",regweapon);
161 registerFunction(lstate,"regspriteline",regspriteline);
162 registerFunction(lstate,"regspritelineshot",regspritelineshot);
163 registerFunction(lstate,"regfirefunction",regfirefunction);
164 registerFunction(lstate,"createshot",createshot);
165 registerFunction(lstate,"regcollision",regcollision);
166 doLuaFile(lstate,"weaponmanager.lua");
167 doLuaFile(lstate,"weapons.lua");