?? player.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 java.awt.Color;
import netwar.utils.*;
import netwar.utils.vectorgraphics.ColorSpectrum;
/** Class representing a single Player's data.
* This is in-game data including a reference to the Base,
* as well as data taken from the PlayerSettings.
* @see netwar.settings.PlayerSettings
* @author Group N2 - Project Netwar
* @author Daniel Grund
*/
public class Player {
private Color teamColor;
private ColorSpectrum teamCSfade;
private ColorSpectrum darkCSfade;
private ColorSpectrum lightCSfade;
private int number;
private Base TeamBase;
private int cash = 0;
private static int localPNum = 0;
private static Player players[] = new Player[0];
private Vector myGameObjects = new LVector();
private int gameObjectsCount = 0;
/** Checks if there are enough players to continue the game.
* @return true if there are one or zero players with bases.
*/
public static boolean EndGame() {
int survivors = 0;
for(int i = 0; i < players.length; i++) {
if(!players[i].baseDead())
survivors++;
}
return (survivors < 2);
}
/** Checks if this player's base is dead
* @return true if the base is dead.
*/
public boolean baseDead() {
if(TeamBase == null)
return true;
if(TeamBase.isDead()) {
TeamBase = null;
return true;
}
return false;
}
/** Accessor for TeamBase
* @return the Base for this player, if it is still alive.
*/
public Base getBase() {
return TeamBase;
}
/** Returns the local player
* @return the local player.
*/
public static Player getLocal() {
return players[localPNum-1];
}
/**Returns the player with number pNum
* @param pNum the number of the desired player
* @return the player
*/
public static Player getPlayer(int pNum) {
return players[pNum-1];
}
/** Returns this player's number.
* Used in network communications.
* @return the player's number.
*/
public int getNumber() {
return number;
}
/** Returns the team color
* @return A java.awt.Color
*/
public Color getColor() {
return teamColor;
}
/** Provide a ColorSpectrum for fading units. (Such as the BeamSniper)
* Return the ColorSpectrum with 100 shades varying from mostly transparant team color / 2 to opaque team color.
* @return a ColorSpectrum
*/
public ColorSpectrum getTeamFadingColorSpectrum() {
if(teamCSfade == null)
teamCSfade = new ColorSpectrum( teamColor.getRed() / 2, teamColor.getRed(),
teamColor.getGreen() / 2, teamColor.getGreen(),
teamColor.getBlue() / 2, teamColor.getBlue(),
32, 255, 100);
return teamCSfade;
}
/** Provide a ColorSpectrum for fading units. (Such as the BeamSniper)
* Return the ColorSpectrum with 100 shades varying from mostly transparant team color to opaque team color * 2.
* @return a ColorSpectrum
*/
public ColorSpectrum getTeamFadingBrightColorSpectrum() {
if(lightCSfade == null)
lightCSfade = new ColorSpectrum( teamColor.getRed(), Math.min(teamColor.getRed() * 2 , 255),
teamColor.getGreen(), Math.min(teamColor.getGreen() * 2 , 255),
teamColor.getBlue(), Math.min(teamColor.getBlue() * 2 , 255),
32, 255, 100);
return lightCSfade;
}
/** Provide a ColorSpectrum for fading units. (Such as the BeamSniper)
* Return the ColorSpectrum with 100 shades varying from mostly transparant team color / 4 to opaque team color / 2.
* @return a ColorSpectrum
*/
public ColorSpectrum getTeamFadingDarkColorSpectrum() {
if(darkCSfade == null)
darkCSfade = new ColorSpectrum( teamColor.getRed() / 4, teamColor.getRed() / 2,
teamColor.getGreen() / 4, teamColor.getGreen() / 2,
teamColor.getBlue() / 4, teamColor.getBlue() / 2,
32, 255, 100);
return darkCSfade;
}
/** Creates a new instance of Player.
* Should only be called by startGame, in player number order
*/
public Player(int pNumber, int baseX, int baseY, Color tColor, boolean isLocal) {
Assert.notFalse(pNumber == players.length + 1, "Player initialized out of order.");
Player p[] = new Player[pNumber];
for(int i = 0; i < pNumber - 1; i++)
p[i] = players[i];
p[pNumber-1] = this;
players = p;
number = pNumber;
if(isLocal)
localPNum = pNumber;
teamColor = tColor;
if(netwar.Mapper.theApp == null) {
TeamBase = new Base(baseX, baseY);
GameObject.newGameObject(TeamBase, baseX, baseY, 0, this);
}
}
/** Accessor to acquire the amount of money a player has.
* @return this player's cash count.
*/
public int getCash() {
return cash;
}
/** Mutator to alter amount of money a player has.
* Cash may only be altered by adding a number (positive or negative).
* Cash may not become negative.
* @param addend The number to add to cash, which may be negative.
*/
public void addToCash(int addend) {
Assert.notFalse(cash + addend >= 0, "Negative cash!");
cash += addend;
}
/** Add a new GameObject to this player's list.
* @param go The new GameObject.
*/
public void newObject(GameObject go) {
myGameObjects.append(go);
gameObjectsCount++;
}
/** Remove an old GameObject from this player's list.
* @param go The old GameObject.
*/
public void remObject(GameObject go) {
myGameObjects.goFirst();
while(myGameObjects.isInList()) {
if(myGameObjects.get() == go) {
myGameObjects.remove();
gameObjectsCount--;
} else
myGameObjects.goNext();
}
}
/** Get first game object in this player's list.
* @return the first GameObject.
*/
public GameObject getFirstObject() {
myGameObjects.goFirst();
return (GameObject) myGameObjects.get();
}
/** Get next game object in this player's list.
* Note that a call to remObject will disrupt the sequence.
* Do not allow it to be called between a call to getFirstObject
* and a call to getNextObject!
* @return the next GameObject in the list.
*/
public GameObject getNextObject() {
myGameObjects.goNext();
return (GameObject) myGameObjects.get();
}
/** Return the number of objects in this player's list.
* @return the count.
*/
public int numObject() { return gameObjectsCount; }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -