-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElementSystem.m
More file actions
280 lines (240 loc) · 8.42 KB
/
Copy pathElementSystem.m
File metadata and controls
280 lines (240 loc) · 8.42 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
function varargout = ElementSystem(action, varargin)
switch lower(action)
case 'hasadvantage'
varargout{1} = hasAdvantage(varargin{1}, varargin{2});
case 'getname'
varargout{1} = getName(varargin{1});
case 'getcolor'
varargout{1} = getColor(varargin{1});
case 'getspriteid'
varargout{1} = getSpriteId(varargin{1});
case 'getstrengths'
varargout{1} = getStrengths(varargin{1});
case 'getweaknesses'
varargout{1} = getWeaknesses(varargin{1});
case 'getdamagemultiplier'
varargout{1} = getDamageMultiplier(varargin{1}, varargin{2});
case 'calculateattack'
[varargout{1}, varargout{2}] = calculateAttack(varargin{1}, varargin{2}, varargin{3});
case 'getrandom'
varargout{1} = randi([1, 4]); % Fire, Earth, Lightning, Wind
case 'getrandomwithneutral'
varargout{1} = randi([1, 5]); % Include Neutral
case 'displaymatchup'
displayMatchup(varargin{1}, varargin{2});
case 'getconstants'
% Return all element constants as a struct
c = struct();
c.FIRE = 1;
c.EARTH = 2;
c.LIGHTNING = 3;
c.WIND = 4;
c.NEUTRAL = 5;
c.NAMES = {'FIRE', 'EARTH', 'LIGHTNING', 'WIND', 'NEUTRAL'};
c.COLORS = {'red', 'brown', 'yellow', 'cyan', 'white'};
varargout{1} = c;
otherwise
error('ElementSystem: Unknown action "%s"', action);
end
end
function advantage = hasAdvantage(attacker, defender)
% HASADVANTAGE - Check if attacker element beats defender element
%
% Inputs:
% attacker - Element ID of attacker (1-5)
% defender - Element ID of defender (1-5)
%
% Output:
% advantage - true if attacker beats defender, false otherwise
%
% Rules:
% FIRE (1) beats everything except Neutral
% EARTH (2) beats LIGHTNING (3)
% LIGHTNING (3) beats WIND (4)
% WIND (4) beats EARTH (2)
% NEUTRAL (5) is impartial - no advantages
% Neutral never has advantage and nothing has advantage over it
if attacker == 5 || defender == 5
advantage = false;
return;
end
% Fire beats everything (except neutral, handled above)
if attacker == 1
advantage = true;
return;
end
% Earth beats Lightning
if attacker == 2 && defender == 3
advantage = true;
return;
end
% Lightning beats Wind
if attacker == 3 && defender == 4
advantage = true;
return;
end
% Wind beats Earth
if attacker == 4 && defender == 2
advantage = true;
return;
end
advantage = false;
end
function name = getName(elementId)
% GETNAME - Get the name string for an element ID
%
% Input:
% elementId - Element ID (1-5)
%
% Output:
% name - String name of element ('FIRE', 'EARTH', etc.)
names = {'FIRE', 'EARTH', 'LIGHTNING', 'WIND', 'NEUTRAL'};
if elementId >= 1 && elementId <= 5
name = names{elementId};
else
name = 'UNKNOWN';
end
end
function color = getColor(elementId)
% GETCOLOR - Get the display color for an element
%
% Input:
% elementId - Element ID (1-5)
%
% Output:
% color - Color string for use in plots/text ('red', 'brown', etc.)
colors = {'red', 'brown', 'yellow', 'cyan', 'white'};
if elementId >= 1 && elementId <= 5
color = colors{elementId};
else
color = 'gray';
end
end
function spriteId = getSpriteId(elementId)
% GETSPRITEID - Get sprite sheet ID for element
%
% Input:
% elementId - Element ID (1-5)
%
% Output:
% spriteId - Sprite ID to use in game engine
%
% Note: Default mapping is 1:1 (element 1 = sprite 1)
% Modify spriteIds array to match your sprite sheet
spriteIds = [1, 2, 3, 4, 5]; % Modify these to match your sprites!
if elementId >= 1 && elementId <= 5
spriteId = spriteIds(elementId);
else
spriteId = 5; % Default to neutral
end
end
function beats = getStrengths(elementId)
% GETSTRENGTHS - Get array of elements this element beats
%
% Input:
% elementId - Element ID (1-5)
%
% Output:
% beats - Array of element IDs that this element is strong against
switch elementId
case 1 % FIRE beats everything except neutral
beats = [2, 3, 4];
case 2 % EARTH beats LIGHTNING
beats = 3;
case 3 % LIGHTNING beats WIND
beats = 4;
case 4 % WIND beats EARTH
beats = 2;
case 5 % NEUTRAL beats nothing
beats = [];
otherwise
beats = [];
end
end
function beatenBy = getWeaknesses(elementId)
% GETWEAKNESSES - Get array of elements that beat this element
%
% Input:
% elementId - Element ID (1-5)
%
% Output:
% beatenBy - Array of element IDs that this element is weak against
switch elementId
case 1 % FIRE - nothing beats fire
beatenBy = [];
case 2 % EARTH - beaten by FIRE, WIND
beatenBy = [1, 4];
case 3 % LIGHTNING - beaten by FIRE, EARTH
beatenBy = [1, 2];
case 4 % WIND - beaten by FIRE, LIGHTNING
beatenBy = [1, 3];
case 5 % NEUTRAL - nothing beats it
beatenBy = [];
otherwise
beatenBy = [];
end
end
function multiplier = getDamageMultiplier(attackerElement, defenderElement)
% GETDAMAGEMULTIPLIER - Get damage multiplier for element matchup
%
% Inputs:
% attackerElement - Element ID of attacker (1-5)
% defenderElement - Element ID of defender (1-5)
%
% Output:
% multiplier - Damage multiplier (1.5 advantage, 0.75 disadvantage, 1.0 neutral)
%
% Note: For Fire attacks, use calculateAttack() instead to get recoil
if hasAdvantage(attackerElement, defenderElement)
multiplier = 1.5; % 50% bonus damage
elseif hasAdvantage(defenderElement, attackerElement)
multiplier = 0.75; % 25% reduced damage
else
multiplier = 1.0; % Neutral matchup
end
end
function [damageDealt, recoilTaken] = calculateAttack(attackerElement, defenderElement, baseDamage)
% CALCULATEATTACK - Calculate damage dealt and recoil taken
%
% Inputs:
% attackerElement - Element ID of attacker (1-5)
% defenderElement - Element ID of defender (1-5)
% baseDamage - Base damage amount before multipliers
%
% Outputs:
% damageDealt - Final damage to deal to defender
% recoilTaken - Damage attacker takes (only Fire has recoil)
%
% Special: Fire does 2x damage but takes 10% recoil
if attackerElement == 1 && defenderElement ~= 5 % Fire attacking non-neutral
damageDealt = floor(baseDamage * 2.0); % 2x damage
recoilTaken = floor(damageDealt * 0.10); % 10% recoil
else
% Normal element calculations
multiplier = getDamageMultiplier(attackerElement, defenderElement);
damageDealt = floor(baseDamage * multiplier);
recoilTaken = 0;
end
end
function displayMatchup(attackerElement, defenderElement)
% DISPLAYMATCHUP - Print element matchup info to console
%
% Inputs:
% attackerElement - Element ID of attacker (1-5)
% defenderElement - Element ID of defender (1-5)
%
% Prints formatted matchup information
attackerName = getName(attackerElement);
defenderName = getName(defenderElement);
if attackerElement == 5 || defenderElement == 5
fprintf('%s vs %s: NEUTRAL - No effect (1.0x damage)\n', attackerName, defenderName);
elseif attackerElement == 1
fprintf('%s vs %s: FIRE ATTACK! (2.0x damage, 10%% recoil)\n', attackerName, defenderName);
elseif hasAdvantage(attackerElement, defenderElement)
fprintf('%s has ADVANTAGE over %s! (1.5x damage)\n', attackerName, defenderName);
elseif hasAdvantage(defenderElement, attackerElement)
fprintf('%s has DISADVANTAGE against %s! (0.75x damage)\n', attackerName, defenderName);
else
fprintf('%s vs %s: Neutral matchup (1.0x damage)\n', attackerName, defenderName);
end
end