⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 insect.java

📁 SIMULATION FOURMILIERE -3D-ISOMETRIQUE
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            else if (curX == lastX && curY > lastY) dir = Direction.DOWN;
            else dir = Direction.RIGHT_DOWN;
        }
    }
    
    
    /**
     * Called when an insect fight with another insect
     * @param insect the other insect we fight with
     */
    public void fight(Insect insect) {
        // If our opponent is dead, we stop the fight
        if (insect == null || insect.energy <= 0) {
            isFighting = false;
            opponent = null;
            order = Order.NONE;
            orderAuthor = null;
        } else {
            // If it is our first kick, we ask for some help
            if(!isFighting)
                askForHelpToFight();
            isFighting = true;
            opponent = insect;
            insect.isFighting = true;
            insect.opponent = this;
            opponent.isFighting = true;
            // We kick him with all our power and hope we kill him
            //insect.decreaseEnergy(Double.valueOf(strength*Math.random()).intValue());
            insect.decreaseEnergy(strength);
            if (insect.energy <= 0) {
                isFighting = false;
                opponent = null;
                order = Order.NONE;
                orderAuthor = null;
                // If we won the fight, our skills are increased
                rank.increaseExperience(this);
            }
        }
    }
    
    
    /** 
     * Called when an insect die.
     */
    protected void die() {
        if (opponent != null && opponent.isFighting) {
            opponent.isFighting = false;
        }
        opponent = null;
        orderAuthor = null;
        team.removeInsect(this);
    }
    
    
    /**
     * Called when an insect eat something
     * @param foodOffered   quantity of food we could eat
     * @return the quantity of food left at the end of the operation
     */
    public int eat(int foodOffered) {
        int foodTaken = (energy + foodOffered) < maxEnergy
            ? foodOffered
            : maxEnergy - energy;
        energy += foodTaken;
        return foodOffered - foodTaken;
    }
    
    
    /**
     * Called when an insect eat as much as he can
     */
    public void eat() {
        energy = maxEnergy;
        food = maxFood;
    }
    
    
    /**
	 * Give the picture of the insect
	 * @return  the picture
	 */
    public Values getPicture() {
        return picture;
    }
    
    
    /**
     * Give the picture of the insect, followed by his actions pictures. For exemple,
     * the picture showing that he is fighting or that he need food
     * @return the list of pictures
     */
    public EnumSet<Values> getPictures() {
        EnumSet<Values> elems = EnumSet.noneOf(Values.class);
        elems.add(getPicture());
        if (isFighting)
            elems.add(Values.ins_fight);
        if (needFood())
            elems.add(Values.ins_help);
        if (isPoisoned)
            elems.add(Values.ins_poison);
        if (isInGroup)
            elems.add(Values.select);
        /* pictures if the insect is ranked */
        if (rank == Rank.SERGEANT)
        	elems.add(Values.rnk_sergeant);
        else if (rank == Rank.CAPTAIN)
        	elems.add(Values.rnk_captain);
        else if (rank == Rank.MAJOR)
        	elems.add(Values.rnk_major);
        else if (rank == Rank.COLONEL)
        	elems.add(Values.rnk_colonel);
        else if (rank == Rank.GENERAL)
        	elems.add(Values.rnk_general);

        return elems;
    }
    
    
    /**
	 * Give the energy of the insect
	 * @return  the energy
	 */
    public int getEnergy() {
        return energy;
    }
    
    
    /**
     * Give the current position of the insect
     * @return the Point of the insect
     */
    public WorldPoint getPos() {
        return curPos;
    }
    
    
    /**
	 * Cheks if the insect is fighting
	 * @return  true if insect is fighting, else false;
	 */
    public boolean isFighting() {
        return isFighting;
    }

    
    /**
     * Checks if the insect needs to demand food
     * @return boolean
     */
    protected boolean needFood() {
        return energy < maxEnergy / 4
            ? true
            : false;
    }

    
    /**
     * Ask some food
     * @param nbCaseAround    The number of case to see around
     */
    protected void askForFood(int nbCaseAround) {
        LinkedList<Insect> friend;

        friend = team.getWorld().giveInsectAround(this, nbCaseAround, true);
        for (Insect insect : friend) {
            if (!insect.isFighting && (insect instanceof AntWorker)) {
                AntWorker antWorker = (AntWorker)insect;
                if(antWorker.order == Order.NONE && antWorker.hasFood()) {
                    antWorker.setDirection(curPos, Order.GIVE_FOOD, this);
                    return;
                }
            }
        }
    }
    
    
    /**
     * Checks if the insect needs to demand food
     * @return boolean
     */
    public boolean needFood(int quarter) {
        return energy < maxEnergy / quarter ? true : false;
    }
    
    
    /**
     * Give order to an Worker to give food
     */
    public void askForHelpToFight() {
        if (opponent == null)
            return;
        LinkedList<Insect> friend = team.getWorld().giveInsectAround(this, radiusFight, true);
        for (Insect insect : friend)
            if (!insect.isFighting && insect.order != Order.FIGHT) {
            	insect.opponent = opponent;
                insect.setDirection(opponent.curPos, Order.FIGHT, this);
            }
    }
    
    
    /**
     * Checks if the insect is near the point of destination
     * @return boolean    true if we are near the destination, false else.
     */
    public boolean isNearDestination() {
        return curPos.distance(destPos) < 2;
    }
    
    
    /**
     * Give the insect's team
	 * @return  the team
	 */
    public Team getTeam() {
        return team;
    }
    
    
    /**
     * Decrease the energy of the insect, so that he needs food to survive
     */
    protected void loseEnergy() {
    		decreaseEnergy(1);
    }
    
    
    /**
     * Force the insect to loose some energy. If it reach '0', then the insect die.
     * (This is the hard low of the Nature)
     * @param quantity  quantity of energy lost.
     */
    protected void decreaseEnergy(int quantity) {
    	if(isFighting && isShieldActivated) {
    		// If an insect got a shield, damages are reduced
    		double tmp = Randomizer.getRangeDouble(2./3., 1.);
    		double qtyLost = tmp * quantity; 
    		energy -= qtyLost <= 1 ? 1 : qtyLost; 
    	}
    	else
    		energy -= quantity;
        if (energy <= 0)
            die();
    }
    
    
    /**
     * Equivalent to a method live(). Give the instruction of what the
     * insect must do in the next turn. The action depends on the current
     * behavior of the insect
     */
    public void getAction() {
        // If we are poisoned, we loose a lot of energy
        if (isPoisoned) {
            decreaseEnergy(POISON_DAMAGE);
            if (energy <= 0)
            	return;
        }
        // If we are on a group, we just follow him
        if (isInGroup)
            curBehavior = BehaviorFactory.getBehaviorGroup();
        // Else we do the default action of the insect
        if (curBehavior != null)
            curBehavior.doAction(this);
        // And we refresh the pictures
        team.getWorld().updateModel(curPos.getX(), curPos.getY(), false);
    }
    
    
    /**
	 * @return true if the insect is currently in a group
	 */
    public boolean getIsInGroup() {
        return isInGroup;
    }

    /**
	 * @param state assign / unassign the insect in a group
	 */
    void setIsInGroup(boolean state) {
        this.isInGroup = state;
    }


    /**
     * Give the insect who ordered us to do something
     * @return the insect, else null
     */
    public Insect getOrderAuthor() {
        return orderAuthor;
    }

	/**
	 * Give the quantity of food of the insect if it is able to handle it
	 * @return the quantity of food handled
	 */
    public int getFood() {
        return food;
    }

    /**
     * Sets the quantity of food handled by the insect
     * @param food	the quantity of food handled
     */
    public void setFood(int food) {
        this.food = food;
    }

    /**
     * Give the opponent of the insect
     * @return the opponent
     */
    public Insect getOpponent() {
        return opponent;
    }

    /**
     * Give the maximum of food the insect the insect can handle
     * @return the maximum of food handled
     */
    public int getMaxFood() {
        return maxFood;
    }

    /**
     * Give the speed of the insect
     * @return the speed of the insect
     */
    public int getSpeed() {
        return speed;
    }

    /**
     * Give the time before a new action
     * @return the time before the next action
     */
    public int getTimeBeforeActing() {
        return timeBeforeActing;
    }
    

    /**
     * Set the time before a new action
     * @param timeBeforeActing the time before acting
     */
    public void setTimeBeforeActing(int timeBeforeActing) {
        this.timeBeforeActing = timeBeforeActing;
    }

    /**
     * Give the order of the insect has to do
     * @return the order to execute
     */
    public Order getOrder() {
        return order;
    }

    /**
     * Set the order to be executed by the insect
     * @param order the order to transmit to the insect
     */
    public void setOrder(Order order) {
        this.order = order;
    }

    /**
     * Give the point of reference of the insect 
     * @return the point of reference
     */
    public WorldPoint getRefPoint() {
        return refPoint;
    }

    /**
     * Set the point of reference of the insect
     * @param refPoint the point of reference to transmit
     */
    public void setRefPoint(WorldPoint refPoint) {
        this.refPoint = refPoint;
    }
    
    /**
     * Set if the insect was poisoned or not
     * @param state true if poisoned, false else
     */
    protected void setPoisoned(boolean state) {
        isPoisoned = state;
    }

    /**
     * Set the rank of the insect
     * It permits to increase easily the level of the insect
     * @param willingRank the rank wished for the insect
     */
	protected void setRank(Rank willingRank) {
		while(rank.ordinal() < willingRank.ordinal()) 
			rank.increaseExperience(this);
	}

	
	/**
	 * Add some strength to the insect
	 * @param number the strength to add
	 */
	void addStrength(int number) {
		strength += number;
	}


	/**
	 * Give how many fights the insect must still win to increase his rank
	 * @return the number of fights to win
	 */
	int getNextRank() {
		return nextRank;
	}


	/**
	 * Assign the number of fights to win before changing of ranck
	 * @param nextRank the number of fights to win
	 */
	void setNextRank(int nextRank) {
		this.nextRank = nextRank;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -