-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene.js
176 lines (136 loc) · 4.16 KB
/
scene.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
//
// ojo supone que al crear la escena en curso se ha dejado en la global thescene
//
var deltatms=50;
var player_initial_pos_x = 128;
var player_initial_pos_y = 128;
function Scene(ctx) {
this.running=false;
this.ctx=ctx;
this.nticks=0;
this.paused=false;
this.frames= new GFrames(this.ctx,'Personajes.png',64,64);
this.otherframes= new GFrames(this.ctx,'Fondos.png',64,64);
this.soundbox= new SoundBox();
this.stats= new Scoreboard();
this.dialog = new Dialog('dialog1');
}
Scene.prototype.start_level = function(x) {
this.stats.reset();
get_map(x,this);
this.background = new Background(this.ctx,this.map);
this.malos = new Array();
var i;
for (i=0;i<this.nmalos;i++) {
var malo_x,malo_y;
do {
malo_x=Math.random()*w;
malo_y=Math.random()*h;
malo_x=Math.floor(malo_x/64)*64;
malo_y=Math.floor(malo_y/64)*64;
//log('malo at ('+malo_x+','+malo_y+')+ free '+this.map.free64block(malo_x,malo_y));
} while( (Math.abs(malo_x-this.player.x)<200 && Math.abs(malo_y-this.player.y)<200)
|| !this.map.free64block(malo_x,malo_y) ) ;
//log('ok');
this.malos.push(new Sprite(this.frames,malo_x,malo_y,sprite_tipo_orco));
}
this.won=false;
}
Scene.prototype.start_game = function() {
if (this.running) {
return;
}
this.running=true;
this.level=1;
this.start_level(this.level);
setTimeout("thescene.tick();",deltatms);
this.dialog.message_start();
}
Scene.prototype.restart_game = function() {
this.start_level(this.level);
setTimeout("thescene.tick();",deltatms);
this.dialog.message_restart();
}
Scene.prototype.tick = function() {
this.nticks+=1;
react_to_control();
this.player.tick();
var i;
for (i=0 ; i < this.malos.length ; i++ ) {
this.malos[i].tick();
if (!this.player.dead) this.player.checkCollision(this.malos[i])
}
this.draw();
if ( !this.paused ) {
setTimeout("thescene.tick();",deltatms);
}
}
Scene.prototype.pause = function() {
this.paused=true;
}
Scene.prototype.unpause = function() {
if (this.paused) {
this.paused=false;
setTimeout("thescene.tick();",deltatms);
}
}
Scene.prototype.dialog_enter = function() {
this.dialog.hide();
if (thescene.player.dead) {
this.pause();
this.level=1;
this.restart_game();
} else if (thescene.won) {
this.level+=1;
this.restart_game();
} else {
this.unpause();
}
}
Scene.prototype.draw = function() {
this.background.draw();
//log('drawing... '+this.nticks);
// erase all
//this.ctx.fillStyle='#ff0000';
//this.ctx.fillRect(0,0,800,600);
this.princess.draw();
this.player.draw();
var i;
for (i=0 ; i < this.malos.length ; i++ ) {
this.malos[i].draw();
}
// this.frames.drawFrame(this.nticks%2+this.player_direccion*2,0,this.player_x,this.player_y);
// this.frames.drawFrame(2+(this.nticks+1)%2,1,600,460);
// this.frames.drawFrame(2+this.nticks%2,1,670,490);
// this.frames.drawFrame(2+(this.nticks+1)%2,1,620,500);
if (debug) {
this.ctx.fillStyle='#ff0000';
if (left1_is_pressed) this.ctx.fillStyle='#00ff00';
this.ctx.fillRect(10,620,10,10);
this.ctx.fillStyle='#ff0000';
if (right1_is_pressed) this.ctx.fillStyle='#00ff00';
this.ctx.fillRect(50,620,10,10);
this.ctx.fillStyle='#ff0000';
if (up1_is_pressed) this.ctx.fillStyle='#00ff00';
this.ctx.fillRect(30,620,10,10);
this.ctx.fillStyle='#ff0000';
if (down1_is_pressed) this.ctx.fillStyle='#00ff00';
this.ctx.fillRect(30,600,10,10);
this.ctx.fillStyle='#ff0000';
if (left2_is_pressed) this.ctx.fillStyle='#00ff00';
this.ctx.fillRect(70,620,10,10);
this.ctx.fillStyle='#ff0000';
if (right2_is_pressed) this.ctx.fillStyle='#00ff00';
this.ctx.fillRect(110,620,10,10);
this.ctx.fillStyle='#ff0000';
if (up2_is_pressed) this.ctx.fillStyle='#00ff00';
this.ctx.fillRect(90,620,10,10);
this.ctx.fillStyle='#ff0000';
if (down2_is_pressed) this.ctx.fillStyle='#00ff00';
this.ctx.fillRect(90,600,10,10);
this.ctx.fillStyle='#00ff00';
if (mouse_is_down) this.ctx.fillRect(mouse_target_x-10,mouse_target_y-10,20,20);
this.ctx.fillStyle='#ff0000';
if (mouse_is_down) this.ctx.fillRect(thescene.player.x-10,thescene.player.y-10,20,20);
}
}