View Javadoc

1   /*
2    * Created on Nov 3, 2004 
3    * Original filename was DetachmentAbility.java
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.clickable.ability;
23  
24  import net.sf.magicproject.action.DetachMe;
25  import net.sf.magicproject.action.MAction;
26  import net.sf.magicproject.action.target.SingletonTarget;
27  import net.sf.magicproject.clickable.targetable.card.MCard;
28  import net.sf.magicproject.event.MEventListener;
29  import net.sf.magicproject.event.context.ContextEventListener;
30  import net.sf.magicproject.test.Test;
31  import net.sf.magicproject.test.TestOn;
32  import net.sf.magicproject.token.TrueFalseAuto;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   */
37  public class DetachmentAbility extends AbstractAbility {
38  
39  	/***
40  	 * Creates a new instance of DetachmentAbility <br>
41  	 * 
42  	 * @param event
43  	 *          the event attached to this ability.
44  	 * @param from
45  	 *          the card is to going to be attached.
46  	 * @param to
47  	 *          the card is to going to be attached to.
48  	 * @param attachmentTest
49  	 *          the attachement condition.
50  	 */
51  	public DetachmentAbility(MEventListener event, MCard from, MCard to,
52  			Test attachmentTest) {
53  		super("detach myself", event, null, null, TrueFalseAuto.AUTO);
54  		actionList = new MAction[] { SingletonTarget
55  				.getInstance(TestOn.CURRENT_CARD) };
56  		effectList = new MAction[] { DetachMe.getInstance() };
57  		this.from = from;
58  		this.to = to;
59  		this.attachmentTest = attachmentTest;
60  
61  		// override the resolver
62  		priority = Priority.hidden_high;
63  		// override the optimizer
64  		optimizer = Optimization.follow;
65  	}
66  
67  	@Override
68  	public MCard getCard() {
69  		return eventComing.card;
70  	}
71  
72  	@Override
73  	public Ability clone(MCard container) {
74  		return new DetachmentAbility(eventComing.clone(container), container, to,
75  				attachmentTest);
76  	}
77  
78  	@Override
79  	public boolean triggerIt(ContextEventListener context) {
80  		super.triggerIt(context);
81  		eventComing.card.controller.zoneManager.triggeredBuffer.addHidden(this,
82  				context);
83  		return true;
84  	}
85  
86  	@Override
87  	public String getLog(ContextEventListener context) {
88  		return "detachment of " + eventComing.card;
89  	}
90  
91  	@Override
92  	public String getAbilityTitle() {
93  		return "DetachedAbility" + (getName() != null ? ": " + getName() : "")
94  				+ super.getAbilityTitle();
95  	}
96  
97  	/***
98  	 * Returns the attached card.
99  	 * 
100 	 * @return the attached card.
101 	 */
102 	public MCard getAttachedCard() {
103 		return from;
104 	}
105 
106 	/***
107 	 * The attached card.
108 	 */
109 	private final MCard from;
110 
111 	private final MCard to;
112 
113 	private final Test attachmentTest;
114 }