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

📄 gamecontroller.java

📁 j2me实现的一款小游戏
💻 JAVA
字号:
package istarion.core;

import java.util.*;
import javax.microedition.lcdui.*;
import istarion.frontend.*;


/**
 * Coordinates the flow of the game.
 */
public class GameController
{

	private static int DEBUG = 0;

	/**
	 * Reference to MIDlet object.
	 */
	private Istarion 	__istarion = null;

	/**
	 * The player character, who is playing the game.
	 */
	private Player __player = null;

	private DungeonFrontend __dungeonFrontend = null;

	/**
	 * Vertical size of the dungeon environment
	 * visible to the player (number of columns).
	 */
	private int __duncols;

	/**
	 * Horizontal size of the dungeon environment
	 * visible to the player (number of rows).
	 */
	private int __dunrows;


	private static Vector __gameMsgs = null;


	/**
	 * Constructor.
	 */
	public GameController(Istarion m)
	{
		__istarion = m;

		Toolkit.tk().setDebugLevel(DEBUG);

		__gameMsgs = new Vector();
		__gameMsgs.addElement("Welcome to Istarion!");

		//__builder = new WorldBuilder();
	}


	public Istarion getIstarion()
	{
		return __istarion;
	}


	/**
	 * Initializes the game.
	 * Creates game world and player character.
	 */
	public void startBackend()
	{
		__istarion.showStartScreen();
	}


	/**
	 * Starts character generation and world generation.
	 */
	private void generateCharacter()
	{
		//new CharGen(this).createCharacter();
		startNewGame(new Player("bla", new Race(), new Profession()));
	}


	/**
	 * Starts a new game with the specified player character.
	 * Is being called by CharGen when character generation is complete.
	 */
	public void startNewGame(Player p)
	{
		__player = p;
		ProgressBar pb = __istarion.getProgressBar();
		WorldBuilder wb = new WorldBuilder(pb);
		Dungeon dungeon = wb.buildDungeon(1);//level 1 dungeon bauen.
		pb = null;
		wb = null;
		__player.enterDungeon(dungeon);

		__dungeonFrontend = __istarion.getDungeonFrontend();
		__duncols = __dungeonFrontend.getDungeonDisplayColumns();
		__dunrows = __dungeonFrontend.getDungeonDisplayRows();
		__updateDungeonFrontend();
		__dungeonFrontend.doShow();
	}



	public Player getPlayer()
	{
		return __player;
	}


	public static void gameMessage(String txt)
	{
		while (__gameMsgs.size() > 10)
			__gameMsgs.removeElement(__gameMsgs.firstElement());
		__gameMsgs.addElement(txt);
	}


	/**
	 * Processes a user's action from the frontend.
	 *
	 * @param cmd a String identifying the command the user entered.
	 * @param disp a String identifying the GUI element visible
	 * at the time, the user entered the command.
	 */
	public void frontendAction(String cmd, String disp)
	{
		boolean moved = false;

		if (disp.equals("startscreen"))
		{
			if (cmd.equals("New"))
			{
				generateCharacter();
			} else if (cmd.equals("Load")) {
				//load an old game.
			}
		}

		if (disp.equals("dungeonscreen"))
		{
			if (cmd.equals("Cmds"))
			{
				__istarion.showMainMenu();
			} else if (cmd.equals("go North")) {
				moved = getPlayer().move(Dungeon.NORTH);
			} else if (cmd.equals("go South")) {
				moved = getPlayer().move(Dungeon.SOUTH);
			} else if (cmd.equals("go East")) {
				moved = getPlayer().move(Dungeon.EAST);
			} else if (cmd.equals("go West")) {
				moved = getPlayer().move(Dungeon.WEST);
			}
		}

		if (disp.equals("mainmenu"))
		{
			if (cmd.equals("Back"))
			{
				__dungeonFrontend.doShow();
			}
		}

		//z黦e der monster ausf黨ren
		if (moved)
		{
			__executeGameTurn();
			__updateDungeonFrontend();
		}


	}


	/**
	 * Simulates a complete turn of the game after
	 * an action from the player took place.
	 * Redraws the dungeon screen afterwards.
	 */
	private void __executeGameTurn()
	{
        getPlayer().getDungeon().callAllMobiles();
 	}


	/**
	 * Calculates line of sight and updates the displayed
	 * dungeon environment on the frontend.
	 */
	private void __updateDungeonFrontend()
	{
		char[][] dungeon = new char[__duncols][__dunrows];
		Vector[][] items = new Vector[__duncols][__dunrows];
		Mobile[][] mobiles = new Mobile[__duncols][__dunrows];
		int half_cols = __duncols / 2;
		int half_rows = __dunrows / 2;

		int targetX, targetY;
		int relX, relY;
		int x, y;

		for (x = 0; x < __duncols; x++)
		{
			targetX = __player.getXLocation() + x - half_cols;
			targetY = __player.getYLocation() - half_rows;
			__drawLoSMatrixLine(dungeon, items, mobiles, targetX, targetY);

			targetX = __player.getXLocation() + x - half_cols;
			targetY = __player.getYLocation() + half_rows;
			__drawLoSMatrixLine(dungeon, items, mobiles, targetX, targetY);
		}

		for (y = 0; y < __dunrows; y++)
		{
			targetX = __player.getXLocation() - half_cols;
			targetY = __player.getYLocation() + y - half_rows;
			__drawLoSMatrixLine(dungeon, items, mobiles, targetX, targetY);

			targetX = __player.getXLocation() + half_cols;
			targetY = __player.getYLocation() + y - half_rows;
			__drawLoSMatrixLine(dungeon, items, mobiles, targetX, targetY);
		}

		/*
		//player memory:
		for (x = 0; x < __duncols; x++)
			for (y = 0; y < __dunrows; y++)
			{
				relX = getXLocation() + x - (__duncols / 2);
				relY = getYLocation() + y - (__dunrows / 2);
				if (sightMatrix[x][y] == 0)
					sightMatrix[x][y] = getMemory(relX, relY);
			}
		*/

		//send result to frontend:
		__dungeonFrontend.redrawDungeon(dungeon, mobiles, items,
			(String)__gameMsgs.lastElement());
	}



	private void __drawLoSMatrixLine(char[][] dungeon, Vector[][] items,
		Mobile[][] mobiles, int targetX, int targetY)
	{
		int player_x, player_y;
		int check_x, check_y;
		int diff_x, diff_y;
		int matrix_x, matrix_y;
		int abs_diff_x, abs_diff_y;
		int signum;
		char static_arch;
		int i;
		int half_cols = __duncols / 2;
		int half_rows = __dunrows / 2;
		Dungeon d = __player.getDungeon();

		player_x = __player.getXLocation();
		player_y = __player.getYLocation();
		diff_x = targetX - player_x;
		diff_y = targetY - player_y;
		abs_diff_x = Math.abs(diff_x);
		abs_diff_y = Math.abs(diff_y);

		if (abs_diff_x > abs_diff_y)
		{

			if (diff_x < 0) signum = -1; else signum = 1;
			check_x = player_x;
			check_y = player_y;

			for (i = 1; i <= abs_diff_x; i++)
			{
				matrix_x = check_x - player_x + half_cols;
				matrix_y = check_y - player_y + half_rows;
				dungeon[matrix_x][matrix_y] = d.getStaticItem(check_x, check_y);
				mobiles[matrix_x][matrix_y] = d.getMobile(check_x, check_y);
				items[matrix_x][matrix_y] = d.getItems(check_x, check_y);

				if (mobiles[matrix_x][matrix_y] != null)
					mobiles[matrix_x][matrix_y].hasPlayerContact(__player);

				if (__player.getDungeon().isCellBlockingSight(check_x, check_y) != false)
					break;

				check_x += signum;
				check_y = player_y + ((i * diff_y) / diff_x);
			}

		} else {

			if (diff_y < 0) signum = -1; else signum = 1;
			check_x = player_x;
			check_y = player_y;

			for (i = 1; i <= abs_diff_y; i++)
			{
				matrix_x = check_x - player_x + half_cols;
				matrix_y = check_y - player_y + half_rows;
				dungeon[matrix_x][matrix_y] = d.getStaticItem(check_x, check_y);
				mobiles[matrix_x][matrix_y] = d.getMobile(check_x, check_y);
				items[matrix_x][matrix_y] = d.getItems(check_x, check_y);

				if (mobiles[matrix_x][matrix_y] != null)
					mobiles[matrix_x][matrix_y].hasPlayerContact(__player);

				if (__player.getDungeon().isCellBlockingSight(check_x, check_y) != false)
					break;

				check_x = player_x + ((i * diff_x) / diff_y);
				check_y += signum;
			}
		}

	}


}

⌨️ 快捷键说明

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