1 #include "weaponmanager.h"
5 #include "shotmanager.h"
7 #include "usereventtype.h"
9 WeaponManager::WeaponManager(Game
* g
) {
14 std::list
<Weapon
*> *_weapons
= NULL
;
17 static int regweapon (lua_State
*L
) {
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
);
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);
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);
48 w
->sprite
.addLinha(linha
);
52 static int regspritelineshot (lua_State
*L
) {
53 Shot
* w
= (Shot
*)lua_touserdata(L
, 1);
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);
60 w
->sprite
.addLinha(linha
);
64 static void deleteShotFunc(void* param
) {
65 Shot
* shot
= (Shot
*) param
;
69 static Uint32
deleteShotCallback(Uint32 interval
, void *param
) {
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
);
86 static int createshot (lua_State
*L
) {
87 double x
,y
,angle
,rate
,gravity
,speed
;
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
);
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
)
113 void WeaponManager::loadWeapons() {
116 std::list
<Weapon
*>::iterator it
;
117 for (it
= weapons
.begin(); it
!= weapons
.end(); it
++)
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");