Advertisement
Guest User

Kk

a guest
Jan 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function setup(){
  2.     grass = loadImage("imgs/grass.png");
  3.     water = loadImage("imgs/water.png");
  4.     player = loadImage("imgs/player.png");
  5.     grassT = loadImage("imgs/grassTarget.png")
  6.    
  7.     tilepSize = 30;
  8.     sizeH = mapTiled.length;
  9.     sizeW = mapTiled[0].length;
  10.     c = 0;
  11.     createCanvas(sizeW*tilepSize, sizeH*tilepSize);
  12.     map_ = [];
  13.     plr = new Player(player, 0, 0);
  14.     for(var i = 0; i < sizeW; i++){
  15.         for(var j = 0; j < sizeH; j++){
  16.             switch(mapTiled[j][i]){
  17.                 case 0:
  18.                     map_[c] = new Tile(grass, i, j, true);
  19.                     break;
  20.                 case 1:
  21.                     map_[c] = new Tile(water, i, j, false);
  22.                     break;
  23.             }
  24.             c++;
  25.         }
  26.     }
  27.     c = 0;
  28. }
  29.  
  30. function draw(){
  31.     background(0);
  32.     for(var i = 0; i < sizeW*sizeH; i++){
  33.         map_[i].put();
  34.     }
  35.     plr.put();
  36. }
  37.  
  38. function Tile(texture, x, y, walkable){
  39.     this.texture = texture;
  40.     this.x = x;
  41.     this.y = y;
  42.     this.walkable = walkable;
  43.     this.put = function(){
  44.         image(this.texture, this.x*30, this.y*30);
  45.     }
  46.     this.clicked = function(px, py){
  47.        
  48.     }
  49. }
  50.  
  51. function Player(img, x, y){
  52.    
  53.    
  54.     this.put = function(){
  55.         image(img, x, y);
  56.     }
  57. }
  58.  
  59. function mouseClicked(){
  60.     var clicked;
  61.     for(var i = 0; i < sizeW*sizeH; i++){
  62.         if(map_[i].texture === grassT){
  63.             map_[i].texture = grass;
  64.         }
  65.     }
  66.     if(clicked == undefined){
  67.         for(var i = 0; i < sizeW*sizeH; i++){
  68.             if(mouseX > map_[i].x*30 && mouseX < map_[i].x*30 + tilepSize && mouseY > map_[i].y*30 && mouseY < map_[i].y*30 + tilepSize){
  69.                 if(map_[i].walkable){
  70.                     console.log("ok");
  71.                     map_[i].texture = grassT;
  72.                 }
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement