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

📄 gameworldbase.java

📁 Java mulitplayer strategy game. Adaptation of KDE Galaxy Conquest. (rules are changed - but still th
💻 JAVA
字号:
package net.sf.jawp.gf.domain;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import net.sf.jawp.gf.api.UIDGenerator;
import net.sf.jawp.gf.api.domain.GameWorld;
import net.sf.jawp.gf.api.domain.Player;
import net.sf.jawp.gf.system.SystemContext;
import net.sf.jawp.util.NumUtils;



/**
 * this is a world where players compete each world is unique -> game events
 * that happen in one world do not affect other worlds
 * 
 * @author jarek
 * @param <GAMEVIEW> read only access
 */
public abstract class GameWorldBase<GAMEVIEW extends GameWorldRO> extends GameWorld
implements GameWorldRO, SystemContext
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private final UIDGenerator uidGenerator;
	
	private final NumUtils mathContext;
	
	private final Map<Long, PlayerDO> players; 
	
	public GameWorldBase(final long k, final String name, final SystemContext context)
	{
		super(k, name);
		this.players = new HashMap<Long, PlayerDO>();
		this.mathContext = context.getNumericalContext();
		this.uidGenerator = context.getUIDGenerator();
	}
	
	public abstract GAMEVIEW getUnmodifiable();
	
	//public abstract GAMEVIEW cloneUnmodifiable();
	
	public final GameWorld cloneGameWorld()
	{
		final GameWorld result = new GameWorld( getKey(),  getName());
		return result;
	}
	
	public final Collection<Player> getPlayersForUser(final long userKey)
	{
		final Collection<Player> result = new LinkedList<Player>();
		Collection<PlayerDO> values; 
		synchronized (this.players)
		{
			values = players.values();
		}
		for ( final PlayerDO player : values)
		{
			if ( player.getUser().getKey() == userKey)
			{
				result.add( new Player(player) );
			}
		}
		return result;
	}
	
	public final void addPlayer( final PlayerDO player, final SystemContext system)
	{
		assert player.getGameKey() == this.getKey();
		synchronized (this.players)
		{
			this.players.put (player.getKey(), player);
		}
		playerAddedEvent( player, system);
	}
	
	/**
	 * override this one for own games
	 * @param player
	 */
	protected void playerAddedEvent(final PlayerDO player, final SystemContext system)
	{
		
	}
	
	public Player findPlayer(final long playerKey)
	{
		synchronized (this.players)
		{
			return this.players.get(playerKey);
		}
	}
	
	/**
	 * override to implement micro-turn (generation) for game
	 * 
	 */
	public void doStep(final long timeSlice)
	{
		
	}

	/**
	 * {@inheritDoc}
	 */
	public final NumUtils getNumericalContext()
	{
		return this.mathContext;
	}

	/**
	 * {@inheritDoc}
	 */
	public final UIDGenerator getUIDGenerator()
	{
		return this.uidGenerator;
	}
}

⌨️ 快捷键说明

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