Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

How to update an entity's AI


TheRealMcrafter
 Share

Recommended Posts

Tried a few things:

 

if (this.isPlaying){
       List l = this.worldObj.getEntitiesWithinAABB(EntityVillager.class, AxisAlignedBB.getBoundingBox(this.xCoord - 20, this.yCoord - 20, this.zCoord - 20, this.xCoord + 20, this.yCoord + 20, this.zCoord + 20));

       if (!l.isEmpty()){
	for (int i = 0; i < l.size(); i++){
		EntityVillager villager = (EntityVillager) l.get(i);
		((EntityVillager) villager).tasks.addTask(0, new EntityAIPanic(villager, 0.9D));

                        //((EntityVillager) villager).targetTasks.addTask(0, new EntityAIPanic(villager, 0.9D));
		//((EntityVillager) villager).tasks.onUpdateTasks();
		//((EntityVillager) villager).targetTasks.onUpdateTasks();
	}
}
}

Link to comment
Share on other sites

Okay, I got it to work how I want it. But now, my sirens only play for like a half a second and then they stop. If I remove this code from my tile entity, the sounds play all the way through and continue looping.

 


if (this.isPlaying){
List l = this.worldObj.getEntitiesWithinAABB(EntityVillager.class, AxisAlignedBB.getBoundingBox(this.xCoord - 20, this.yCoord - 20, this.zCoord - 20, this.xCoord + 20, this.yCoord + 20, this.zCoord + 20));

if (!l.isEmpty()){
	for (int i = 0; i < l.size(); i++){
		EntityVillager villager = (EntityVillager) l.get(i);
		ExtendedVillager props = ExtendedVillager.get(villager);
		props.isScared = true;
	}
}
}

 

Anyone have an idea what I'm doing wrong?

Link to comment
Share on other sites

I play the sound by sending a packet with the sound name from the tile entity to all nearby players in the same world, and then passing the sound name to my Looping Audio Handler, and then passing that to my Siren Looper class.

 

Tile Entity:

if (!isPlaying && shouldStart){
			shouldStart = false; 
			shouldStop = false; 
			isPlaying = true; 
			this.updateClientRender();
			SirenModPacketDispatcher.sendToAllAround(new SirenModPlayLoopedSoundMessage(xCoord, yCoord, zCoord, "sirenT121"), this.worldObj.provider.dimensionId, xCoord, yCoord, zCoord, 50000000);
		}

 

Play Sound Message:

public IMessage handleClientMessage(EntityPlayer player, SirenModPlayLoopedSoundMessage message, MessageContext ctx) {
    		new LoopingAudioHandler(player.worldObj, message.x, message.y, message.z, message.soundName);

 

Looping Audio Handler:

public LoopingAudioHandler(World world, int x, int y, int z, String soundName){
	SirenLooper looper = new SirenLooper(world.getTileEntity(x, y, z), soundName);
	Minecraft.getMinecraft().getSoundHandler().playSound(looper);
}

 

Siren Looper:

public SirenLooper(TileEntity tile, String soundName){ 
        super(new ResourceLocation("SirenMod:" + soundName));  
        this.tile = tile;
        this.repeat = true; 
        if (tile instanceof NuclearSirenTileEntity){
            volume = 5.0F;
    	} else if (tile instanceof GeneralSirenTileEntity){
            volume = 10.0F;
    	} else if (tile instanceof BurglarSirenTileEntity){
            volume = 5.0F;
    	} else if (tile instanceof FireAlarmTileEntity){
            volume = 5.0F;
    	} else if (tile instanceof AmericanSignalT121TileEntity){
            volume = 20.0F;

    	} else {
            volume = 5.0F;
    	}
        this.xPosF = tile.xCoord;
        this.yPosF = tile.yCoord;
        this.zPosF = tile.zCoord;
    }


    public void update(){ 
    	if (tile instanceof GeneralSirenTileEntity){
    		if (((GeneralSirenTileEntity) tile).isShouldStop() || ((GeneralSirenTileEntity) tile).isInvalid()){  
                this.donePlaying = true;}
    	} else if (tile instanceof NuclearSirenTileEntity){
    		if (((NuclearSirenTileEntity) tile).isShouldStop() || ((NuclearSirenTileEntity) tile).isInvalid()){    
                this.donePlaying = true;}
    	} else if (tile instanceof BurglarSirenTileEntity){
    		if (((BurglarSirenTileEntity) tile).isShouldStop() || ((BurglarSirenTileEntity) tile).isInvalid()){    
                this.donePlaying = true;}
    	} else if (tile instanceof FireAlarmTileEntity){
    		if (((FireAlarmTileEntity) tile).isShouldStop() || ((FireAlarmTileEntity) tile).isInvalid()){    
                this.donePlaying = true;}
    	} else if (tile instanceof AmericanSignalT121TileEntity){
    		if (((AmericanSignalT121TileEntity) tile).isShouldStop() || ((AmericanSignalT121TileEntity) tile).isInvalid()){    
                this.donePlaying = true;}
    	} else {
    		throw new ClassCastException();
    	}   
    }

@Override
    public boolean isDonePlaying(){
        return this.donePlaying;} 
}

Link to comment
Share on other sites

public void setShouldStop(boolean shouldStop){
    	if (shouldStop){
    		if (isPlaying){
    			isPlaying = false; 
    			this.shouldStop = true;
    		}
    	} else {
    		this.shouldStop = shouldStop;
    	}
    }

 

 

I know I could have just done

 

public void setShouldStop(boolean shouldStop){
    this.shouldStop = shouldStop;
}

 

but in order for my sirens to work properly, I have to do the above. Everything worked fine until I implemented the AI changes above, then they play for a few seconds and stop.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.