Пример #1
0
void AI::generateMovement()
{
	//for every entity, check distance from hero, if the attacking entity is close enough, move to it.
	for(unsigned int i = 0 ; i < CEntity::EntityList.size() ; i++)
	{
		double entX = (double)CEntity::EntityList[i]->xPos;
		double entY = (double)CEntity::EntityList[i]->yPos;
		double charX = (double)character::charControl.retPosX();
		double charY = (double)character::charControl.retPosY();

		//calculate the distance from the hero to the entity
		int distFromChar = (int)sqrt((entX-charX)*(entX-charX) + (entY-charY)*(entY-charY));
		
		//if (distFromChar <= 250 && (entX - charX > heroWidth || entY - charY > heroHeight || charX - entX > CEntity::EntityList[i]->widthCol || charY - entY > CEntity::EntityList[i]->heightCol)) //if the entity is close enough to the hero
		if (distFromChar > 50 && distFromChar <= 250)
		{
			//for checking which side the entity has to face, we calculate the angle using the 2 points
			float angle = atan2(entY - charY, entX - charX); //returns in radiants
			angle = angle * 180 / PI; //convert to degrees
			CEntity::EntityList[i]->setStartFrame(i, 0);
			CEntity::EntityList[i]->setMaxFrame(i, 4);
			CEntity::EntityList[i]->OnLoop();
			entityDirection(angle,i);
			moveEntityToChar(i);
			
		}
		//if entity is close to target
		else if (distFromChar < 50)
		{
			//CEntity::EntityList[i]->setFrame(i, 0);
			attackEntity(i);
		}
	}
}
Пример #2
0
void EntityCreature::updatePlayerActionState() 
{
        hasAttacked = getCanWalk();
        float f = 16;
        if(playerToAttack == null)
        {
            playerToAttack = findPlayerToAttack();
            if(playerToAttack != null)
            {
                pathToEntity = worldObj.getPathToEntity(this, playerToAttack, f);
            }
        } else
        if(!playerToAttack.isEntityAlive())
        {
            playerToAttack = null;
        } else
        {
            float f1 = playerToAttack.getDistanceToEntity(this);
            if(canEntityBeSeen(playerToAttack))
            {
                attackEntity(playerToAttack, f1);
            }
        }
        if(!hasAttacked && playerToAttack != null && (pathToEntity == null || rand.nextInt(20) == 0))
        {
            pathToEntity = worldObj.getPathToEntity(this, playerToAttack, f);
        } else
        if(!hasAttacked && (pathToEntity == null && rand.nextInt(80) == 0 || rand.nextInt(80) == 0))
        {
            boolean flag = false;
            int j = -1;
            int k = -1;
            int l = -1;
            float f2 = -99999;
            for(int i1 = 0; i1 < 10; i1++)
            {
                int j1 = MathHelper.floor_double((posX + (double)rand.nextInt(13)) - 6);
                int k1 = MathHelper.floor_double((posY + (double)rand.nextInt(7)) - 3);
                int l1 = MathHelper.floor_double((posZ + (double)rand.nextInt(13)) - 6);
                float f3 = getBlockPathWeight(j1, k1, l1);
                if(f3 > f2)
                {
                    f2 = f3;
                    j = j1;
                    k = k1;
                    l = l1;
                    flag = true;
                }
            }

            if(flag)
            {
                pathToEntity = worldObj.getEntityPathToXYZ(this, j, k, l, 10);
            }
        }
        int i = MathHelper.floor_double(boundingBox.minY + 0.5);
        boolean flag1 = func_27013_ag();
        boolean flag2 = handleLavaMovement();
        rotationPitch = 0.0F;
        if(pathToEntity == null || rand.nextInt(100) == 0)
        {
            super.updatePlayerActionState();
            pathToEntity = null;
            return;
        }
        Vec3 vec3d = pathToEntity.getPosition(this);
        for(double d = width * 2.0F; vec3d != null && vec3d.squareDistanceTo(posX, vec3d.yCoord, posZ) < d * d;)
        {
            pathToEntity.incrementPathIndex();
            if(pathToEntity.isFinished())
            {
                vec3d = null;
                pathToEntity = null;
            } else
            {
                vec3d = pathToEntity.getPosition(this);
            }
        }

        isJumping = false;
        if(vec3d != null)
        {
            double d1 = vec3d.xCoord - posX;
            double d2 = vec3d.zCoord - posZ;
            double d3 = vec3d.yCoord - (double)i;
            float f4 = (float)((Math.atan2(d2, d1) * 180) / 3.1415927410125732) - 90;
            float f5 = f4 - rotationYaw;
            moveForward = moveSpeed;
            for(; f5 < -180; f5 += 360) { }
            for(; f5 >= 180; f5 -= 360) { }
            if(f5 > 30)
            {
                f5 = 30;
            }
            if(f5 < -30)
            {
                f5 = -30;
            }
            rotationYaw += f5;
            if(hasAttacked && playerToAttack != null)
            {
                double d4 = playerToAttack.posX - posX;
                double d5 = playerToAttack.posZ - posZ;
                float f7 = rotationYaw;
                rotationYaw = (float)((Math.atan2(d5, d4) * 180) / 3.1415927410125732) - 90;
                float f6 = (((f7 - rotationYaw) + 90) * 3.141593) / 180;
                moveStrafing = -MathHelper.sin(f6) * moveForward * 1.0F;
                moveForward = MathHelper.cos(f6) * moveForward * 1.0F;
            }
            if(d3 > 0.0D)
            {
                isJumping = true;
            }
        }
        if(playerToAttack != null)
        {
            faceEntity(playerToAttack, 30, 30);
        }
        if(isCollidedHorizontally && !hasPath())
        {
            isJumping = true;
        }
        if(rand.nextFloat() < 0.8 && (flag1 || flag2))
        {
            isJumping = true;
        }
}