- 一个 Gun 基类,有攻击力 ap 字段和开火 Fire() 方法,DesertEagle 和 AK47 是它的子类
- 一个 Player 玩家类,有装备 weapon 字段,有换枪,和开枪方法
- 在游戏中,可以通过按键盘 1 装备 AK47,按 2 装备 DesertEagle,换装备时,应该在控制台输出;
- 在游戏中,可以通过鼠标左键开枪,并在控制台输出是什么枪进行的射击,伤害是多少
枪类:父类
public abstract class Guns
{
private int _ap;
public int Ap { get => _ap; set => _ap = value; }
private string _name;
public string Name { get => _name; set => _name = value; }
private int shengyuzidan;
public int Shengyuzidan { get => shengyuzidan; set => shengyuzidan = value; }
public Guns()
{
}
public Guns(string _name, int _ap, int shengyuzidan)
{
this._name = _name;
this._ap = _ap;
this.Shengyuzidan = shengyuzidan;
}
public abstract void Fire();
}
子类:
internal class AK : Guns
{
public AK(string _name, int _ap, int shengyuzidan)
{
this.Name = _name;
this.Ap = _ap;
this.Shengyuzidan = shengyuzidan;
}
public override void Fire()
{
this.Shengyuzidan -= 1;
Debug.Log($"AK开枪了,你的弹匣剩余为{this.Shengyuzidan}\n+" +
$"你的手臂发麻并且对面对造成了{this.Ap}");
}
}
玩家类:
public class Player
{
private int hp;
private int level;
private int maxhp;
private Guns weapon;
public int Hp { get => hp; set => hp = value; }
public int Level { get => level; set => level = value; }
public int Maxhp { get => maxhp; set => maxhp = value; }
internal Guns Weapon { get => weapon; set => weapon = value; }
public Player(int hp, int level, int maxhp)
{
Hp = hp;
Level = level;
Maxhp = maxhp;
}
public void ChangeGuns(Guns gun)
{
this.weapon = gun;
Debug.Log($"换上了{gun.Name}");
}
public void Shoot()
{
weapon.Fire();
}
}
管理类:
public class playercontroll : MonoBehaviour
{
private Player player;
private Guns gun1;
private Guns gun2;
// Start is called before the first frame update
public void initPlayer()
{
player = new Player(10, 1, 10);
player.Weapon = gun1;
Debug.Log($"当前武器为{player.Weapon.Name}");
}
void Start()
{
gun1 =new DesertEagle("沙鹰",75,6);
gun2 = new AK("AK", 45, 30);
this.initPlayer();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
player.Weapon = gun1;
Debug.Log($"当前武器为{player.Weapon.Name}");
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
player.Weapon = gun2;
Debug.Log($"当前武器为{player.Weapon.Name}");
}
if (Input.GetKeyDown(KeyCode.K))
{
player.Shoot();
}
}