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 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
);
57 static int regspritelineshot (lua_State
*L
) {
58 Shot
* w
= (Shot
*)lua_touserdata(L
, 1);
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);
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
);
72 static void deleteShotFunc(void* param
) {
73 Shot
* shot
= (Shot
*) param
;
77 static Uint32
deleteShotCallback(Uint32 interval
, void *param
) {
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
);
94 static int createshot (lua_State
*L
) {
95 double x
,y
,angle
,rate
,gravity
,speed
;
98 x
= lua_tonumber(L
, 1);
99 y
= lua_tonumber(L
, 2);
100 angle
= lua_tonumber(L
, 3);
101 rate
= lua_tonumber(L
, 4);
102 weapon
= (Weapon
*)lua_touserdata(L
, 5);
103 gravity
= lua_tonumber(L
, 6);
104 speed
= lua_tonumber(L
, 7);
105 damage
= lua_tonumber(L
, 8);
107 x
+= weapon
->position
.x
;
108 y
+= weapon
->position
.y
;
110 Shot
* newshot
= new Shot(x
,y
,angle
,speed
,gravity
,weapon
,damage
);
111 weapon
->game
->shotManager
->addShot(newshot
);
112 SDL_AddTimer(rate
*_game
->rate
,deleteShotCallback
,newshot
);
113 lua_pushlightuserdata(L
, newshot
);
117 static int regcollision(lua_State
*L
) {
118 const char* nome
= lua_tostring(L
, 1);
119 double x1
, y1
, x2
, y2
;
120 x1
= lua_tonumber(L
, 2);
121 y1
= lua_tonumber(L
, 3);
122 x2
= lua_tonumber(L
, 4);
123 y2
= lua_tonumber(L
, 5);
124 Linha
l(x1
, y1
, x2
, y2
);
125 if (_collision
->find(nome
) == _collision
->end()) {
127 (*_collision
)[nome
] = p
;
129 (*_collision
)[nome
].addLinha(l
);
133 Weapon
* WeaponManager::getWeapon(std::string name
) {
134 std::list
<Weapon
*>::iterator it
;
135 for (it
= weapons
.begin(); it
!= weapons
.end(); it
++) {
136 if ((*it
)->name
== name
)
142 Polygon
WeaponManager::getCollision(std::string name
) {
143 return collision
[name
];
146 WeaponItem
* WeaponManager::getItem(std::string name
) {
147 return new WeaponItem(getWeapon(name
), getCollision(name
));
150 void WeaponManager::loadWeapons() {
153 std::list
<Weapon
*>::iterator it
;
154 for (it
= weapons
.begin(); it
!= weapons
.end(); it
++)
160 _collision
= &collision
;
162 registerFunction(lstate
,"regweapon",regweapon
);
163 registerFunction(lstate
,"regspriteline",regspriteline
);
164 registerFunction(lstate
,"regspritelineshot",regspritelineshot
);
165 registerFunction(lstate
,"regfirefunction",regfirefunction
);
166 registerFunction(lstate
,"createshot",createshot
);
167 registerFunction(lstate
,"regcollision",regcollision
);
168 doLuaFile(lstate
,"weaponmanager.lua");
169 doLuaFile(lstate
,"weapons.lua");