|
[ANET] Spells
#338796
08/21/10 17:47
08/21/10 17:47
|
Joined: Jun 2008
Posts: 428
Rasch
OP
Senior Member
|
OP
Senior Member
Joined: Jun 2008
Posts: 428
|
Hi, im really thinking about how to create spells. I can code evrything else like movement, dead reckoning and so on. But i really could need some help how to create a spell. Lets say player push key 1. Then he´s choosing his enemy. After choosing he casts a fireball (particle animation) this fireball flys to the enemy and kills him. How can i create such an effect so that every player can see this one? With enet_ent_create? Creating a fireball model and adding particle animation to it? Also with those if(MY_CREATOR == MY_CLIENTID)... lines? Tahnks
|
|
|
Re: [ANET] Spells
[Re: Razoron]
#338824
08/21/10 20:45
08/21/10 20:45
|
Joined: Nov 2002
Posts: 913 Berlin, Germany
SchokoKeks
User
|
User
Joined: Nov 2002
Posts: 913
Berlin, Germany
|
@Razoron: Since ANet Version 1.2.3.4 you can send structs with events, thanks to the "length/size" parameter that is passed. Take a look at the code I'm using for these spells to see how: The player wants to cast a spell and sends it to the server:
// struct that contains spell info
typedef struct {
unsigned short id; // id of spell
var ent_targ; // globpointer of target
} s_sendspell;
s_sendspell d_send_spell; // global variable of the above struct that is automatically initialized, not a pointer.
void cl_send_spell(int spell_id, int spell_level, ENTITY* ent_targ)
{
//TODO: check if the player is allowed to
d_send_spell.id = spell_id;
d_send_spell.ent_targ = enet_ent_globpointer(ent_targ);
enet_clsend_event(EVENT_SPELL, &d_send_spell, sizeof(s_sendspell), SERVER);
}
The server receives the spell and only sends the ID to the clients:
void sv_e_spell(var sender, STRING* data, var size)
{
if (size != sizeof(s_sendspell)) {svdiag("\nERROR in sv_e_spell: received invalid data length"); return;}
s_sendspell* sp_data = (s_sendspell*)data->chars; // get a new pointer with correct type, because the data is passed as a STRING*
// own code for handling spells, replace it with yours
// I included it to show cheating tests and how to send ok back to player
ENTITY* ent_caster = enet_ent_locpointer(pl_globpointers[sender]);
ENTITY* ent_targ = enet_ent_locpointer(sp_data->ent_targ);
int sp_level = spellbook_get_level((s_spell_book*)ent_caster->spellbook, sp_data->id);
if (sp_level == 0) {svdiag("\nCHEATING ATTEMPT in sv_e_spell: player doesn't have send spell"); return;}
// do spell action
if (sv_spell_act(ent_caster, sp_data->id, sp_level, ent_targ))
{
// send ok back to player, just the id, not the whole struct
// will start cooldown timer at the player
enet_svsend_event(EVENT_SPELL, sp_data->id, sizeof(unsigned short), sender);
// TODO: send effect to all players nearby.
}
}
I've used the type "unsigned short" to save 2 bytes of transferred data. Thats not really necessary for smaller games, but I want to save traffic wherever possible. I'm also not sending the caster, it is implied that the player entity of the sender is always the caster.
|
|
|
|