27.03.2014, 17:33
(
Последний раз редактировалось zPain; 27.03.2014 в 19:11.
)
Introduзгo:
Este pequeno sistema foi desenvolvido a fim de servir como um exemplo a mais de aprofundamento no estudo de bit flags, que foi assunto abordado recentemente em um tutorial de Rockfire.
O sistema resume-se basicamente em ativar/desativar os parвmetros dos veнculos (engine, lights, alarm, doors, bonnet, boot, objective). Eu fiz apenas para engine, lights, bonnet e boot para que, caso alguйm utilize, este possa praticar fazendo com os outros parвmetros.
Cуdigo:
Lembrando:
Download: http://pastebin.com/HXVaMzLE
Crйditos no cуdigo.
Este pequeno sistema foi desenvolvido a fim de servir como um exemplo a mais de aprofundamento no estudo de bit flags, que foi assunto abordado recentemente em um tutorial de Rockfire.
O sistema resume-se basicamente em ativar/desativar os parвmetros dos veнculos (engine, lights, alarm, doors, bonnet, boot, objective). Eu fiz apenas para engine, lights, bonnet e boot para que, caso alguйm utilize, este possa praticar fazendo com os outros parвmetros.
Cуdigo:
pawn Код:
/*
Engine system
Este sistema foi desenvolvido nгo sу
para uso dos interessados, como tambйm
a fim de tornar ainda mais claro o uso de
Bit Flags.
Flags:
- Motor
- Farуis
- Capф
- Porta-malas
Crйditos:
- zPain
- Slice
- Rockfire
*/
#include <a_samp>
enum E_VEHICLE_FLAGS
{
FLAG_VEHICLE_BOOT = 0b1,
FLAG_VEHICLE_BONNET = 0b10,
FLAG_VEHICLE_LIGHTS = 0b100,
FLAG_VEHICLE_ENGINE = 0b1000
}
new E_VEHICLE_FLAGS:g_vFlags[MAX_VEHICLES];
// Public functions
public OnFilterScriptInit() {
ManualVehicleEngineAndLights();
return true;
}
public OnVehicleSpawn(vehicleid) {
// Unset em todas as flags
g_vFlags[vehicleid] = E_VEHICLE_FLAGS:0;
return true;
}
public OnPlayerStateChange(playerid, newstate, oldstate) {
if(newstate == PLAYER_STATE_DRIVER) {
SendClientMessage(playerid, 0xffa500ff, "Informaзхes do veнculo:");
SendClientMessage(playerid, 0xffa500ff, " 1. Para ligar/desligar o motor, tecle LALT.");
SendClientMessage(playerid, 0xffa500ff, " 2. Para acender/apagar os farуis, tecle LCTRL.");
SendClientMessage(playerid, 0xffa500ff, " 3. Para abrir/fechar o capф, tecle Y.");
SendClientMessage(playerid, 0xffa500ff, " 4. Para abrir/fechar o porta-malas, tecle N.");
}
return true;
}
public OnPlayerCommandText(playerid, cmdtext[]) {
if(cmdtext[1] == 'v') {
if(IsPlayerAdmin(playerid)) {
if(cmdtext[3]) {
static Float:px, Float:py, Float:pz, Float:pa;
GetPlayerPos(playerid, px, py, pz);
GetPlayerFacingAngle(playerid, pa);
CreateVehicle(strval(cmdtext[3]), px, py, pz, pa, random(255), random(255), -1);
return true;
}
}
}
return false;
}
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys) {
if(GetPlayerState(playerid) == PLAYER_STATE_DRIVER) {
static
engine,
lights,
alarm,
doors,
bonnet,
boot,
objective,
vehicleid;
vehicleid = GetPlayerVehicleID(playerid);
GetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, boot, objective);
if(newkeys & KEY_ACTION) {
// Ligar/desligar motor
// Ou seja, se a flag estiver ativa, desativa-a, e vice-versa.
g_vFlags[vehicleid] ^= FLAG_VEHICLE_ENGINE;
// Uso de !!
// Converte INTEGER em BOOLEAN
SetVehicleParamsEx(vehicleid, !!(g_vFlags[vehicleid] & FLAG_VEHICLE_ENGINE), lights, alarm, doors, bonnet, boot, objective);
// Verificaзгo
if(g_vFlags[vehicleid] & FLAG_VEHICLE_ENGINE)
SendClientMessage(playerid, 0x32cd32ff, "** Motor LIGADO.");
else
SendClientMessage(playerid, 0xff0000ff, "** Motor DESLIGADO.");
}
else if(newkeys & KEY_FIRE) {
g_vFlags[vehicleid] ^= FLAG_VEHICLE_LIGHTS;
SetVehicleParamsEx(vehicleid, engine, !!(g_vFlags[vehicleid] & FLAG_VEHICLE_LIGHTS), alarm, doors, bonnet, boot, objective);
if(g_vFlags[vehicleid] & FLAG_VEHICLE_LIGHTS) {
SendClientMessage(playerid, 0x32cd32ff, "** Farуis ACESOS.");
}
else {
SendClientMessage(playerid, 0xff0000ff, "** Farуis APAGADOS.");
}
}
else if(newkeys & KEY_YES) {
g_vFlags[vehicleid] ^= FLAG_VEHICLE_BONNET;
SetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, !!(g_vFlags[vehicleid] & FLAG_VEHICLE_BONNET), boot, objective);
if(g_vFlags[vehicleid] & FLAG_VEHICLE_BONNET) {
SendClientMessage(playerid, 0x32cd32ff, "** Capф ABERTO.");
}
else {
SendClientMessage(playerid, 0xff0000ff, "** Capф FECHADO.");
}
}
else if(newkeys & KEY_NO) {
g_vFlags[vehicleid] ^= FLAG_VEHICLE_BOOT;
SetVehicleParamsEx(vehicleid, engine, lights, alarm, doors, bonnet, !!(g_vFlags[vehicleid] & FLAG_VEHICLE_BOOT), objective);
if(g_vFlags[vehicleid] & FLAG_VEHICLE_BOOT) {
SendClientMessage(playerid, 0x32cd32ff, "** Porta-malas ABERTO.");
}
else {
SendClientMessage(playerid, 0xff0000ff, "** Porta-malas FECHADO.");
}
}
}
return true;
}
Lembrando:
pawn Код:
var |= FLAG // ativa a flag
var &= ~FLAG // desativa a flag
var ^= FLAG // toggle (ou seja, se a flag estiver ativa, desativa-a, e vice-versa)
Download: http://pastebin.com/HXVaMzLE
Crйditos no cуdigo.