头歌植物大战僵尸第5关

1. TrainBotanyCommand.java

package step5.CampSystem;

import step5.Factory.BotanicRepeaterFactory;
import java.awt.*;

public class TrainBotanyCommand implements ITrainCommand {
    Point mPosition;
    int RowPos;

    public TrainBotanyCommand(Point pos, int rowPos) {
        mPosition = pos;
        RowPos = rowPos;
    }

    @Override
    public void Execute() {
        // 生产BotanicRepeater
        BotanicRepeaterFactory factory = new BotanicRepeaterFactory();
        factory.CreateCharacter(mPosition, RowPos);
    }
}

2. Campsite.java

package step5.CampSystem;

import step2.MediatorStructure.IGameSystem;
import java.util.ArrayDeque;
import java.util.Deque;

public class Campsite implements IGameSystem {
    protected float mTrainTime;
    protected Deque<ITrainCommand> mCommands;
    private float mTrainTimer = 0;

    public Campsite(float trainTime) {
        mTrainTime = trainTime;
    }

    public void Update() {
        UpdateCommand();
    }

    @Override
    public void Init() {
        mTrainTimer = mTrainTime;
        mCommands = new ArrayDeque<>();
    }

    private void UpdateCommand() {
        if (mCommands.size() <= 0) return;
        mTrainTimer -= 0.5f;
        if (mTrainTimer <= 0) {
            // 执行队列中的第一个命令
            ITrainCommand cmd = mCommands.pollFirst();
            cmd.Execute();
            mTrainTimer = mTrainTime;
        }
    }

    public void Train(ITrainCommand cmd) {
        mCommands.addLast(cmd);
    }

    public void CancelTrainCommand() {
        if (mCommands.size() > 0) {
            mCommands.removeLast();
            if (mCommands.size() == 0) {
                mTrainTimer = mTrainTime;
            }
        }
    }

    public int getTrainCount() { 
        return mCommands.size();
    }
}

3. Mediator.java

package step5.MediatorStructure;

import step5.CampSystem.Campsite;
import step5.CampSystem.ITrainCommand;
import step5.CharacterSystem.Bullet.Bullet;
import step5.CharacterSystem.Character.ICharacter;
import step5.CharacterSystem.CharacterSystem;
import step5.Factory.BulletFactory;
import step5.GameLevelSystem.StageSystem;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;

public class Mediator {
    private static Mediator _instance = new Mediator();
    public int[] enyrowPos;
    public int[] botanyrowPos;
    private CharacterSystem mCharacterSystem;
    public CharacterSystem getmCharacterSystem() { return mCharacterSystem; }
    private StageSystem mStageSystem;
    private Campsite mCampsitesystem;

    public static Mediator get_instance() {
        return _instance;
    }

    public void Init() {
        InitLogicArea(800, 53);
        mCharacterSystem = new CharacterSystem();
        mStageSystem = new StageSystem();
        mCampsitesystem = new Campsite(3f);
        mCharacterSystem.Init();
        mStageSystem.Init();
        mCampsitesystem.Init();
        mCharacterSystem.CreatEventSystem(mStageSystem);
    }

    public void Update() {
        mCharacterSystem.Update();
        mStageSystem.Update();
        mCampsitesystem.Update();
    }

    public void AddBotany(ICharacter botany) {
        mCharacterSystem.AddBotany(botany);
    }

    public void AddBullet(Bullet bullet) {
        mCharacterSystem.AddBullt(bullet);
    }

    public void AddEnemy(ICharacter enemy) {
        mCharacterSystem.AddEnemy(enemy);
    }

    public Bullet GetBullet(String name, Point position, Point targetPosition) {
        Bullet blt = mCharacterSystem.GetBullet(name);
        if (blt != null) {
            mCharacterSystem.AddBullt(blt);
            blt.SetActive(position, targetPosition);
            return blt;
        }
        blt = BulletFactory.CreateBullet(name, position, targetPosition);
        return blt;
    }

    public void InitLogicArea(int windowHeight, int borderHeight) {
        botanyrowPos = new int[5];
        enyrowPos = new int[5];
        int enyHeight;
        int plantHeight;
        try {
            BufferedImage bufferedImage = ImageIO.read(new FileInputStream("images/Plants/Repeater/Repeater.gif"));
            plantHeight = bufferedImage.getHeight();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            BufferedImage bufferedImage = ImageIO.read(new FileInputStream("images/Zombies/Zombie/Zombie.gif"));
            enyHeight = bufferedImage.getHeight();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int i;
        int rowheight = (windowHeight - borderHeight) / 5;
        for (i = 1; i <= 5; i++) {
            enyrowPos[i - 1] = 10 + rowheight * i - enyHeight;
            botanyrowPos[i - 1] = 10 + rowheight * i - plantHeight;
        }
    }

    public void PushCommand(ITrainCommand cmd) {
        // 将命令推送到兵营子系统
        mCampsitesystem.Train(cmd);
    }
}
posted @ 2025-06-04 22:09  是否未晚  阅读(37)  评论(0)    收藏  举报