/******************************************************************************* * Copyright (c) 2001, 2010 Mathew A. Nelson and Robocode contributors * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://robocode.sourceforge.net/license/epl-v10.html * * Contributors: * Flemming N. Larsen * - Initial implementation * Pavel Savara * - Xml Serialization, refactoring *******************************************************************************/ package net.sf.robocode.battle.snapshot; import java.io.IOException; import net.sf.robocode.battle.peer.ProjectilePeer; import net.sf.robocode.peer.ExecCommands; import net.sf.robocode.serialization.IXmlSerializable; import net.sf.robocode.serialization.SerializableOptions; import net.sf.robocode.serialization.XmlReader; import net.sf.robocode.serialization.XmlWriter; import robocode.control.snapshot.BulletState; import robocode.control.snapshot.IBulletSnapshot; /** * A snapshot of a bullet at a specific time instant in a battle. The snapshot * contains a snapshot of the bullet data at that specific time. * * @author Flemming N. Larsen (original) * @author Pavel Savara (contributor) * @since 1.6.1 */ public final class BulletSnapshot extends ProjectileSnapshot implements java.io.Serializable, IXmlSerializable, IBulletSnapshot { /** * */ private static final long serialVersionUID = 1L; /** * Creates a snapshot of a bullet that must be filled out with data later. */ public BulletSnapshot() { super(); } /** * Creates a snapshot of a bullet. * * @param bullet * the bullet to make a snapshot of. */ public BulletSnapshot(ProjectilePeer proj) { super(proj); } /** * {@inheritDoc} */ public void writeXml(XmlWriter writer, SerializableOptions options) throws IOException { writer.startElement(options.shortAttributes ? "b" : "bullet"); { writer.writeAttribute("id", ownerIndex + "-" + bulletId); if (!options.skipExploded || state != BulletState.MOVING) { writer.writeAttribute(options.shortAttributes ? "s" : "state", state.toString()); writer.writeAttribute(options.shortAttributes ? "p" : "power", power, options.trimPrecision); } if (state == BulletState.HIT_VICTIM) { writer.writeAttribute(options.shortAttributes ? "v" : "victim", victimIndex); } if (state == BulletState.FIRED) { writer.writeAttribute(options.shortAttributes ? "o" : "owner", ownerIndex); writer.writeAttribute( options.shortAttributes ? "h" : "heading", heading, options.trimPrecision); } writer.writeAttribute("x", paintX, options.trimPrecision); writer.writeAttribute("y", paintY, options.trimPrecision); if (!options.skipNames) { if (color != ExecCommands.defaultProjectileColor) { writer.writeAttribute(options.shortAttributes ? "c" : "color", Integer.toHexString(color).toUpperCase()); } } if (!options.skipExploded) { if (frame != 0) { writer.writeAttribute("frame", frame); } if (isExplosion) { writer.writeAttribute("isExplosion", true); writer.writeAttribute("explosion", explosionImageIndex); } } if (!options.skipVersion) { writer.writeAttribute("ver", serialVersionUID); } } writer.endElement(); } /** * {@inheritDoc} */ public XmlReader.Element readXml(XmlReader reader) { return reader.expect("bullet", new XmlReader.Element() { public IXmlSerializable read(XmlReader reader) { final BulletSnapshot snapshot = new BulletSnapshot(); reader.expect("state", new XmlReader.Attribute() { public void read(String value) { snapshot.state = BulletState.valueOf(value); } }); reader.expect("power", new XmlReader.Attribute() { public void read(String value) { snapshot.power = Double.parseDouble(value); } }); reader.expect("x", new XmlReader.Attribute() { public void read(String value) { snapshot.x = Double.parseDouble(value); snapshot.paintX = snapshot.x; } }); reader.expect("y", new XmlReader.Attribute() { public void read(String value) { snapshot.y = Double.parseDouble(value); snapshot.paintY = snapshot.y; } }); reader.expect("color", new XmlReader.Attribute() { public void read(String value) { snapshot.color = Long.valueOf(value.toUpperCase(), 16) .intValue(); } }); reader.expect("isExplosion", new XmlReader.Attribute() { public void read(String value) { snapshot.isExplosion = Boolean.parseBoolean(value); } }); reader.expect("explosion", new XmlReader.Attribute() { public void read(String value) { snapshot.explosionImageIndex = Integer.parseInt(value); } }); reader.expect("frame", new XmlReader.Attribute() { public void read(String value) { snapshot.frame = Integer.parseInt(value); } }); return snapshot; } }); } }