Classe WeaponItem (parcial)
[Projeto-PCG.git] / weaponmanager.cpp
blob6eb175fcd1b44c01cb98252cb07a36ebc211356c
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 w->sprite.addLinha(linha);
51 return 0;
54 static int regspritelineshot (lua_State *L) {
55 Shot* w = (Shot*)lua_touserdata(L, 1);
56 Ponto a,b;
57 a.x = lua_tonumber(L, 2);
58 a.y = lua_tonumber(L, 3);
59 b.x = lua_tonumber(L, 4);
60 b.y = lua_tonumber(L, 5);
61 Linha linha(a,b);
62 w->sprite.addLinha(linha);
63 return 0;
66 static void deleteShotFunc(void* param) {
67 Shot* shot = (Shot*) param;
68 delete shot;
71 static Uint32 deleteShotCallback(Uint32 interval, void *param) {
72 SDL_Event event;
73 SDL_UserEvent userevent;
74 userevent.type = SDL_USEREVENT;
75 userevent.code = FUNCTIONCALL;
76 userevent.data1 = (void*)deleteShotFunc;
77 userevent.data2 = (void*)param;
79 event.type = SDL_USEREVENT;
80 event.user = userevent;
82 SDL_PushEvent(&event);
84 return 0;
88 static int createshot (lua_State *L) {
89 double x,y,angle,rate,gravity,speed;
90 Weapon* weapon;
91 x = lua_tonumber(L, 1);
92 y = lua_tonumber(L, 2);
93 angle = lua_tonumber(L, 3);
94 rate = lua_tonumber(L, 4);
95 weapon = (Weapon*)lua_touserdata(L, 5);
96 gravity = lua_tonumber(L, 6);
97 speed = lua_tonumber(L, 7);
99 Shot* newshot = new Shot(x,y,angle,speed,gravity,weapon);
100 weapon->game->shotManager->addShot(newshot);
101 SDL_AddTimer(rate*_game->rate,deleteShotCallback,newshot);
102 lua_pushlightuserdata(L, newshot);
103 return 1;
106 static int regcollision(lua_State *L) {
107 const char* nome = lua_tostring(L, 1);
108 double x1, y1, x2, y2;
109 x1 = lua_tonumber(L, 2);
110 y1 = lua_tonumber(L, 3);
111 x2 = lua_tonumber(L, 4);
112 y2 = lua_tonumber(L, 5);
113 Linha l(x1, y1, x2, y2);
114 if (_collision->find(nome) == _collision->end()) {
115 Polygon p;
116 (*_collision)[nome] = p;
118 (*_collision)[nome].addLinha(l);
119 return 0;
122 Weapon* WeaponManager::getWeapon(std::string name) {
123 std::list<Weapon*>::iterator it;
124 for (it = weapons.begin(); it != weapons.end(); it++) {
125 if ((*it)->name == name)
126 return (*it);
128 return NULL;
131 Polygon WeaponManager::getCollision(std::string name) {
132 return collision[name];
135 WeaponItem* WeaponManager::getItem(std::string name) {
136 return new WeaponItem(getWeapon(name), getCollision(name));
139 void WeaponManager::loadWeapons() {
140 if (lstate != NULL)
141 lua_close(lstate);
142 std::list<Weapon*>::iterator it;
143 for (it = weapons.begin(); it != weapons.end(); it++)
144 delete (*it);
145 weapons.clear();
146 _weapons = &weapons;
147 _game = game;
148 collision.clear();
149 _collision = &collision;
150 lstate = newState();
151 registerFunction(lstate,"regweapon",regweapon);
152 registerFunction(lstate,"regspriteline",regspriteline);
153 registerFunction(lstate,"regspritelineshot",regspritelineshot);
154 registerFunction(lstate,"regfirefunction",regfirefunction);
155 registerFunction(lstate,"createshot",createshot);
156 registerFunction(lstate,"regcollision",regcollision);
157 doLuaFile(lstate,"weaponmanager.lua");
158 doLuaFile(lstate,"weapons.lua");