-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameMPG.m
More file actions
197 lines (164 loc) · 6.06 KB
/
gameMPG.m
File metadata and controls
197 lines (164 loc) · 6.06 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
function gameMPG()
version = '1.1';
% Main figure for game
player = [];
player.name = [];
player.colorfill = [];
player.colorborder = [];
player.hp = []; % Ranges from 0 (Dead) - 100 (Healthy)
player.stma = []; % Ranges form 0 (Exhusted) - 30 (Energized)
player.xp = []; % Currency within the game
player.pos = []; % Player position on the map
player.speed = [];
game = [];
game.time = []; % Time since beginning of game
game.fps = []; % Framerate of figure
game.running = []; % Terminates the run loop when zero
game.mapx = []; % Width of the map
game.mapy = []; % Height of the map
ent(1) = struct('type','init','posx',0,'posy',0,'hp',0); % All world entities are stored in this var
disp('----------------------------------');
fprintf('Matlab Roleplaying Game (ver %s)\n',version)
disp('©2017 Wyatt Phillips');
disp('----------------------------------');
% Load save if present
if exist('game.mat', 'file')
load('game.mat');
else
player.name = input('Please enter a nickname: ','s');
disp('Enter a character color in the following format');
disp('[x,x,x] - Red(0-1) Green(0-1) Blue(0-1)');
disp('Red >> [1,0,0]');
disp('Blue >> [0,0,1]');
disp('Yellow >> [1,1,0]');
player.colorfill = input('Fill Color: '); % -! Add check for valid matrix.
player.colorborder = input('Border Color: '); % -! Add check for valid matrix.
% Generating Game
game.mapx = 128;
game.mapy = 128;
% Create Claracter
player.hp = 100;
player.stma = 30;
player.xp = 0;
player.pos = [70,25];
player.speed = 2;
%Generate Starting Entities
for i = 1:5
for j = 1:4
ent = spawnCoin20(32+(16*i),110-(16*j),ent);
end
end
ent = spawnCoin50(105,110,ent);
end
run(game,player,ent,version);
end
function run(game,player,ent,version)
def_game = game;
def_player = player;
def_ent = ent;
text = strcat(['MATLAB RPG (ver ',version,')']);
win = figure('Name',text);
game.running = 1;
% Run Main Loop
[win,game,player,ent] = render(win,game,player,ent);
while game.running == 1
% Key Watchdog
waitforbuttonpress;
button = get(win,'CurrentCharacter');
if strcmp(button,'w')
player.pos(2) = player.pos(2) + player.speed;
[player,ent] = checkCollisions(player,ent);
[win,game,player,ent] = render(win,game,player,ent);
end
if strcmp(button,'a')
player.pos(1) = player.pos(1) - player.speed;
[player,ent] = checkCollisions(player,ent);
[win,game,player,ent] = render(win,game,player,ent);
end
if strcmp(button,'s')
player.pos(2) = player.pos(2) - player.speed;
[player,ent] = checkCollisions(player,ent);
[win,game,player,ent] = render(win,game,player,ent);
end
if strcmp(button,'d')
player.pos(1) = player.pos(1) + player.speed;
[player,ent] = checkCollisions(player,ent);
[win,game,player,ent] = render(win,game,player,ent);
end
if strcmp(button,'c')
disp("Saving Preset...");
gameSave(def_game,def_player,def_ent);
end
if strcmp(button,'v')
disp("Saving Game...");
gameSave(game,player,ent);
end
if strcmp(button,'k')
disp("Save Detroyed...");
delete('game.mat')
end
end
cleanup = onCleanup(@() gameShutdown(game,player,ent,win));
end
function [ent] = spawnCoin20(x,y,ent)
ent;
ent(end+1) = struct('type','coin20','posx',x,'posy',y,'hp',100);
end
function [ent] = spawnCoin50(x,y,ent)
ent;
ent(end+1) = struct('type','coin50','posx',x,'posy',y,'hp',100);
end
function [player,ent] = checkCollisions(player,ent)
count = zeros(1,length(ent));
for n = 1:length(ent)
if player.pos(1) >= ent(n).posx-4 && player.pos(1) <= ent(n).posx+4 ...
&& player.pos(2) >= ent(n).posy-4 && player.pos(2) <= ent(n).posy+4 ...
&& ent(n).hp ~= 0
ent(n).hp = 0;
if strcmp(ent(n).type,'coin20')
player.xp = player.xp + 20;
elseif strcmp(ent(n).type,'coin50')
player.xp = player.xp + 50;
end
end
count(n) = ent(n).hp == 0;
end
if all(count)
for j = 2:length(ent)
ent(j).hp = 100;
end
end
end
function [win,game,player,ent] = render(win,game,player,ent)
tic;
clf(win);
hold('on');
xlim([0,game.mapx]);
ylim([0,game.mapy]);
plot(game.mapx,game.mapy,'o');
% Render player
plot(player.pos(1),player.pos(2),'o','MarkerFaceColor',player.colorfill,'MarkerEdgeColor',player.colorborder);
% Render entities
for i = 1:length(ent)
if ent(i).hp ~= 0
switch ent(i).type
case 'coin20'
plot(ent(i).posx,ent(i).posy,'o','MarkerFaceColor',[1,.84,0],'MarkerEdgeColor',[.85,.65,.13]);
case 'coin50'
plot(ent(i).posx,ent(i).posy,'o','MarkerFaceColor',[.86,.08,.24],'MarkerEdgeColor',[0,0,1]);
end
end
end
%Render Overlay GUI
text(8,game.mapy-8,player.name,'Color','black','FontSize',14);
txt = strcat(['HP: ',num2str(player.hp)]);
text(8,game.mapy-16,txt,'Color','black','FontSize',10);
txt = strcat(['Stamina: ',num2str(player.stma)]);
text(8,game.mapy-22,txt,'Color','black','FontSize',10);
txt = strcat(['XP: ',num2str(player.xp)]);
text(8,10,txt,'Color','black','FontSize',10);
game.fps = 1/toc;
end
function gameSave(game,player,ent)
save('game.mat');
end