-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp5_js.js
203 lines (151 loc) · 4.82 KB
/
p5_js.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
const TAMANHO_TILE = 1280/25;
const VEL = 1;
var socket = io()
let img;
function preload() {
img = loadImage('./tiles/tiles.png');
}
class ElementoMapa{
constructor(posx,posy,cor,tam){
this.x =posx;
this.y = posy;
this.cor = cor;
this.tam = tam;
this.draw = function(){
fill(this.cor);
square( this.x * this.tam , this.y * this.tam, this.tam );
}
}
}
class SpriteBlock{
constructor(posx,posy,matrixX,matrixY){
this.x =posx;
this.y = posy;
this.matrixX = matrixX;
this.matrixY = matrixY;
this.draw = function(){
//fill(this.cor);
image(img,
this.x * TAMANHO_TILE ,
this.y * TAMANHO_TILE,
TAMANHO_TILE,
TAMANHO_TILE,
16*this.matrixX,
16*this.matrixY,
16,
16
);
//square( this.x * this.tam , this.y * this.tam, this.tam );
}
}
}
class Wall extends SpriteBlock{
constructor(x,y){
super(x,y,1,1);
}
}
class BlocoAzul extends ElementoMapa{
constructor(posx,posy){
super(posx,posy,'blue',TAMANHO_TILE);
}
}
class BlocoVermelho extends ElementoMapa{
constructor(posx,posy){
super(posx,posy,'red',TAMANHO_TILE);
}
}
class BlocoAmarelo extends ElementoMapa{
constructor(posx,posy){
super(posx,posy,'yellow',TAMANHO_TILE);
}
}
class BlocoPreto extends ElementoMapa{
constructor(posx,posy){
super(posx,posy,'black',TAMANHO_TILE);
}
}
class Player extends BlocoVermelho{
constructor(id,posx,posy){
super(posx,posy);
this.id = id;
}
move(tecla){
if ( tecla==UP_ARROW && this.canMove(this.x, this.y-VEL) ) this.y-= VEL;
if ( tecla==DOWN_ARROW && this.canMove(this.x, this.y+VEL) ) this.y+= VEL;
if ( tecla==LEFT_ARROW && this.canMove(this.x-VEL, this.y) ) this.x-= VEL;
if ( tecla==RIGHT_ARROW && this.canMove(this.x+VEL, this.y) ) this.x+= VEL;
}
update(){
}
canMove(newX, newY){
return !maps[ Math.floor(newY) ][ Math.floor(newX)] || maps[ Math.floor(newY)][ Math.floor(newX)] == 2;
}
}
class Inimigo extends BlocoAmarelo{
constructor(posx,posy){
super(posx,posy);
}
move(){
let mover = parseInt(random(1, 5)) ;
//console.log(mover);
setTimeout(()=>{
if ( mover == 1 && this.canMove(this.x, this.y-1) ) this.y--;
if ( mover == 2 && this.canMove(this.x, this.y+1) ) this.y++;
if ( mover == 3 && this.canMove(this.x-1, this.y) ) this.x--;
if ( mover == 4 && this.canMove(this.x+1, this.y) ) this.x++;
},random(0, 1000));
}
canMove(newX, newY){
return !maps[newY][newX] || maps[newY][newX] == 2;
}
}
var maps = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
];
var elementos = [];
var players = [] ;
var jogador;
//var inimigo = new Inimigo(10,7)
function setup(){
const blocos = [BlocoPreto, Wall, BlocoAmarelo, BlocoVermelho];
jogador = new Player(parseInt(random(10)),1,7);
createCanvas(1280, 685);
frameRate(30);
for (let i = 0; i < maps.length; i++) {
for(let j=0;j< maps[i].length;j++) {
elementos.push(new blocos[maps[i][j]](j,i));
}
}
}
function draw(){
background(0,0,0);
for (let i = 0; i < elementos.length; i++) {
elementos[i].draw();
}
jogador.draw();
// inimigo.draw();
// inimigo.move();
// console.log("Total players",players.length);
if(players.length){
for(let i=0;i<players.length;i++){
//console.log(players[i],players[i].x);
fill(players[i].cor);
square( players[i].x * players[i].tam , players[i].y * players[i].tam, players[i].tam );
}
}
if(keyIsDown(keyCode)) {
jogador.move(keyCode);
}
}