now uses ammo/stock value that server sends

This commit is contained in:
yvt 2013-08-31 02:50:51 +09:00
parent 3844e9c753
commit e9f4660579
6 changed files with 61 additions and 12 deletions

View File

@ -483,6 +483,11 @@ namespace spades {
winp.secondary = false;
}
if(player->GetTool() == Player::ToolWeapon &&
player->IsAwaitingReloadCompletion()) {
winp.primary = false;
}
player->SetInput(inp);
player->SetWeaponInput(winp);
@ -922,11 +927,15 @@ namespace spades {
weapInput.secondary = down;
}
}else if(CheckKey(cg_keyReloadWeapon, name) && down){
world->GetLocalPlayer()->Reload();
if(world->GetLocalPlayer()->IsToolWeapon()){
weapInput.secondary = false;
Weapon *w = world->GetLocalPlayer()->GetWeapon();
if(w->GetAmmo() < w->GetClipSize() &&
w->GetStock() > 0){
world->GetLocalPlayer()->Reload();
if(world->GetLocalPlayer()->IsToolWeapon()){
weapInput.secondary = false;
}
net->SendReload();
}
net->SendReload();
}else if(CheckKey(cg_keyToolSpade, name) && down){
if(world->GetLocalPlayer()->GetTeamId() < 2 &&
world->GetLocalPlayer()->IsAlive() &&
@ -2837,7 +2846,8 @@ namespace spades {
case Player::ToolWeapon:
{
Weapon *weap = p->GetWeapon();
if(weap->IsReloading()){
if(weap->IsReloading() ||
p->IsAwaitingReloadCompletion()){
msg = "Reloading";
}else if(weap->GetAmmo() == 0 &&
weap->GetStock() == 0){

View File

@ -1359,7 +1359,13 @@ namespace spades {
Player *p = GetPlayer(reader.ReadByte());
if(p != GetLocalPlayerOrNull())
p->Reload();
else{
int clip = reader.ReadByte();
int reserve = reader.ReadByte();
if(clip < 255 && reserve < 255) {
p->ReloadDone(clip, reserve);
}
}
// FIXME: use of "clip ammo" and "reserve ammo"?
}
break;
@ -1519,8 +1525,11 @@ namespace spades {
NetPacketWriter wri(PacketTypeWeaponReload);
wri.Write((uint8_t)GetLocalPlayer()->GetId());
wri.Write((uint8_t)0); // clip_ammo; not used?
wri.Write((uint8_t)0); // reserve_ammo; not used?
// these value should be 255, or
// NetClient will think reload was done when
// it receives echoed WeaponReload packet
wri.Write((uint8_t)255); // clip_ammo; not used?
wri.Write((uint8_t)255); // reserve_ammo; not used?
enet_peer_send(peer, 0, wri.CreatePacket());
}

View File

@ -75,6 +75,7 @@ namespace spades {
blockCursorDragging = false;
holdingGrenade = false;
reloadingServerSide = false;
}
@ -213,6 +214,13 @@ namespace spades {
return;
}
weapon->Reload();
if(this == world->GetLocalPlayer())
reloadingServerSide = true;
}
void Player::ReloadDone(int clip, int stock) {
reloadingServerSide = false;
weapon->ReloadDone(clip, stock);
}
void Player::Restock() {

View File

@ -119,6 +119,10 @@ namespace spades {
IntVector3 blockCursorPos;
IntVector3 blockCursorDragPos;
// for local players, completion of reload is
// notified to client
bool reloadingServerSide;
float respawnTime;
void RepositionPlayer(const Vector3&);
@ -162,6 +166,7 @@ namespace spades {
int GetNumBlocks() { return blockStocks;}
int GetNumGrenades() { return grenades; }
void Reload();
void ReloadDone(int clip, int stock);
void Restock();
void GotBlock();
@ -169,6 +174,9 @@ namespace spades {
return tool == ToolWeapon;
}
bool IsToolSelectable(ToolType);
bool IsAwaitingReloadCompletion() {
return reloadingServerSide;
}
void SetPosition(const Vector3&);
void SetOrientation(const Vector3&);

View File

@ -102,14 +102,20 @@ namespace spades {
// reload done
reloading = false;
if(IsReloadSlow()){
// TODO: dealing with ammo/stock value
// server sends for local player
ammo++;
stock--;
Reload();
}else{
int newStock;
newStock = std::max(0, stock - GetClipSize() + ammo);
ammo += stock - newStock;
stock = newStock;
// for local player, server sends
// new ammo/stock value
if(owner != owner->GetWorld()->GetLocalPlayer()){
int newStock;
newStock = std::max(0, stock - GetClipSize() + ammo);
ammo += stock - newStock;
stock = newStock;
}
}
@ -127,6 +133,11 @@ namespace spades {
return fired;
}
void Weapon::ReloadDone(int ammo, int stock) {
this->ammo = ammo;
this->stock = stock;
}
void Weapon::Reload() {
SPADES_MARK_FUNCTION();

View File

@ -80,6 +80,9 @@ namespace spades {
int GetAmmo() { return ammo; }
int GetStock() { return stock;}
// for local player
void ReloadDone(int ammo, int stock);
float GetReloadProgress();
float TimeToNextFire();