Flamethrower decente ateh
[Projeto-PCG.git] / weaponmanager.cpp
blob6ef7059499fdd45131804383af7a9bd4b994a31d
1 #include "weaponmanager.h"
2 #include "luaenv.h"
3 #include "weapon.h"
4 #include "game.h"
5 #include "shotmanager.h"
6 #include "shot.h"
7 #include "usereventtype.h"
9 WeaponManager::WeaponManager(Game* g) {
10 lstate = NULL;
11 game = g;
14 std::list<Weapon*> *_weapons = NULL;
15 Game *_game = NULL;
17 static int regweapon (lua_State *L) {
18 Ponto r,l,tip;
19 r.x = lua_tonumber(L, 1);
20 r.y = lua_tonumber(L, 2);
21 l.x = lua_tonumber(L, 3);
22 l.y = lua_tonumber(L, 4);
23 Weapon* newweapon = new Weapon(_game,L);
24 newweapon->name = lua_tostring (L, 5);
25 newweapon->fireRate = lua_tonumber(L, 6);
26 tip.x = lua_tonumber(L, 7);
27 tip.y = lua_tonumber(L, 8);
28 newweapon->setRightHand(r);
29 newweapon->setLeftHand(l);
30 newweapon->setTip(tip);
31 lua_pushlightuserdata(L, newweapon);
32 _weapons->push_front(newweapon);
33 return 1;
36 static int regfirefunction (lua_State *L) {
37 lua_settable(L, LUA_REGISTRYINDEX);
40 static int regspriteline (lua_State *L) {
41 Weapon* w = (Weapon*)lua_touserdata(L, 1);
42 Ponto a,b;
43 a.x = lua_tonumber(L, 2);
44 a.y = lua_tonumber(L, 3);
45 b.x = lua_tonumber(L, 4);
46 b.y = lua_tonumber(L, 5);
47 Linha linha(a,b);
48 w->sprite.addLinha(linha);
49 return 0;
52 static int regspritelineshot (lua_State *L) {
53 Shot* w = (Shot*)lua_touserdata(L, 1);
54 Ponto a,b;
55 a.x = lua_tonumber(L, 2);
56 a.y = lua_tonumber(L, 3);
57 b.x = lua_tonumber(L, 4);
58 b.y = lua_tonumber(L, 5);
59 Linha linha(a,b);
60 w->sprite.addLinha(linha);
61 return 0;
64 static void deleteShotFunc(void* param) {
65 Shot* shot = (Shot*) param;
66 delete shot;
69 static Uint32 deleteShotCallback(Uint32 interval, void *param) {
70 SDL_Event event;
71 SDL_UserEvent userevent;
72 userevent.type = SDL_USEREVENT;
73 userevent.code = FUNCTIONCALL;
74 userevent.data1 = (void*)deleteShotFunc;
75 userevent.data2 = (void*)param;
77 event.type = SDL_USEREVENT;
78 event.user = userevent;
80 SDL_PushEvent(&event);
82 return 0;
86 static int createshot (lua_State *L) {
87 double x,y,angle,rate,gravity,speed;
88 Weapon* weapon;
89 x = lua_tonumber(L, 1);
90 y = lua_tonumber(L, 2);
91 angle = lua_tonumber(L, 3);
92 rate = lua_tonumber(L, 4);
93 weapon = (Weapon*)lua_touserdata(L, 5);
94 gravity = lua_tonumber(L, 6);
95 speed = lua_tonumber(L, 7);
97 Shot* newshot = new Shot(x,y,angle,speed,gravity,weapon);
98 weapon->game->shotManager->addShot(newshot);
99 SDL_AddTimer(rate,deleteShotCallback,newshot);
100 lua_pushlightuserdata(L, newshot);
101 return 1;
104 Weapon* WeaponManager::getWeapon(std::string name) {
105 std::list<Weapon*>::iterator it;
106 for (it = weapons.begin(); it != weapons.end(); it++) {
107 if ((*it)->name == name)
108 return (*it);
110 return NULL;
113 void WeaponManager::loadWeapons() {
114 if (lstate != NULL)
115 lua_close(lstate);
116 std::list<Weapon*>::iterator it;
117 for (it = weapons.begin(); it != weapons.end(); it++)
118 delete (*it);
119 weapons.clear();
120 _weapons = &weapons;
121 _game = game;
122 lstate = newState();
123 registerFunction(lstate,"regweapon",regweapon);
124 registerFunction(lstate,"regspriteline",regspriteline);
125 registerFunction(lstate,"regspritelineshot",regspritelineshot);
126 registerFunction(lstate,"regfirefunction",regfirefunction);
127 registerFunction(lstate,"createshot",createshot);
128 doLuaFile(lstate,"weaponmanager.lua");
129 doLuaFile(lstate,"weapons.lua");