Показать сообщение отдельно
Старый 01.03.2011, 00:22
wvxvw вне форума Посмотреть профиль Отправить личное сообщение для wvxvw Найти все сообщения от wvxvw
  № 5  
Ответить с цитированием
wvxvw
Modus ponens
 
Аватар для wvxvw

модератор форума
Регистрация: Jul 2006
Адрес: #1=(list #1#)
Сообщений: 8,049
Записей в блоге: 38
Я могу попробовать в двух словах описать общую стратегию...
Итак, для начала нужно понять, что функционал можно делегировать стейтам. Т.е. у нас есть в общем 4 стейта, каждый из них работает тогда когда есть определенное количество игроков / учасников чата. У каждого стейта есть метод setTailPosition унаследованый от общего предка. И тогда все вместе это выглядит как-то так:
Код AS3:
class State
{
	protected var _chatBubble:ChatBubbleIngame;
	protected var _jumpTable:Vector.<Vector.<State>>;
 
	public function State(chatBubble:ChatBubbleIngame)
	{
		super();
		this._chatBubble = chatBubble;
	}
 
	public function setTailPosition():void { }
 
	public function transition(seat:int, maxPlayers:int):void
	{
		this._chatBubble.currentState = this.getPlayersMap(maxPlayers)[seat];
	}
 
	private function getPlayersMap(maxPlayers:int):Vector.<State>
	{
		if (!this._jumpTable) this.createJumptable();
		return this._jumpTable[int(maxPlayers == 6)];
	}
 
	// NOTE: this part would normally come from settings.
	// This one is here for illustration purposes only.
	private function createJumptable():void
	{
		this._jumpTable = new <Vector.<State>>[
		new <State>[
			this._chatBubble.state2, 
			this._chatBubble.state3,
			this._chatBubble.state1,
			this._chatBubble.state1,
			this._chatBubble.state1,
			this._chatBubble.state2,
		],
		new <State>[
			this._chatBubble.state2, 
			this._chatBubble.state3,
			this._chatBubble.state3,
			this._chatBubble.state1,
			this._chatBubble.state1,
			this._chatBubble.state1,
			this._chatBubble.state1,
			this._chatBubble.state2,
			this._chatBubble.state2,
		]];
	}
 
}
Код AS3:
class State1
{
	public function State1(chatBubble:ChatBubbleIngame)
	{
		super(chatBubble);
	}
 
	public override function setTailPosition():void
	{
		this._chatBubble._gfx.x = -2;
		this._chatBubble._gfx.y = -54;
	}
}
Код AS3:
class State2
{
	public function State2(chatBubble:ChatBubbleIngame)
	{
		super(chatBubble);
	}
 
	public override function setTailPosition():void
	{
		this._chatBubble._gfx.x = -5;
		this._chatBubble._gfx.y = 57;
	}
}
Код AS3:
class State3
{
	public function State3(chatBubble:ChatBubbleIngame)
	{
		super(chatBubble);
	}
 
	public override function setTailPosition():void
	{
		this._chatBubble._gfx.x = 2;
		this._chatBubble._gfx.y = -54;
	}
}
Код AS3:
class ChatBubbleIngame
{
	public function set currentState(value:State):void { this._currentState = value; }
 
	public function get state1():State1 { return this._state1; }
 
	public function get state2():State1 { return this._state2; }
 
	public function get state3():State1 { return this._state3; }
 
	private var _state1:State1;
 
	private var _state2:State2;
 
	private var _state3:State3;
 
	private var _currentState:State;
 
	public function ChatBubbleIngame(host:DisplayObjectContainer, seat:int, maxPlayers:int)
	{
		super(host);
		this._state1 = new State1(this);
		this._state2 = new State2(this);
		this._state3 = new State3(this);
		this._currentState = this._state1;
		this._currentState.transition(seat, maxPlayers);
		this.setTailPosition();
	}
 
	public function setTailPosition():void
	{
		this._currentState.setTailPosition();
	}
}
Таким образом мы избегаем условных конструкций и мутабельности, которая, как извесно, причина большинства ошибок в программах.
__________________
Hell is the possibility of sanity


Последний раз редактировалось wvxvw; 01.03.2011 в 00:29.