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

📄 command.java

📁 用java开发的一个实施策略游戏源码 值得学习一下
💻 JAVA
字号:
/*
	Netwar
	Copyright (C) 2002  Daniel Grund, Kyle Kakligian, Jason Komutrattananon, & Brian Hibler.

	This file is part of Netwar.

	Netwar is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Netwar is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Netwar; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package netwar.game;
import netwar.utils.*;
import java.io.Serializable;

/**
 * A collection of Integers to indicate a player's decision to influence the game.
 * <p>
 * The action codes are as follows: <BR>
 * 0 = No command. Used for synchronizing games over the network.<BR>
 * 1 = New game object.<BR>
 *   Param 1 = x, Param 2 = y (in hex coordinates).<BR>
 *   In this case, selection[0] identifies the type to be created.<BR>
 * 2 = Set goal: location. Selected units try to get to a location.<BR>
 *   Param 1 = x, Param 2 = y (in hex coordinates).<BR>
 * 3 = Set goal: unit. Selected units try to approach/follow a Unit.<BR>
 *     If that Unit is an enemy, the units will try to destroy it.<BR>
 *   Param 1 = unit index for target. Param 2 not used.<p>
 * Additional action codes will be added as needed. Keep in mind that these will
 * never include interface controls (such as zooming or scrolling) nor will it
 * include all of the detail actions Units can perform, only the macroscopic
 * actions that can be assigned by a player.<p>
 * Note: The index used by command will be a unique identifier for the GameObject, not
 * merely the array index, which would be easy to look up. This is because indices could
 * change before the Command is executed. The GameObject referenced could even be deleted
 * in that time! 
 * @author Group N2 - Project Netwar
 * @author Daniel Grund
 */
public class Command implements Serializable {
	int action;
	int player;
	int selection[];
	int param1;
	int param2;
	//int param3... if needed, we can expand it.

	/** The set of local commands which need to be sent to other players.
	 * These will not be executed until they are echoed from the server.
	 * These commands are queued as they are generated and sent to the
	 * server at a fixed rate (expected to be one per frame).
	 * The array queue was deemed optimal for this, as it will not usually
	 * grow very large, but should have the capacity for extra growth.
	 */
	public static Queue pendingCommands = new AQueue();

	/** The set of commands collected from the network.
	 * This includes one command sent from this computer and echoed by the server.
	 * These will be executed this cycle, and replaced before the next cycle.
	 * The number needed for this collection equals to number of players, and will
	 * not increase, so a basic array is adequate.
	 */
	public static Command currentCommands[] = new Command[0];
	
	/** Indicates that the currentCommands contains the Command objects for
	 * the next cycle.
	 */
	public static boolean commandsUpToDate = false;

	public static Command empty = new Command(0,0,new int[0],0,0);
	
	/** Create the command.
	 * @param a action code.
	 * @param pl player number.
	 * @param sel array of indices for the selected game objects.
	 * @param p1 parameter 1 for the command.
	 * @param p2 parameter 2 for the command.
	 */
	public Command(int a, int pl, int[] sel, int p1, int p2) {
		action = a;
		player = pl;
		if(sel == null)
			selection = null;
		else{
			selection = new int[sel.length];
			for(int i = 0; i < sel.length; i++)
				selection[i] = sel[i];
		}
		param1 = p1;
		param2 = p2;
	}
	/** Execute the command. Makes the command take effect.
	 * What the command will do depends primarily on the action code.
	 * This should be called once per Command, then the Command should be discarded.
	 * As a precaution, the action will be set to 0 after executing the action.
	 */
	public void execute() {
		int i; //loop variable
		GameObject go1, go2; //References obtained with IDs
		switch(action) {
			case 0:
			return;
			case 1:
				//Make a new object
			    //This should call the correct player's base.
                            Base base = Player.getPlayer(player).getBase();
                            if(base != null)
                                base.spawnUnit(param1, player);
			break;
			case 2:
				//setGoal(param1,param2);
				for(i = 0; i < selection.length; i++)
				{
					go1 = GameObject.getObjectWithID(selection[i]);
					if(go1 != null)
						go1.setGoal(param1, param2);
				}
			break;
			case 3:
				//setGoal((GameObject)param1);
				go2 = GameObject.getObjectWithID(param1);
				if(go2 != null) {
					for(i = 0; i < selection.length; i++)
					{
						go1 = GameObject.getObjectWithID(selection[i]);
						if(go1 != null) {
							if(go1 == go2)
								go1.setGoal();
							else
								go1.setGoal(go2);
						}
					}
				}else{
					for(i = 0; i < selection.length; i++)
					{
						go1 = GameObject.getObjectWithID(selection[i]);
						if(go1 != null)
							go1.setGoal();
					}
				}
			break;
		}
		action = 0;
	}
}

⌨️ 快捷键说明

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