View Single Post
  #4  
Old 01-02-2012, 10:15 PM
Tricxta Tricxta is offline
The Muffin Man
Tricxta's Avatar
Join Date: Oct 2010
Location: Australia
Posts: 563
Tricxta is just really niceTricxta is just really nice
Quote:
Originally Posted by Gunderak View Post
PHP Code:
      this.sx2 = this.sx;
      
this.sy2 = this.sy;
      if (
this.dir == 0 && !onwall(this.sx, this.sy - 0.5)) {
        
this.sy -= 0.5;
      }
      if (
this.dir == 1 && !onwall(this.sx - 0.5, this.sy)) {
        
this.sx -= 0.5;
      }
      if (
this.dir == 2 && !onwall(this.sx, this.sy + 0.5)) {
        
this.sy += 0.5;
      }
      if (
this.dir == 3 && !onwall(this.sx + 0.5, this.sy)) {
        
this.sx += 0.5;
      }
...
    if (
this.dir == 0 && player.y > this.sy) {
      
player.y -= 0.5;
    }
    if (
this.dir == 2 && player.y < this.sy) {
      
player.y += 0.5;
    }
    if (
this.dir == 1 && player.x > this.sx) {
      
player.x -= 0.5;
    }
    if (
this.dir == 3 && player.x < this.sx) {
      
player.x += 0.5;
    } 
I always criticise people for this so don't worry you're not alone.
Instead of writing all these seperate statement it's possible to compact them
PHP Code:
for (temp.i = 0; i < 4; i++){
  if (
this.dir == temp.i && !onwall(this.sx + vecx(this.dir) * 0.5, this.sy + vecy(this.dir) * 0.5)){
    
this.sx += vecx(this.dir) * 0.5;
    
this.sy += vecy(this.dir) * 0.5;
  }
} 
Another good trick to remember is if you want to invert your direction instead of touching the direction you can just do vecx(dir)*-1 since I figured you're not so hot with math.

Also callimuc is correct in advising you to use updateboard although in my past the respawn method for those tiles becomes a bit messy which is something to keep in mind. None the less... you're on the right track so far, keep at it
Reply With Quote