28.01.2012, 10:45
You shouldnt create your textdraw in OnPlayerUpdate since it gets called very frequently
The whole code optimised
The whole code optimised
pawn Код:
new
Text: gtpVehicleHealth[MAX_PLAYERS] = {Text: INVALID_TEXT_DRAW, ...};
pawn Код:
public OnPlayerStateChange(playerid, newstate, oldstate) {
switch((newstate ^ oldstate) & newstate) {
case PLAYER_STATE_DRIVER: {
new Text: text = gtpVehicleHealth[playerid] =
TextDrawCreate(5, 433, "_");
TextDrawSetShadow(text, 0);
TextDrawUseBox(text, 1);
TextDrawBoxColor(text, 0x202020FF);
TextDrawTextSize(text, 48, 0);
TextDrawShowForPlayer(playerid, text);
}
}
switch((oldstate ^ newstate) & oldstate) {
case PLAYER_STATE_DRIVER: {
TextDrawDestroy(gtpVehicleHealth[playerid]);
gtpVehicleHealth[playerid] = Text: INVALID_TEXT_DRAW;
}
}
return true;
}
pawn Код:
public OnPlayerUpdate(playerid) {
if(gtpVehicleHealth[playerid] != Text: INVALID_TEXT_DRAW) {
static
string[16],
Float: vHealth;
static const
hColors[] = {
0x00FF00FF,
0x40FF00FF,
0x80FF00FF,
0xC0FF00FF,
0xFFFF00FF,
0xFFC000FF,
0xFF8000FF,
0xFF4000FF,
0xFF0000FF
};
GetVehicleHealth(GetPlayerVehicleID(playerid), vHealth);
format(string, sizeof string, "%.0f%%", vHealth);
TextDrawSetString(gtpVehicleHealth[playerid], string);
TextDrawColor(gtpVehicleHealth[playerid],
hColors[sizeof hColors - max(1, floatround(((vHealth - 250.0) / (750.0 / sizeof hColors)), floatround_round))]
);
}
return true;
}