using System;
using System.Xml.Linq;
///
/// Modela una torre defensiva en un juego tipo TD ("tower defense").
/// https://e...content-available-to-author-only...a.org/wiki/Tower_defense
/// Posición: fila, columna
/// Vida la vida restante
/// Ataque la fuerza de cada ataque
/// Impacto(): recibe un impacto
///
public class Torre {
public Torre()
{
this.Vida = 100;
}
public int Vida { get; private set; } // 0 - 100
public /*required*/ int Fila { get; init; }
public /*required*/ int Columna { get; init; }
public /*required*/ int Ataque { get; set; }
public Tuple Posicion
{
get => new Tuple(Fila, Columna);
/* set
{
this.Fila = value.Item1;
this.Columna = value.Item2;
}*/
}
public void Impacto(int danno)
{
this.Vida -= danno;
}
public override string ToString()
{
return $"Torre: pos:{this.Posicion} atq:{this.Ataque}, vida:{this.Vida}";
}
}
//using System.Xml.Linq;
public class TorreXML
{
public TorreXML(Torre t)
{
this.Torre = t;
}
public XElement ToXML()
{
/*
*/
/*
1005032100
*/
/*
100100
*/
return new XElement("Torre",
new XAttribute("Vida", this.Torre.Vida),
new XAttribute("Ataque", this.Torre.Ataque),
new XAttribute("Fila", this.Torre.Fila),
new XAttribute("Columna", this.Torre.Columna));
}
public void Save(string nf)
{
this.ToXML().Save( nf );
}
public static Torre Load(string nf)
{
return FromXML( XElement.Load( nf ) );
}
public static Torre FromXML(XElement xet)
{
var fila = int.Parse(xet.Attribute("Fila")?.Value ?? "0");
var columna = int.Parse(xet.Attribute("Columna")?.Value ?? "0");
var ataque = int.Parse(xet.Attribute("Ataque")?.Value ?? "0");
return new Torre {
Fila = fila, Columna = columna, Ataque = ataque
};
}
public Torre Torre { get; }
}
public class TestTorre {
static void Main()
{
Action