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
;
97 x
= lua_tonumber(L
, 1);
98 y
= lua_tonumber(L
, 2);
99 angle
= lua_tonumber(L
, 3);
100 rate
= lua_tonumber(L
, 4);
101 weapon
= (Weapon
*)lua_touserdata(L
, 5);
102 gravity
= lua_tonumber(L
, 6);
103 speed
= lua_tonumber(L
, 7);
105 x
+= weapon
->position
.x
;
106 y
+= weapon
->position
.y
;
108 Shot
* newshot
= new Shot(x
,y
,angle
,speed
,gravity
,weapon
);
109 weapon
->game
->shotManager
->addShot(newshot
);
110 SDL_AddTimer(rate
*_game
->rate
,deleteShotCallback
,newshot
);
111 lua_pushlightuserdata(L
, newshot
);
115 static int regcollision(lua_State
*L
) {
116 const char* nome
= lua_tostring(L
, 1);
117 double x1
, y1
, x2
, y2
;
118 x1
= lua_tonumber(L
, 2);
119 y1
= lua_tonumber(L
, 3);
120 x2
= lua_tonumber(L
, 4);
121 y2
= lua_tonumber(L
, 5);
122 Linha
l(x1
, y1
, x2
, y2
);
123 if (_collision
->find(nome
) == _collision
->end()) {
125 (*_collision
)[nome
] = p
;
127 (*_collision
)[nome
].addLinha(l
);
131 Weapon
* WeaponManager::getWeapon(std::string name
) {
132 std::list
<Weapon
*>::iterator it
;
133 for (it
= weapons
.begin(); it
!= weapons
.end(); it
++) {
134 if ((*it
)->name
== name
)
140 Polygon
WeaponManager::getCollision(std::string name
) {
141 return collision
[name
];
144 WeaponItem
* WeaponManager::getItem(std::string name
) {
145 return new WeaponItem(getWeapon(name
), getCollision(name
));
148 void WeaponManager::loadWeapons() {
151 std::list
<Weapon
*>::iterator it
;
152 for (it
= weapons
.begin(); it
!= weapons
.end(); it
++)
158 _collision
= &collision
;
160 registerFunction(lstate
,"regweapon",regweapon
);
161 registerFunction(lstate
,"regspriteline",regspriteline
);
162 registerFunction(lstate
,"regspritelineshot",regspritelineshot
);
163 registerFunction(lstate
,"regfirefunction",regfirefunction
);
164 registerFunction(lstate
,"createshot",createshot
);
165 registerFunction(lstate
,"regcollision",regcollision
);
166 doLuaFile(lstate
,"weaponmanager.lua");
167 doLuaFile(lstate
,"weapons.lua");