Вверх ↑
Этот топик читают: Гость
Ответов: 4557
#1: 2014-11-30 19:16:42 ЛС | профиль | цитата
  Сегодня ни один проект не может обойтись без сущностей - это боты, мобы, спрайты. Возможно бывают и другие виды.
В Minecraft есть различные сущность: мобы, двери/люки/кровать/кристалл Дракона Края, частицы и т.д. Сегодня заострим внимание на мобах.

Для начала работы создадим файл EntityPig.java. Из названия уже понято, что работать будем со свиньей.

EntityPig.java


package net.minecraft.src;



public class EntityPig extends EntityAnimal

{

    /** AI task for player control. */

    private final EntityAIControlledByPlayer aiControlledByPlayer;



    public EntityPig(World par1World)

    {

        super(par1World);

        this.texture = "/mob/pig.png";

        this.setSize(0.9F, 0.9F);

        this.getNavigator().setAvoidsWater(true);

        float var2 = 0.25F;

        this.tasks.addTask(0, new EntityAISwimming(this));

        this.tasks.addTask(1, new EntityAIPanic(this, 0.38F));

        this.tasks.addTask(2, this.aiControlledByPlayer = new EntityAIControlledByPlayer(this, 0.34F));

        this.tasks.addTask(3, new EntityAIMate(this, var2));

        this.tasks.addTask(4, new EntityAITempt(this, 0.3F, Item.carrotOnAStick.itemID, false));

        this.tasks.addTask(4, new EntityAITempt(this, 0.3F, Item.carrot.itemID, false));

        this.tasks.addTask(5, new EntityAIFollowParent(this, 0.28F));

        this.tasks.addTask(6, new EntityAIWander(this, var2));

        this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));

        this.tasks.addTask(8, new EntityAILookIdle(this));

    }



    /**

     * Returns true if the newer Entity AI code should be run

     */

    public boolean isAIEnabled()

    {

        return true;

    }



    public int getMaxHealth()

    {

        return 10;

    }



    protected void updateAITasks()

    {

        super.updateAITasks();

    }



    /**

     * returns true if all the conditions for steering the entity are met. For pigs, this is true if it is being ridden

     * by a player and the player is holding a carrot-on-a-stick

     */

    public boolean canBeSteered()

    {

        ItemStack var1 = ((EntityPlayer)this.riddenByEntity).getHeldItem();

        return var1 != null && var1.itemID == Item.carrotOnAStick.itemID;

    }



    protected void entityInit()

    {

        super.entityInit();

        this.dataWatcher.addObject(16, Byte.valueOf((byte)0));

    }



    /**

     * (abstract) Protected helper method to write subclass entity data to NBT.

     */

    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)

    {

        super.writeEntityToNBT(par1NBTTagCompound);

        par1NBTTagCompound.setBoolean("Saddle", this.getSaddled());

    }



    /**

     * (abstract) Protected helper method to read subclass entity data from NBT.

     */

    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)

    {

        super.readEntityFromNBT(par1NBTTagCompound);

        this.setSaddled(par1NBTTagCompound.getBoolean("Saddle"));

    }



    /**

     * Returns the sound this mob makes while it's alive.

     */

    protected String getLivingSound()

    {

        return "mob.pig.say";

    }



    /**

     * Returns the sound this mob makes when it is hurt.

     */

    protected String getHurtSound()

    {

        return "mob.pig.say";

    }



    /**

     * Returns the sound this mob makes on death.

     */

    protected String getDeathSound()

    {

        return "mob.pig.death";

    }



    /**

     * Plays step sound at given x, y, z for the entity

     */

    protected void playStepSound(int par1, int par2, int par3, int par4)

    {

        this.playSound("mob.pig.step", 0.15F, 1.0F);

    }



    /**

     * Called when a player interacts with a mob. e.g. gets milk from a cow, gets into the saddle on a pig.

     */

    public boolean interact(EntityPlayer par1EntityPlayer)

    {

        if (super.interact(par1EntityPlayer))

        {

            return true;

        }

        else if (this.getSaddled() && !this.worldObj.isRemote && (this.riddenByEntity == null || this.riddenByEntity == par1EntityPlayer))

        {

            par1EntityPlayer.mountEntity(this);

            return true;

        }

        else

        {

            return false;

        }

    }



    /**

     * Returns the item ID for the item the mob drops on death.

     */

    protected int getDropItemId()

    {

        return this.isBurning() ? Item.porkCooked.itemID : Item.porkRaw.itemID;

    }



    /**

     * Drop 0-2 items of this living's type. @param par1 - Whether this entity has recently been hit by a player. @param

     * par2 - Level of Looting used to kill this mob.

     */

    protected void dropFewItems(boolean par1, int par2)

    {

        int var3 = this.rand.nextInt(3) + 1 + this.rand.nextInt(1 + par2);



        for (int var4 = 0; var4 ‹ var3; ++var4)

        {

            if (this.isBurning())

            {

                this.dropItem(Item.porkCooked.itemID, 1);

            }

            else

            {

                this.dropItem(Item.porkRaw.itemID, 1);

            }

        }



        if (this.getSaddled())

        {

            this.dropItem(Item.saddle.itemID, 1);

        }

    }



    /**

     * Returns true if the pig is saddled.

     */

    public boolean getSaddled()

    {

        return (this.dataWatcher.getWatchableObjectByte(16) & 1) != 0;

    }



    /**

     * Set or remove the saddle of the pig.

     */

    public void setSaddled(boolean par1)

    {

        if (par1)

        {

            this.dataWatcher.updateObject(16, Byte.valueOf((byte)1));

        }

        else

        {

            this.dataWatcher.updateObject(16, Byte.valueOf((byte)0));

        }

    }



    /**

     * Called when a lightning bolt hits the entity.

     */

    public void onStruckByLightning(EntityLightningBolt par1EntityLightningBolt)

    {

        if (!this.worldObj.isRemote)

        {

            EntityPigZombie var2 = new EntityPigZombie(this.worldObj);

            var2.setLocationAndAngles(this.posX, this.posY, this.posZ, this.rotationYaw, this.rotationPitch);

            this.worldObj.spawnEntityInWorld(var2);

            this.setDead();

        }

    }



    /**

     * Called when the mob is falling. Calculates and applies fall damage.

     */

    protected void fall(float par1)

    {

        super.fall(par1);



        if (par1 › 5.0F && this.riddenByEntity instanceof EntityPlayer)

        {

            ((EntityPlayer)this.riddenByEntity).triggerAchievement(AchievementList.flyPig);

        }

    }



    /**

     * This function is used when two same-species animals in 'love mode' breed to generate the new baby animal.

     */

    public EntityPig spawnBabyAnimal(EntityAgeable par1EntityAgeable)

    {

        return new EntityPig(this.worldObj);

    }



    /**

     * Checks if the parameter is an item which this animal can be fed to breed it (wheat, carrots or seeds depending on

     * the animal type)

     */

    public boolean isBreedingItem(ItemStack par1ItemStack)

    {

        return par1ItemStack != null && par1ItemStack.itemID == Item.carrot.itemID;

    }



    /**

     * Return the AI task for player control.

     */

    public EntityAIControlledByPlayer getAIControlledByPlayer()

    {

        return this.aiControlledByPlayer;

    }



    public EntityAgeable createChild(EntityAgeable par1EntityAgeable)

    {

        return this.spawnBabyAnimal(par1EntityAgeable);

    }

}




