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

📄 gameobject.java

📁 用java开发的一个实施策略游戏源码 值得学习一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
	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 netwar.utils.vectorgraphics.*;
import netwar.gui.HexViewer;
import java.awt.*;
import netwar.mapper.*;  // sigh, I didn't want to have to include this -kyle

/** Abstract class to define a GameObject.
 * This defines any object which:
 * <BR> occupies at least one Hex,
 * <BR> has vertical aspects (it isn't flat at z = 0),
 * <BR> (optionally) can be destroyed by weapons,
 * <BR> (optionally) has a weapon.
 * @author Group N2 - Project Netwar
 * @author Daniel Grund
 * @author Kyle Kakligian
 */
public abstract class GameObject {
	/** Vector of GameObjects containing all currently active GameObjects. */
	static protected Vector GameObjects = new LVector();
	/** The ID number to assign to the next GameObject added into the game. */
	static int nextID = 1;
	/** The ID number of this GameObject. A GameObject must have an ID so it can
	 * be referenced by Commands, which may take some time before they are implemented
	 * (so an object may be deleted before then) and will be implemented on all
	 * computers involved in the game.
	 */
	protected int myID;
	/** The Player who owns this game Object. It can be compared for friend-or-foe
	 * recognition, and can be accessed to acquire the team color, and other team
	 * wide facts.
	 */
	protected Player myPlayer;
	/** The head of the linked list which provides the distanceSquared and Point3D offset
	 * from this GameObject to each other GameObject, sorted from closest to farthest.
	 */
	public SelfSortingMutualDistanceSquared firstSSMDS = null;
	
	/** Point3d's for locations within the hex. I.e. 0,0,0 is the floor at hex center.
	 * vr[0] is a Point3d for where the center of the hex is if the GameObject is
	 * currently centered in the hex.
	 */
	protected Point3D vr[];
	
	/** X part of current hex coordinate. */
	protected int x;
	/** Y part of current hex coordinate */
	protected int y;
	
	//Animation sequence and frame
	/** Code number indicating the animation sequence in progress.
	 * <BR> 0 = only passive animations.
	 * <BR> 1 = rotating left.
	 * <BR> 2 = rotating right.
	 * <BR> 3 = moving forward.
	 * <BR> 4 = being created.
	 * <BR> 5 = dying.
	 */
	protected int action = 0;
	/** The number of frames remaining until this animation sequence is completed. */
	protected int frame = 0;
	/** The number of frames remaining until the primary weapon is reloaded. */
	protected int reload = 0;
	
	//Current long-term objectives
	/** Code number indicating the long term goal set by this object.
	 * <BR> 0 = Wait for orders/watch for targets.
	 * <BR> 1 = Go to hex whose coordinates are (goalX, goalY).
	 * <BR> 2 = Pursue GameObject target; try to keep it within followDistance() hexes.
	 */
	protected int mode = 0; //0 = stand still, 1 = go to destination, 2 = pursue target
	/** X part of the hex coord of the long term goal. */
	protected int goalX;
	/** Y part of the hex coord of the long term goal. */
	protected int goalY;
	/** The GameObject which is being pursued and/or shot at by this GameObject. */
	protected GameObject target;
	/** The SSMDS which provides distanceSquared and offset data for the target. */
	protected SelfSortingMutualDistanceSquared targetSSMDS = null;
	
