-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit.hs
More file actions
113 lines (92 loc) · 4.46 KB
/
Unit.hs
File metadata and controls
113 lines (92 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
{-Module : Unit
Description :
Modulo para controle de unidades como criação, e alteração dos atributos durante o jogo;
-}
module Unit
(
Unit(..),
Class(..),
Attributes(..),
showUnit,
createUnit,
setName,
setHP,
setClass,
setAttack,
setDefense,
setSpeed,
getName,
getClass,
getHP,
getMaxHP,
getAttack,
getDefense,
getSpeed,
isDead
) where
type UnitName = String -- Nome da Unidade
type HP = Int -- Pontos de vida atual
type MaxHP = Int -- Pontos de vida Maximo
type Attack = Int -- Pontos de ataque
type Defense = Int -- Pontos de defesa
type Speed = Int -- Pontos de Velocidade
type IsDead = Bool -- Verificador se a unidade está viva
type IsPlayer = Bool -- Verifica se é controlada pelo jogador
data Class = Archer | Warrior | Wizard deriving (Show, Read, Eq) -- Classe da unidade, para determinar habilidade
data Attributes = Attributes (HP, MaxHP, Attack, Defense, Speed) deriving (Show, Read, Eq) -- Representação de todos atributos em uma tupla
data Unit = NullUnity | DeadUnit | Unit (UnitName, Class, Attributes, IsDead, IsPlayer) deriving (Show, Read, Eq) -- Dados da Unidade
showUnit :: Unit -> [Char]
showUnit (Unit (name, clas, Attributes (hp, maxhp, atk, def, spd), dead, player)) = do
(name) ++";"++
(show clas) ++";"++
(show hp) ++";"++
(show maxhp) ++";"++
(show atk) ++";"++
(show def) ++";"++
(show spd) ++";"++
(show dead) ++";"++
(show player)
-- Retorna o nome da Unidade
getName :: Unit -> String
getName (Unit ("", _, _, _, _)) = "O Desconhecido"
getName (Unit (name, Archer, _, _, _)) = name ++ ", o arqueiro"
getName (Unit (name, Warrior, _, _, _)) = name ++ ", o guerreiro"
getName (Unit (name, Wizard, _, _, _)) = name ++ ", o mago"
getClass :: Unit -> Class
getClass (Unit (_, cls, _, _, _)) = cls
setName :: Unit -> String -> Unit
setName (Unit (name, clas, (Attributes (hp, maxHP, atk, def, spd)), dead, player)) newName = (Unit (newName, clas, Attributes (hp, maxHP, atk, def, spd), dead, player))
setClass :: Unit -> Class -> Unit
setClass (Unit (name, clas, (Attributes (hp, maxHP, atk, def, spd)), dead, player)) newClass = (Unit (name, newClass, Attributes (hp, maxHP, atk, def, spd), dead, player))
-- Retorna o HP atual da unidade
getHP :: Unit -> Int
getHP (Unit (_, _, Attributes (hp, _, _, _, _), _, _)) = hp
-- Retorna o HP maximo da unidade
getMaxHP :: Unit -> Int
getMaxHP (Unit (_, _, Attributes (_, maxHP, _, _, _), _, _)) = maxHP
-- Retorna os pontos de ataque atual da unidade
getAttack :: Unit -> Int
getAttack (Unit (_, _, Attributes (_, _, atk, _, _), _, _)) = atk
-- Retorna os pontos de defesa atual da unidade
getDefense :: Unit -> Int
getDefense (Unit (_, _, Attributes (_, _, _, def, _), _, _)) = def
-- Retorna os pontos de velocidade atual da unidade
getSpeed :: Unit -> Int
getSpeed (Unit (_, _, Attributes (_, _, _, _, spd), _, _)) = spd
-- Dado os atributos, uma nova unidade é criada
createUnit :: UnitName -> Class -> HP -> Attack -> Defense -> Speed -> IsPlayer -> Unit
createUnit unitName clas maxHP atk def spd player = (Unit (unitName, clas, (Attributes (maxHP, maxHP, atk, def, spd)), False, player))
-- Dado uma unidade e os pontos de vida, uma nova unidade é retornada com os pontos de vida atualizados
setHP :: Unit -> Int -> Unit
setHP (Unit (name, clas, (Attributes (oldHP, maxHP, atk, def, spd)), dead, player)) newHP = (Unit (name, clas, Attributes (newHP, maxHP, atk, def, spd), dead, player))
-- Dado uma unidade e os pontos de ataque, uma nova unidade é retornada com os pontos de ataque atualizados
setAttack :: Unit -> Int -> Unit
setAttack (Unit ( name, clas, (Attributes (hp,maxHP, oldAtk, def, spd)), dead, player)) newAtk = (Unit ( name, clas, Attributes (hp,maxHP, newAtk, def, spd), dead, player))
-- Dado uma unidade e os pontos de defesa, uma nova unidade é retornada com os pontos de defesa atualizados
setDefense :: Unit -> Int -> Unit
setDefense (Unit ( name, clas, (Attributes (hp,maxHP, atk, oldDef, spd)), dead, player)) newDef = (Unit ( name, clas, Attributes (hp,maxHP, atk, newDef, spd), dead, player))
-- Dado uma unidade e os pontos de velocidade, uma nova unidade é retornada com os pontos de velocidade atualizados
setSpeed :: Unit -> Int -> Unit
setSpeed (Unit (name, clas, (Attributes (hp,maxHP, atk, def, oldSpd)), dead, player)) newSpd = (Unit (name, clas, Attributes (hp,maxHP, atk, def, newSpd), dead, player))
isDead :: Unit -> Bool
isDead unit = if (getHP unit) <= 0 then True else False