Разбираем:



public class EntityPig extends EntityAnimal


Наша свинья будет домашним животным (скотом).


 public EntityPig(World par1World)

    {

        super(par1World);

        this.texture = "/mob/pig.png";

        this.setSize(0.9F, 0.9F);

        this.getNavigator().setAvoidsWater(true);

        float var2 = 0.25F;

        this.tasks.addTask(0, new EntityAISwimming(this));

        this.tasks.addTask(1, new EntityAIPanic(this, 0.38F));

        this.tasks.addTask(2, this.aiControlledByPlayer = new EntityAIControlledByPlayer(this, 0.34F));

        this.tasks.addTask(3, new EntityAIMate(this, var2));

        this.tasks.addTask(4, new EntityAITempt(this, 0.3F, Item.carrotOnAStick.itemID, false));

        this.tasks.addTask(4, new EntityAITempt(this, 0.3F, Item.carrot.itemID, false));

        this.tasks.addTask(5, new EntityAIFollowParent(this, 0.28F));

        this.tasks.addTask(6, new EntityAIWander(this, var2));

        this.tasks.addTask(7, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));

        this.tasks.addTask(8, new EntityAILookIdle(this));

    }


Текстура, размеры, интелект.


 public boolean isAIEnabled()

    {

        return true;

    }



    public int getMaxHealth()

    {

        return 10;

    }



    protected void updateAITasks()

    {

        super.updateAITasks();

    }




