View Javadoc

1   /*
2    * Created on 12 mars 2005
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   * 
21   */
22  package net.sf.magicproject.action;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  
27  import net.sf.magicproject.action.handler.StandardAction;
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.event.DeclaredAttacking;
30  import net.sf.magicproject.event.DeclaredBlocking;
31  import net.sf.magicproject.event.Event;
32  import net.sf.magicproject.event.context.ContextEventListener;
33  import net.sf.magicproject.test.TestOn;
34  
35  /***
36   * Change the normal jump(1) in the current actions chain, to another one . This
37   * jump can be positive or negative. Zero mean infinite loop.
38   * 
39   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
40   * @since 0.82
41   */
42  class GenerateEvent extends UserAction implements StandardAction {
43  
44  	private TestOn blocking;
45  
46  	private TestOn attacking;
47  
48  	/***
49  	 * Create an instance of Hop by reading a file Offset's file must pointing on
50  	 * the first byte of this action
51  	 * <ul>
52  	 * Structure of InputStream : Data[size]
53  	 * <li>idAction [1]</li>
54  	 * <li>event id [1]</li>
55  	 * <li>attacking [TestOn]</li>
56  	 * <li>blocking [TestOn]</li>
57  	 * </ul>
58  	 * 
59  	 * @param inputFile
60  	 *          file containing this action
61  	 * @throws IOException
62  	 *           if error occurred during the reading process from the specified
63  	 *           input stream
64  	 */
65  	GenerateEvent(InputStream inputFile) throws IOException {
66  		super(inputFile);
67  		idEvent = Event.valueOf(inputFile);
68  		switch (idEvent) {
69  		case DECLARED_ATTACKING:
70  			attacking = TestOn.deserialize(inputFile);
71  			break;
72  		case DECLARED_BLOCKING:
73  			attacking = TestOn.deserialize(inputFile);
74  			blocking = TestOn.deserialize(inputFile);
75  			break;
76  		default:
77  			throw new InternalError("\t>>Unsupported event '" + idEvent
78  					+ "' for GenerateEvent action");
79  		}
80  	}
81  
82  	@Override
83  	public final Actiontype getIdAction() {
84  		return Actiontype.GENERATE_EVENT;
85  	}
86  
87  	public boolean play(ContextEventListener context, Ability ability) {
88  		switch (idEvent) {
89  		case DECLARED_ATTACKING:
90  			if (!DeclaredAttacking.tryAction(attacking.getCard(ability, context,
91  					ability.getCard()))) {
92  				return false;
93  			}
94  			DeclaredAttacking.dispatchEvent(attacking.getCard(ability, context,
95  					ability.getCard()));
96  			return true;
97  		case DECLARED_BLOCKING:
98  			if (!DeclaredBlocking.tryAction(blocking.getCard(ability, context,
99  					ability.getCard()), attacking.getCard(ability, context, ability
100 					.getCard()))) {
101 				return false;
102 			}
103 			DeclaredBlocking.dispatchEvent(blocking.getCard(ability, context, ability
104 					.getCard()), attacking.getCard(ability, context, ability.getCard()));
105 			return true;
106 		default:
107 			return true;
108 		}
109 	}
110 
111 	@Override
112 	public String toString(Ability ability) {
113 		return "Generate event";
114 	}
115 
116 	/***
117 	 * The event id to genrate
118 	 */
119 	private Event idEvent;
120 
121 }