Fixed arrow flight and spawning bugs, fixed critical trail, close #420

This commit is contained in:
Dylan K. Taylor 2017-04-26 18:55:48 +01:00
parent 2f87dfdcb0
commit caf4937222
4 changed files with 69 additions and 46 deletions

View File

@ -2620,8 +2620,9 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade
new DoubleTag("", cos($this->yaw / 180 * M_PI) * cos($this->pitch / 180 * M_PI)) new DoubleTag("", cos($this->yaw / 180 * M_PI) * cos($this->pitch / 180 * M_PI))
]), ]),
"Rotation" => new ListTag("Rotation", [ "Rotation" => new ListTag("Rotation", [
new FloatTag("", $this->yaw), //yaw/pitch for arrows taken crosswise, not along the arrow shaft.
new FloatTag("", $this->pitch) new FloatTag("", ($this->yaw > 180 ? 360 : 0) - $this->yaw), //arrow yaw must range from -180 to +180
new FloatTag("", -$this->pitch)
]), ]),
"Fire" => new ShortTag("Fire", $this->isOnFire() ? 45 * 60 : 0) "Fire" => new ShortTag("Fire", $this->isOnFire() ? 45 * 60 : 0)
]); ]);

View File

@ -44,6 +44,24 @@ class Arrow extends Projectile{
public function __construct(Level $level, CompoundTag $nbt, Entity $shootingEntity = null, $critical = false){ public function __construct(Level $level, CompoundTag $nbt, Entity $shootingEntity = null, $critical = false){
$this->isCritical = (bool) $critical; $this->isCritical = (bool) $critical;
parent::__construct($level, $nbt, $shootingEntity); parent::__construct($level, $nbt, $shootingEntity);
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CRITICAL, $critical);
}
public function isCritical() : bool{
return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CRITICAL);
}
public function setCritical(bool $value = true){
$this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CRITICAL, $value);
}
public function getResultDamage() : int{
$base = parent::getResultDamage();
if($this->isCritical()){
return ($base + mt_rand(0, (int) ($base / 2) + 1));
}else{
return $base;
}
} }
public function onUpdate($currentTick){ public function onUpdate($currentTick){
@ -55,17 +73,12 @@ class Arrow extends Projectile{
$hasUpdate = parent::onUpdate($currentTick); $hasUpdate = parent::onUpdate($currentTick);
if(!$this->hadCollision and $this->isCritical){ if($this->onGround or $this->hadCollision){
$this->level->addParticle(new CriticalParticle($this->add( $this->setCritical(false);
$this->width / 2 + mt_rand(-100, 100) / 500,
$this->height / 2 + mt_rand(-100, 100) / 500,
$this->width / 2 + mt_rand(-100, 100) / 500)));
}elseif($this->onGround){
$this->isCritical = false;
} }
if($this->age > 1200){ if($this->age > 1200){
$this->kill(); $this->close();
$hasUpdate = true; $hasUpdate = true;
} }
@ -84,6 +97,8 @@ class Arrow extends Projectile{
$pk->speedX = $this->motionX; $pk->speedX = $this->motionX;
$pk->speedY = $this->motionY; $pk->speedY = $this->motionY;
$pk->speedZ = $this->motionZ; $pk->speedZ = $this->motionZ;
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$pk->metadata = $this->dataProperties; $pk->metadata = $this->dataProperties;
$player->dataPacket($pk); $player->dataPacket($pk);

View File

@ -1118,7 +1118,7 @@ abstract class Entity extends Location implements Metadatable{
$this->lastYaw = $this->yaw; $this->lastYaw = $this->yaw;
$this->lastPitch = $this->pitch; $this->lastPitch = $this->pitch;
$this->level->addEntityMovement($this->chunk->getX(), $this->chunk->getZ(), $this->id, $this->x, $this->y + $this->getEyeHeight(), $this->z, $this->yaw, $this->pitch, $this->yaw); $this->level->addEntityMovement($this->chunk->getX(), $this->chunk->getZ(), $this->id, $this->x, $this->y, $this->z, $this->yaw, $this->pitch, $this->yaw);
} }
if($diffMotion > 0.0025 or ($diffMotion > 0.0001 and $this->getMotion()->lengthSquared() <= 0.0001)){ //0.05 ** 2 if($diffMotion > 0.0025 or ($diffMotion > 0.0001 and $this->getMotion()->lengthSquared() <= 0.0001)){ //0.05 ** 2

View File

@ -64,13 +64,46 @@ abstract class Projectile extends Entity{
if(isset($this->namedtag->Age)){ if(isset($this->namedtag->Age)){
$this->age = $this->namedtag["Age"]; $this->age = $this->namedtag["Age"];
} }
} }
public function canCollideWith(Entity $entity){ public function canCollideWith(Entity $entity){
return $entity instanceof Living and !$this->onGround; return $entity instanceof Living and !$this->onGround;
} }
/**
* Returns the amount of damage this projectile will deal to the entity it hits.
* @return int
*/
public function getResultDamage() : int{
return ceil(sqrt($this->motionX ** 2 + $this->motionY ** 2 + $this->motionZ ** 2) * $this->damage);
}
public function onCollideWithEntity(Entity $entity){
$this->server->getPluginManager()->callEvent(new ProjectileHitEvent($this));
$damage = $this->getResultDamage();
if($this->shootingEntity === null){
$ev = new EntityDamageByEntityEvent($this, $entity, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
}else{
$ev = new EntityDamageByChildEntityEvent($this->shootingEntity, $this, $entity, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
}
$entity->attack($ev->getFinalDamage(), $ev);
$this->hadCollision = true;
if($this->fireTicks > 0){
$ev = new EntityCombustByEntityEvent($this, $entity, 5);
$this->server->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$entity->setOnFire($ev->getDuration());
}
}
$this->close();
}
public function saveNBT(){ public function saveNBT(){
parent::saveNBT(); parent::saveNBT();
$this->namedtag->Age = new ShortTag("Age", $this->age); $this->namedtag->Age = new ShortTag("Age", $this->age);
@ -133,42 +166,14 @@ abstract class Projectile extends Entity{
if($movingObjectPosition !== null){ if($movingObjectPosition !== null){
if($movingObjectPosition->entityHit !== null){ if($movingObjectPosition->entityHit !== null){
$this->onCollideWithEntity($movingObjectPosition->entityHit);
$this->server->getPluginManager()->callEvent(new ProjectileHitEvent($this)); return false;
$motion = sqrt($this->motionX ** 2 + $this->motionY ** 2 + $this->motionZ ** 2);
$damage = ceil($motion * $this->damage);
if($this instanceof Arrow and $this->isCritical){
$damage += mt_rand(0, (int) ($damage / 2) + 1);
}
if($this->shootingEntity === null){
$ev = new EntityDamageByEntityEvent($this, $movingObjectPosition->entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
}else{
$ev = new EntityDamageByChildEntityEvent($this->shootingEntity, $this, $movingObjectPosition->entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
}
$movingObjectPosition->entityHit->attack($ev->getFinalDamage(), $ev);
$this->hadCollision = true;
if($this->fireTicks > 0){
$ev = new EntityCombustByEntityEvent($this, $movingObjectPosition->entityHit, 5);
$this->server->getPluginManager()->callEvent($ev);
if(!$ev->isCancelled()){
$movingObjectPosition->entityHit->setOnFire($ev->getDuration());
}
}
$this->kill();
return true;
} }
} }
$this->move($this->motionX, $this->motionY, $this->motionZ); $this->move($this->motionX, $this->motionY, $this->motionZ);
if($this->isCollided and !$this->hadCollision){ if($this->isCollided and !$this->hadCollision){ //Collided with a block
$this->hadCollision = true; $this->hadCollision = true;
$this->motionX = 0; $this->motionX = 0;
@ -176,11 +181,14 @@ abstract class Projectile extends Entity{
$this->motionZ = 0; $this->motionZ = 0;
$this->server->getPluginManager()->callEvent(new ProjectileHitEvent($this)); $this->server->getPluginManager()->callEvent(new ProjectileHitEvent($this));
}elseif(!$this->isCollided and $this->hadCollision){ return false;
}elseif(!$this->isCollided and $this->hadCollision){ //Collided with block, but block later removed
//This currently doesn't work because the arrow's motion is all zeros when it's hit a block, so move() doesn't do any collision checks.
//TODO: fix this
$this->hadCollision = false; $this->hadCollision = false;
} }
if(!$this->onGround or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001){ if(!$this->hadCollision or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001){
$f = sqrt(($this->motionX ** 2) + ($this->motionZ ** 2)); $f = sqrt(($this->motionX ** 2) + ($this->motionZ ** 2));
$this->yaw = (atan2($this->motionX, $this->motionZ) * 180 / M_PI); $this->yaw = (atan2($this->motionX, $this->motionZ) * 180 / M_PI);
$this->pitch = (atan2($this->motionY, $f) * 180 / M_PI); $this->pitch = (atan2($this->motionY, $f) * 180 / M_PI);
@ -188,7 +196,6 @@ abstract class Projectile extends Entity{
} }
$this->updateMovement(); $this->updateMovement();
} }
return $hasUpdate; return $hasUpdate;