Интелект и здоровье.


     {

        ItemStack var1 = ((EntityPlayer)this.riddenByEntity).getHeldItem();

        return var1 != null && var1.itemID == Item.carrotOnAStick.itemID;

    }






пытается достать морковку на удочке (начинает ходить за ней)


    public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)

    {

        super.writeEntityToNBT(par1NBTTagCompound);

        par1NBTTagCompound.setBoolean("Saddle", this.getSaddled());

    }



    /**

     * (abstract) Protected helper method to read subclass entity data from NBT.

     */

    public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)

    {

        super.readEntityFromNBT(par1NBTTagCompound);

        this.setSaddled(par1NBTTagCompound.getBoolean("Saddle"));

    }


Возможность носить на себе седло


    protected String getLivingSound()

    {

        return "mob.pig.say";

    }



    /**

     * Returns the sound this mob makes when it is hurt.

     */

    protected String getHurtSound()

    {

        return "mob.pig.say";

    }



    /**

     * Returns the sound this mob makes on death.

     */

    protected String getDeathSound()

    {

        return "mob.pig.death";

    }



    /**

     * Plays step sound at given x, y, z for the entity

     */

    protected void playStepSound(int par1, int par2, int par3, int par4)

    {

        this.playSound("mob.pig.step", 0.15F, 1.0F);

    }


Звуки моба.


  protected int getDropItemId()

    {

        return this.isBurning() ? Item.porkCooked.itemID : Item.porkRaw.itemID;

    }

Дроп: свинина и жаренная свинина.


  protected void dropFewItems(boolean par1, int par2)

    {

        int var3 = this.rand.nextInt(3) + 1 + this.rand.nextInt(1 + par2);



        for (int var4 = 0; var4 ‹ var3; ++var4)

        {

            if (this.isBurning())

            {

                this.dropItem(Item.porkCooked.itemID, 1);

            }

            else

            {

                this.dropItem(Item.porkRaw.itemID, 1);

            }

        }



        if (this.getSaddled())

        {

            this.dropItem(Item.saddle.itemID, 1);

        }

    }

Условие получение дропа.

дальше идет работа с мобов (Превращение в ЗомбиСвинью и хождение за морковй)



public EntityAgeable createChild(EntityAgeable par1EntityAgeable)
{
return this.spawnBabyAnimal(par1EntityAgeable);
}

При размножении появляется детеныш


Готово. Теперь в EntityList.java прописываем моба и создаем яйцо призыва


addMapping(EntityPig.class, "Pig", 90, 15771042, 14377823);

Разбираем:
EntityPig.class - наш файл со свиньей
"Pig" - Локализическое имя моба
90 - ID моба
15771042, 14377823 - цвет яйца призыва.

Готово! Теперь наш моб может жить, размножаться и следить за игроком.
карма: 6
2
Голосовали:OTBETCTBEHHblN, SuperSs
Ответов: 815
NeverClan
Assistants
#2: 2014-11-30 23:32:13 ЛС | профиль | цитата

public void writeEntityToNBT(NBTTagCompound par1NBTTagCompound)

public void readEntityFromNBT(NBTTagCompound par1NBTTagCompound)


Кусок кода отвечающий за загрузку и сохранение моба.
Где Saddle переменная, отвечающая за то, имеется седло на данный момент или нет.
карма: 44
Строил спауны еще до того как открыли Guest
1
Голосовали:Flanagun
2
Сообщение
...