1 #include "weaponmanager.h"
4 #include "weaponitem.h"
6 #include "shotmanager.h"
8 #include "usereventtype.h"
10 WeaponManager::WeaponManager(Game
* g
) {
15 std::list
<Weapon
*> *_weapons
= NULL
;
17 std::map
<std::string
, Polygon
> *_collision
= NULL
;
19 static int regweapon (lua_State
*L
) {
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
);
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);
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);
50 w
->sprite
.addLinha(linha
);
54 static int regspritelineshot (lua_State
*L
) {
55 Shot
* w
= (Shot
*)lua_touserdata(L
, 1);
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);
62 w
->sprite
.addLinha(linha
);
66 static void deleteShotFunc(void* param
) {
67 Shot
* shot
= (Shot
*) param
;
71 static Uint32
deleteShotCallback(Uint32 interval
, void *param
) {
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
);
88 static int createshot (lua_State
*L
) {
89 double x
,y
,angle
,rate
,gravity
,speed
;
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
);
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()) {
116 (*_collision
)[nome
] = p
;
118 (*_collision
)[nome
].addLinha(l
);
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
)
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() {
142 std::list
<Weapon
*>::iterator it
;
143 for (it
= weapons
.begin(); it
!= weapons
.end(); it
++)
149 _collision
= &collision
;
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");