        /** Used for saving maps, this method returns a structure of all the unit's positions.
         */        
        public static GameObjectLocationStruct[] getLocationStruct() {
            GameObjectLocationStruct ret[] = new GameObjectLocationStruct[GameObjects.getLength()];
            int t = 0;
            for(GameObjects.goFirst(); GameObjects.isInList(); GameObjects.goNext()) {
                GameObject go = (GameObject)(GameObjects.get());
                ret[t] = new GameObjectLocationStruct(go.x, go.y, 0);
                t++;
            }
            return ret;
        }
        /** Used for saving maps, this method returns a structure of all the unit's package names.
         */        
        public static String[] getLocationStructStrings() {
            String ret[] = new String[GameObjects.getLength()];
            int t = 0;
            for(GameObjects.goFirst(); GameObjects.isInList(); GameObjects.goNext()) {
                ret[t] = new String(GameObjects.get().getClass().getName());
                t++;
            }
            return ret;
        }
	/** Adds a GameObject into the game at the specified location, on the specified team.
	 * This performs the following initialization processes:
	 * <BR> Assigns a new unique ID number to the GameObject.
	 * <BR> Adds the GameObject into the main Vector of GameObjects.
	 * <BR> Sets the hex coordinates of the GameObject to those passed to this method.
	 * <BR> Calls the param() method of the GameObject, giving it p as the parameter.
	 * This allows an arbitrary initialization to take place, which may require an integer.
	 * <BR> Calls the CreateVectors() method of the GameObject, which allows the 3D location
	 * data to be initialized.
	 * <BR> Integrates this GameObject into the SSMDS distance tracking system.
	 * <BR> Initializes the animation sequence to the first frame of the creation graphics.
	 * <BR> Sets the Player's team to that passed to this method.
	 * <BR> Puts the GameObject in the hex identified by the parameters. It is the calling
	 * method's responsibility to ensure that the hex is a valid location to make this
	 * GameObject.
	 * @param u The GameObject to be incorporated into the game.
	 * @param startX The X part of the hex coordinate to start the object in.
	 * @param startY The Y part of the hex coordinate to start the object in.
	 * @param p The parameter for the arbitrary initialization method param().
	 * @param team The player which owns this object. May be null, for a neutral object.
	 * @return u, the GameObject which was just incorporated.
	 */
	public static GameObject newGameObject(GameObject u, int startX, int startY, int p, Player team) {
		Hex spawnHex = Hex.getHex(startX, startY);
                
		/**/if(nextID == 2147483647) return null;
		//Game will stop making GameObjects if we get too many.
		//This will seem really wierd, but very few people will find this feature.
		//The problem will be that, although many IDs are no longer used, we can't
		//easily change IDs because Commands in the Queue, the array, and in transit
		//will refer to the wrong IDs.
		//A better solution at this point is to go through and readjust ALL IDs,
		//and make the Commands which were already in the Queue adjust to the new
		//IDs, but that is big and long and stuff. Maybe next year.
		
		u.myID = nextID++;
                GameObjects.append(u);
		u.myPlayer = team;
                if(team != null)
                        team.newObject(u);
		u.x = startX;
		u.y = startY;
		u.param(p);
		u.createVectors();
		u.addTo(HexViewer.getHexViewer());
		for(GameObjects.goFirst(); GameObjects.get() != u; GameObjects.goNext())
			SelfSortingMutualDistanceSquared.newSSMDS(u, (GameObject)GameObjects.get());
		u.action = 4; //Making
		u.frame = u.framesToMake();
		spawnHex.enter(u);
		spawnHex.reserve();
		return u;
	}
        /** Same as newGameObject, only for the mapper.
         * @see #newGameObject newFameObject()
         */        
	public static GameObject newGameObjectM(GameObject u, int startX, int startY, int p) {
                HexM spawnHex = HexM.getHex(startX, startY);
		/**/if(nextID == 2147483647) return null;
		//Game will stop making GameObjects if we get too many.
		//This will seem really wierd, but very few people will find this feature.
		//The problem will be that, although many IDs are no longer used, we can't
		//easily change IDs because Commands in the Queue, the array, and in transit
		//will refer to the wrong IDs.
		//A better solution at this point is to go through and readjust ALL IDs,
		//and make the Commands which were already in the Queue adjust to the new
		//IDs, but that is big and long and stuff. Maybe next year.
		
		u.myID = nextID++;
                GameObjects.append(u);
                try {u.myPlayer = new Player(1, 0, 0, Color.red, true);}
		catch(Exception e) {u.myPlayer = Player.getLocal();}
                
		u.x = startX;
		u.y = startY;
                u.param(p);
		u.createVectors();
                u.addTo(HexViewerM.getHexViewer());
		for(GameObjects.goFirst(); GameObjects.get() != u; GameObjects.goNext())
			SelfSortingMutualDistanceSquared.newSSMDS(u, (GameObject)GameObjects.get());
        	u.action = 4; //Making
                u.frame = u.framesToMake();
                spawnHex.enter(u);
		return u;
	}
        /** Same as newGameObject, only for the mapper where the unit is not to be displayed.
         * @see #newGameObject newFameObject()
         */        
	public static GameObject newGameObjectMnohex(GameObject u, int p) {
                if(nextID == 2147483647) return null;
		u.myID = nextID++;
                
                try {u.myPlayer = new Player(1, 0, 0, Color.red, true);}
		catch(Exception e) {u.myPlayer = Player.getLocal();}
                
		u.param(p);
		u.createVectors();
                //u.addTo(HexViewerM.getHexViewer());
		//for(GameObjects.goFirst(); GameObjects.get() != u; GameObjects.goNext())
		//	SelfSortingMutualDistanceSquared.newSSMDS(u, (GameObject)GameObjects.get());
        	u.action = 4; //Making
                u.frame = u.framesToMake();
                return u;
	}
	/** Removes this GameObject from the game.
	 * This involves the following de-integration processes:
	 * <BR> Removes this object from the SSMDS tracking system.
	 * <BR> Removes this object from the GameObjects Vector.
	 * <BR> Removing the object from the Hex reservation system is the
	 * responsibility of the calling method.
	 */
	public void remove() {
		if(firstSSMDS != null)
			firstSSMDS.remove(this);
		GameObjects.goFirst();
		while(GameObjects.get() != this)
			GameObjects.goNext();
		GameObjects.remove();
                if(myPlayer != null)
                        myPlayer.remObject(this);
	}
        /** Sets the position of the GameObject.
         */        
	public void setXY(int X, int Y) {x=X;y=Y;}
	/** Given an ID number, finds the GameObject with that ID.
	 * @param ID The ID number of the GameObject.
	 * @return The GameObject with that ID, or null if none exists with that ID.
	 **/
	public static GameObject getObjectWithID(int ID) {
		GameObject go;
		//First, see if we are already on the Unit or past it.
		//If on the Unit, hurray. If past it, go back to start.
		if(GameObjects.isInList()) {
			go = (GameObject)GameObjects.get();
			if(go.myID == ID)
				return go;
			if(go.myID > ID)
				GameObjects.goFirst();
		}else
			GameObjects.goFirst();
		while(GameObjects.isInList()) {
			go = (GameObject)GameObjects.get();
			if(go.myID == ID) //Found it!
				return go;

⌨️ 快捷键说明

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