1
+ // Required modules and models for the battle system.
1
2
use bytebeasts :: {
2
3
models :: {beast :: Beast , mt :: Mt , player :: Player , battle :: Battle , potion :: Potion },
3
4
};
4
5
6
+ // Interface defining battle-related actions.
5
7
#[dojo:: interface]
6
8
trait IBattleActions {
7
- fn init_battle (ref world : IWorldDispatcher , player_id : u32 , opponent_id : u32 ) -> u32 ;
8
- fn check_flee_success (player_beast : Beast , opponent_beast : Beast ) -> bool ;
9
- fn calculate_damage (mt : Mt , attacker : Beast , defender : Beast ) -> u32 ;
10
- fn opponent_turn (ref world : IWorldDispatcher , battle_id : u32 );
11
- fn attack (ref world : IWorldDispatcher , battle_id : u32 , mt_id : u32 );
12
- fn use_potion (ref world : IWorldDispatcher , battle_id : u32 , potion_id : u32 );
9
+ // Initializes a battle
10
+ fn init_battle (ref world : IWorldDispatcher , player_id : u32 , opponent_id : u32 ) -> u32 ;
11
+ // Checks flee success
12
+ fn check_flee_success (player_beast : Beast , opponent_beast : Beast ) -> bool ;
13
+ // Calculates damage
14
+ fn calculate_damage (mt : Mt , attacker : Beast , defender : Beast ) -> u32 ;
15
+ // Handles opponent's turn
16
+ fn opponent_turn (ref world : IWorldDispatcher , battle_id : u32 );
17
+ // Executes an attack
18
+ fn attack (ref world : IWorldDispatcher , battle_id : u32 , mt_id : u32 );
19
+ // Uses a potion
20
+ fn use_potion (ref world : IWorldDispatcher , battle_id : u32 , potion_id : u32 );
21
+ // Attempts to flee
13
22
fn flee (ref world : IWorldDispatcher , battle_id : u32 );
14
23
}
15
24
25
+ // Contract implementing battle actions.
16
26
#[dojo:: contract]
17
27
mod battle_system {
18
28
use super :: {IBattleActions };
19
29
use bytebeasts :: {
20
30
models :: {beast :: Beast , mt :: Mt , player :: Player , battle :: Battle , potion :: Potion },
21
31
};
22
32
33
+ // Event emitted for battle status updates.
23
34
#[derive(Copy , Drop , Serde )]
24
35
#[dojo:: model]
25
36
#[dojo:: event]
26
37
struct StatusBattle {
27
38
#[key]
28
- battle_id : u32 ,
29
- message : felt252 ,
39
+ battle_id : u32 , // Battle identifier
40
+ message : felt252 , // Event message
30
41
}
31
42
43
+ // Event emitted for player status updates.
32
44
#[derive(Copy , Drop , Serde )]
33
45
#[dojo:: model]
34
46
#[dojo:: event]
35
47
struct Status {
36
48
#[key]
37
- player_id : u32 ,
38
- message : felt252 ,
49
+ player_id : u32 , // Player identifier
50
+ message : felt252 , // Event message
39
51
}
40
52
41
53
#[abi(embed_v0)]
42
54
impl BattleActionsImpl of IBattleActions <ContractState > {
55
+ // Initializes a new battle.
43
56
fn init_battle (ref world : IWorldDispatcher , player_id : u32 , opponent_id : u32 ) -> u32 {
44
- let player = get! (world , player_id , (Player ));
45
- let opponent = get! (world , opponent_id , (Player ));
46
- let active_beast_player = get! (world , player . beast_1, (Beast ));
47
- let active_beast_opponent = get! (world , opponent . beast_1, (Beast ));
57
+ let player = get! (world , player_id , (Player )); // Fetch player data
58
+ let opponent = get! (world , opponent_id , (Player )); // Fetch opponent data
59
+ let active_beast_player = get! (world , player . beast_1, (Beast )); // Player's beast
60
+ let active_beast_opponent = get! (world , opponent . beast_1, (Beast )); // Opponent's beast
48
61
49
62
let battle_created_id = 1 ; // Hardcoded for now
50
63
set! (
@@ -61,17 +74,19 @@ mod battle_system {
61
74
);
62
75
63
76
let message = ' Battle started' ;
64
- emit! (world , (Status { player_id : player_id , message : message }));
77
+ emit! (world , (Status { player_id : player_id , message : message })); // Emit player status
65
78
66
- emit! (world , (StatusBattle { battle_id : battle_created_id , message : message }));
79
+ emit! (world , (StatusBattle { battle_id : battle_created_id , message : message })); // Emit battle status
67
80
68
- return battle_created_id ;
81
+ return battle_created_id ; // Return battle ID
69
82
}
70
83
84
+ // Checks if the player can flee based on beast levels.
71
85
fn check_flee_success (player_beast : Beast , opponent_beast : Beast ) -> bool {
72
86
player_beast . level > opponent_beast . level
73
87
}
74
88
89
+ // Calculates the damage dealt by an attacker to a defender.
75
90
fn calculate_damage (mt : Mt , attacker : Beast , defender : Beast ) -> u32 {
76
91
let base_damage = mt . mt_power * attacker . attack / defender . defense;
77
92
@@ -83,98 +98,119 @@ mod battle_system {
83
98
84
99
let hit_chance = 80_u32 ; // Hardcoded for now
85
100
if hit_chance > mt . mt_accuracy {
86
- return 0_u32 ;
101
+ return 0_u32 ; // Misses if hit chance is greater than accuracy
87
102
}
88
103
89
104
effective_damage
90
105
}
91
106
107
+ // Executes the opponent's turn in the battle.
92
108
fn opponent_turn (ref world : IWorldDispatcher , battle_id : u32 ) {
93
- let mut battle = get! (world , battle_id , (Battle ));
109
+ let mut battle = get! (world , battle_id , (Battle )); // Retrieve battle details
94
110
95
111
let mut player_beast = get! (world , battle . active_beast_player, (Beast ));
96
112
let mut opponent_beast = get! (world , battle . active_beast_opponent, (Beast ));
97
- let opponent_attack = get! (world , opponent_beast . mt1, (Mt ));
113
+ let opponent_attack = get! (world , opponent_beast . mt1, (Mt )); // Opponent's chosen move
98
114
115
+ // Calculate damage dealt by the opponent to the player
99
116
let damage = self . calculate_damage (opponent_attack , opponent_beast , player_beast );
100
117
if damage >= player_beast . current_hp {
101
- player_beast . current_hp = 0 ;
118
+ player_beast . current_hp = 0 ; // Knock out player’s beast if damage exceeds current HP
102
119
} else {
103
- player_beast . current_hp -= damage ;
120
+ player_beast . current_hp -= damage ; // Subtract damage from player's beast HP
104
121
}
105
- set! (world , (player_beast ));
122
+ set! (world , (player_beast )); // Update player's beast in the world
106
123
124
+ // Check if the player's beast is knocked out
107
125
if player_beast . current_hp <= 0_u32 {
108
126
let message = ' Player Beast Knocked Out!' ;
109
- emit! (world , (StatusBattle { battle_id , message }));
110
- battle . battle_active = 0 ;
111
- set! (world , (battle ));
127
+ emit! (world , (StatusBattle { battle_id , message })); // Emit battle status update
128
+ battle . battle_active = 0 ; // Emit battle status update
129
+ set! (world , (battle )); // Update battle status in the world
112
130
}
113
131
}
114
132
133
+ // Executes the player's attack in the battle.
115
134
fn attack (ref world : IWorldDispatcher , battle_id : u32 , mt_id : u32 ) {
116
- let mut battle = get! (world , battle_id , (Battle ));
135
+ let mut battle = get! (world , battle_id , (Battle )); // Retrieve battle details
117
136
137
+ // Fetch the player's and opponent's active beasts and the chosen move (mt) of the player
118
138
let player_beast = get! (world , battle . active_beast_player, (Beast ));
119
139
let mut opponent_beast = get! (world , battle . active_beast_opponent, (Beast ));
120
140
let mt = get! (world , mt_id , (Mt ));
121
141
142
+ // Calculate damage dealt by the player to the opponent
122
143
let damage = self . calculate_damage (mt , player_beast , opponent_beast );
123
144
145
+ // Apply damage to opponent's beast HP
124
146
if damage >= opponent_beast . current_hp {
125
- opponent_beast . current_hp = 0 ;
147
+ opponent_beast . current_hp = 0 ; // Knock out opponent’s beast if damage exceeds current HP
126
148
} else {
127
- opponent_beast . current_hp -= damage ;
149
+ opponent_beast . current_hp -= damage ; // Subtract damage from opponent's beast HP
128
150
}
129
- set! (world , (opponent_beast ));
151
+ set! (world , (opponent_beast )); // Update opponent's beast in the world
130
152
153
+ // Check if the opponent's beast is knocked out
131
154
if opponent_beast . current_hp <= 0_u32 {
132
155
let message = ' Opponent Beast Knocked Out!' ;
133
- emit! (world , (StatusBattle { battle_id , message }));
134
- battle . battle_active = 0 ;
135
- set! (world , (battle ));
156
+ emit! (world , (StatusBattle { battle_id , message })); // Emit battle status update
157
+ battle . battle_active = 0 ; // End the battle
158
+ set! (world , (battle )); // Update battle status in the world
136
159
} else {
137
160
let message = ' Attack Performed!' ;
138
- emit! (world , (StatusBattle { battle_id , message }));
161
+ emit! (world , (StatusBattle { battle_id , message })); // Emit message indicating the attack was performed
139
162
}
163
+
140
164
}
141
165
166
+ // Allows the player to use a potion on their beast to restore health.
142
167
fn use_potion (ref world : IWorldDispatcher , battle_id : u32 , potion_id : u32 ) {
143
- let mut battle = get! (world , battle_id , (Battle ));
168
+ let mut battle = get! (world , battle_id , (Battle )); // Retrieve battle details
144
169
170
+ // Fetch the player's active beast and the potion being used
145
171
let mut player_beast = get! (world , battle . active_beast_player, (Beast ));
146
172
let potion = get! (world , potion_id , (Potion ));
147
173
174
+ // Restore health points based on potion's effect without exceeding max HP
148
175
if potion . potion_effect <= player_beast . current_hp {
149
- player_beast . current_hp += potion . potion_effect;
176
+ player_beast . current_hp += potion . potion_effect; // Add potion effect to current HP
150
177
}
151
178
else {
152
- player_beast . current_hp = player_beast . hp;
179
+ player_beast . current_hp = player_beast . hp; // Cap HP to the beast's max health
153
180
}
154
181
155
- set! (world , (player_beast ));
182
+ set! (world , (player_beast )); // Update the player's beast in the world
156
183
player_beast = get! (world , battle . active_beast_player, (Beast ));
157
184
185
+ // Emit an event to notify that the potion has been used
158
186
let message = ' Item Used!' ;
159
187
emit! (world , (StatusBattle { battle_id , message }));
160
188
}
161
189
190
+ // Attempts to flee the battle. Success is determined by the relative levels of the player and opponent beasts.
162
191
fn flee (ref world : IWorldDispatcher , battle_id : u32 ) {
163
- let mut battle = get! (world , battle_id , (Battle ));
192
+ let mut battle = get! (world , battle_id , (Battle )); // Retrieve battle details
164
193
194
+ // Fetch both player's and opponent's active beasts
165
195
let player_beast = get! (world , battle . active_beast_player, (Beast ));
166
196
let opponent_beast = get! (world , battle . active_beast_opponent, (Beast ));
167
197
198
+ // Determine if flee attempt is successful based on beast levels
168
199
let flee_success = self . check_flee_success (player_beast , opponent_beast );
169
200
if flee_success {
170
- battle . battle_active = 0 ;
171
- set! (world , (battle ));
201
+ battle . battle_active = 0 ; // Set battle as inactive upon successful flee
202
+ set! (world , (battle )); // Update battle status in the world
203
+
204
+ // Emit event indicating the player successfully fled the battle
172
205
let message = ' Player Fled!' ;
173
206
emit! (world , (StatusBattle { battle_id , message }));
174
207
} else {
208
+ // Emit event indicating flee attempt failed
175
209
let message = ' Flee failed!' ;
176
210
emit! (world , (StatusBattle { battle_id , message }));
177
211
}
212
+
178
213
}
179
214
}
180
215
}
216
+
0 commit comments