forked from coderdojoindy/cookie-clicker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdungeons.js
346 lines (318 loc) · 10.8 KB
/
dungeons.js
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
var LaunchDungeons=function()
{
Game.GetWord=function(type)
{
if (type=='secret') return choose(['hidden','secret','mysterious','forgotten','forbidden','lost','sunk','buried','concealed','shrouded','invisible','elder']);
if (type=='ruined') return choose(['ancient','old','ruined','ravaged','destroyed','collapsed','demolished','burnt','torn-down','shattered','dilapidated','abandoned','crumbling','derelict','decaying']);
if (type=='magical') return choose(['arcane','magical','mystical','sacred','honed','banished','unholy','holy','demonic','enchanted','necromantic','bewitched','haunted','occult','astral']);
return '';
}
/*=====================================================================================
DUNGEONS
=======================================================================================*/
Game.DungeonTypes=[];
Game.DungeonType=function(name)
{
this.name=name;
this.nameGenerator=function(){return 'Mysterious dungeon';};
this.roomTypes=[];
Game.DungeonTypes[this.name]=this;
return this;
};
/*=====================================================================================
CREATE DUNGEON TYPES
=======================================================================================*/
new Game.DungeonType('Factory').
nameGenerator=function(){
var str='';
str+=Game.GetWord(choose(['secret','ruined','magical']))+' '+choose(['factory','factories','bakery','bakeries','confectionery','laboratory','research center','chocolate forge','chocolate foundry','manufactory','warehouse','machinery','works','bakeworks','workshop','assembly line']);
return str;
};
new Game.DungeonType('Mine').
nameGenerator=function(){
var str='';
str+=Game.GetWord(choose(['secret','ruined','magical']))+' '+choose(['chocolate','chocolate','chocolate','white chocolate','sugar','cacao'])+' '+choose(['mine','mines','pit','pits','quarry','excavation','tunnel','shaft','lode','trench','mountain','vein','cliff','peak','dome','crater','abyss','chasm','hole','burrow']);
return str;
};
new Game.DungeonType('Portal').
nameGenerator=function(){
var str='';
str+=Game.GetWord(choose(['secret','ruined','magical']))+' '+choose(['portal','gate','dimension','warpgate','door']);
return str;
};
new Game.DungeonType('Secret zebra level').
nameGenerator=function(){
var str='';
str+=Game.GetWord(choose(['secret']))+' '+choose(['zebra level']);
return str;
};
/*=====================================================================================
DUNGEON MECHANICS
=======================================================================================*/
Game.DungeonTiles=[];
Game.DungeonTile=function(name,pic,obstacle)
{
this.name=name;
this.pic=pic;
this.obstacle=obstacle;
this.id=Game.DungeonTiles.length;
Game.DungeonTiles[this.id]=this;
}
new Game.DungeonTile('void',[0,0],1);
new Game.DungeonTile('path',[1,0],0);
new Game.DungeonTile('path2',[2,0],0);
new Game.DungeonTile('land',[1,1],0);
new Game.DungeonTile('land2',[2,1],0);
new Game.DungeonTile('wall1',[0,1],1);
new Game.DungeonTile('wall2',[0,2],1);
new Game.DungeonTile('water',[2,2],1);
new Game.DungeonTile('bridge',[1,2],0);
Game.Item=function(type,x,y)//not loot, just objects you could find on the map : mobs, interactables, player, exits...
{
this.type=type;
this.x=x;
this.y=y;
this.dungeon=-1;
this.targets=[];
this.stuck=0;
this.Draw=function()
{
var pic=[0,0];
if (this.type=='monster') pic=[4,0];
return '<div class="thing" style="left:'+(this.x*16)+'px;top:'+(this.y*16)+'px;background-position:'+(-pic[0]*16)+'px '+(-pic[1]*16)+'px;"></div>';
}
this.Wander=function()
{
this.targets=[];
if (this.dungeon.CheckObstacle(this.x-1,this.y)==0) this.targets.push([-1,0]);
if (this.dungeon.CheckObstacle(this.x+1,this.y)==0) this.targets.push([1,0]);
if (this.dungeon.CheckObstacle(this.x,this.y-1)==0) this.targets.push([0,-1]);
if (this.dungeon.CheckObstacle(this.x,this.y+1)==0) this.targets.push([0,1]);
this.Move();
}
this.GoTo=function(x,y)
{
this.targets=[];
if (this.x<x) this.targets.push([1,0]);
if (this.x>x) this.targets.push([-1,0]);
if (this.y<y) this.targets.push([0,1]);
if (this.y>y) this.targets.push([0,-1]);
this.Move();
}
this.Move=function()
{
if (this.targets.length>0)
{
var target=choose(this.targets);
if (this.dungeon.CheckObstacle(this.x+target[0],this.y+target[1])==0)
{
this.x+=target[0];
this.y+=target[1];
}
else this.stuck++;
}
}
this.Turn=function()
{
if (this.type=='monster')
{
this.GoTo(this.dungeon.heroItem.x,this.dungeon.heroItem.y);//track the player
if (this.stuck || this.targets.length==[]) this.Wander();//can't reach the player? walk around randomly
}
this.stuck=0;
}
}
Game.Dungeons=[];
Game.Dungeon=function(type,id)
{
this.type=type;
this.id=id;
Game.Dungeons[this.id]=this;
this.log=[];
this.name=Game.DungeonTypes[this.type].nameGenerator();
this.hero=null;
this.Log=function(what)
{
}
this.items=[];
this.GetItem=function(x,y)
{
for (var i in this.items) {if (this.items[i].x==x && this.items[i].y==y) return i;}
return -1;
}
this.AddItem=function(what,x,y)
{
this.RemoveItem(x,y);
var item=new Game.Item(what,x,y);
this.items.push(item);
item.dungeon=this;
return item;
}
this.RemoveItem=function(x,y)
{
var item=this.GetItem(x,y);
if (item!=-1) this.items.splice(this.items.indexOf(item),1);
}
this.DrawItems=function()
{
var str='';
for (var i in this.items) {str+=this.items[i].Draw();}
return str;
}
this.CheckObstacle=function(x,y)
{
if (x<0 || x>=this.map.w || y<0 || y>=this.map.h) return 1;
if (this.GetItem(x,y)!=-1) return 1;
return this.map.data[x][y].obstacle;
}
this.map={};
this.Generate=function()
{
this.map={data:[],w:50,h:50,str:'',things:[],entrance:[0,0]};
this.map.entrance=[Math.floor(Math.random()*this.map.w),Math.floor(Math.random()*this.map.h)];
this.map.str='';
for (var x=0;x<this.map.w;x++)
{
this.map.data[x]=[];
for (var y=0;y<this.map.h;y++)
{
this.map.data[x][y]=Game.DungeonTiles[5];
if ((x%5>0 && y%5>0) || (x%5==2 || y%5==2)) this.map.data[x][y]=Game.DungeonTiles[choose([1,2,3,4])];
this.map.str+='<div class="tile" id="tile-'+this.id+'-'+x+'-'+y+'" style="left:'+(x*16)+'px;top:'+(y*16)+'px;background-position:'+(-this.map.data[x][y].pic[0]*16)+'px '+(-this.map.data[x][y].pic[1]*16)+'px;"></div>';
}
}
for (var i=0;i<50;i++) {this.AddItem('monster',Math.floor(Math.random()*this.map.w),Math.floor(Math.random()*this.map.h));}
}
this.onTile=-1;
this.Draw=function()
{
var str='';
var x=-this.hero.x;
var y=-this.hero.y;
str+='<div id="map'+this.id+'" class="map" style="width:'+(9*16)+'px;height:'+(9*16)+'px;"><div id="mapcontainer'+this.id+'" style="position:absolute;left:'+(x*16)+'px;top:'+(y*16)+'px;"><div id="mapitems'+this.id+'"></div>'+this.map.str+'</div></div>';
str+='<div style="position:absolute;left:'+(9*16+16)+'px;">'+
'<a onclick="Game.ObjectsById['+this.id+'].setSpecial(0);">Exit</a><br>'+
'<a class="control west" onclick="Game.HeroesById['+this.hero.id+'].Move(-1,0);"></a><br>'+
'<a class="control east" onclick="Game.HeroesById['+this.hero.id+'].Move(1,0);"></a><br>'+
'<a class="control north" onclick="Game.HeroesById['+this.hero.id+'].Move(0,-1);"></a><br>'+
'<a class="control south" onclick="Game.HeroesById['+this.hero.id+'].Move(0,1);"></a><br>'+
'<a class="control middle" onclick="Game.HeroesById['+this.hero.id+'].Move(0,0);"></a><br>'+
'</div>';
l('rowSpecial'+this.id).innerHTML='<div style="width:100%;height:100%;z-index:10000;position:absolute;left:0px;top:0px;">'+str+'</div>';
}
this.Refresh=function()
{
if (!l('mapcontainer'+this.id)) this.Draw();
var x=4-this.hero.x;
var y=4-this.hero.y;
l('mapcontainer'+this.id).style.left=(x*16)+'px';
l('mapcontainer'+this.id).style.top=(y*16)+'px';
l('mapitems'+this.id).innerHTML=this.DrawItems();
}
this.Turn=function()
{
for (var i in this.items)
{
this.items[i].Turn();
}
this.Refresh();
}
this.DrawButton=function()
{
var str='';
str+='<div style="text-align:center;margin:48px auto;color:#999;"><a onclick="Game.ObjectsById['+this.id+'].setSpecial(1);">Enter</a></div>';
return str;
}
}
/*=====================================================================================
CREATE DUNGEONS
=======================================================================================*/
Game.Objects['Factory'].special=function()
{
this.dungeon=new Game.Dungeon('Factory',this.id);
this.dungeon.Generate();
this.specialDrawFunction=function(){this.dungeon.Refresh();};
this.drawSpecialButton=function(){return this.dungeon.DrawButton();};
Game.HeroesById[0].EnterDungeon(this.dungeon,this.dungeon.map.entrance[0],this.dungeon.map.entrance[1]);
}
/*=====================================================================================
HEROES
=======================================================================================*/
Game.Heroes=[];
Game.HeroesById=[];
Game.Hero=function(name,pic)
{
this.name=name;
this.pic=pic;
this.stats={
hp:20,
hpm:20,
might:5,
guard:5,
speed:5,
dodge:5,
luck:5
};
this.dialogue={
'greeting':'Oh hey.|Sup.',
'entrance':'Here we go.|So exciting.',
'completion':'That was easy.|All done here.',
'defeat':'Welp.|Better luck next time.'
};
this.gear={
'armor':-1,
'weapon':-1
};
this.inDungeon=-1;
this.completedDungeons=0;
this.x=0;
this.y=0;
this.EnterDungeon=function(dungeon,x,y)
{
this.inDungeon=dungeon.id;
dungeon.hero=this;
this.x=x;
this.y=y;
dungeon.heroItem=dungeon.AddItem('hero',x,y);
Game.Dungeons[this.inDungeon].Refresh();
}
this.Move=function(x,y)
{
var dungeon=Game.Dungeons[this.inDungeon];
if (1 || dungeon.CheckObstacle(this.x+x,this.y+y)==0 || (x==0 && y==0))
{
this.x=this.x+x;
this.y=this.y+y;
dungeon.heroItem.x=this.x;
dungeon.heroItem.y=this.y;
dungeon.Turn();
}
}
this.save=function()
{
var str='';
str+=
this.inDungeon+','+
this.completedDungeons+','+
this.gear.armor+','+
this.gear.weapon
;
return str;
}
this.load=function(data)
{
var str=data.split(',');
this.inDungeon=parseInt(str[0]);
this.completedDungeons=parseInt(str[1]);
this.gear.armor=parseInt(str[2]);
this.gear.weapon=parseInt(str[3]);
}
this.id=Game.Heroes.length;
Game.Heroes[this.name]=this;
Game.HeroesById[this.id]=this;
}
/*=====================================================================================
CREATE HEROES
=======================================================================================*/
new Game.Hero('Mysterious hero','nopic.png');
};