A question regarding the self.pos object


#1

So i’m in the desert area, and i know that i could just use the self.moveXY method manually to dodge the yaks, but i want to use it the way i’m suppose to be using it, so here’s the thing:
My character can only detect enemy from all around him, but i want him to only focus on one of his side, kinda like:

loop:
    x = self.pos.x
    y = self.pos.y
    enemy = self.findNearest(self.findEnemies())
    if enemy and self.distanceTo(enemy) < (+x).10:
        newX = self.pos.x-10 
        newY = self.pos.y
        self.moveXY(newX, newY)
    if enemy and self.distanceTo(enemy) < (-x).10:
        newX = self.pos.x+10  
        newY = self.pos.y
        self.moveXY(newX, newY)

the -x and +x is the side in which the character would react to the passing enemy (-x means left, +x means right), and adding the same +/- value to the y direction would result in the character’s FOV increase in an ark-like pattern.

But the point is, is there a string of code that would let me tell my character to focus on a particular side and tell him to scan the other direction if there’s no other enemy on said direction, because HOLY YAK the level is confusingly hard, i actually manage to finish the first level; simply by using the self.wait() method! And i have no idea what i’m suppose to be doing on the second level, so i’m officially stuck i guess :frowning:

Also here’s my coding:

loop:
    x = self.pos.x
    y = self.pos.y
    enemy = self.findNearest(self.findEnemies())
    if enemy and self.distanceTo(enemy) < 15:
        newX = self.pos.x+5
        newY = self.pos.y
        self.moveXY(newX, newY)

I don’t know what i’m suppose to do, i mess around with this coding for half an hour but my character is still want to pick up a fight with a yak that walk to his right by bumping to it. so do i have to manually set the self.moveXY method to avoid the rest of the Yak or what? Or should i experiment with the this “break” thing the veteran players keep adding to their own coding in the forum?

And thanks for reading beforehand :smiley:


#2

Remember that you have enemy.pos.x, enemy.pos.y and self.pos.x, self.pos.y

#if the yak is above you move down
if (enemy.pos.y>self.pos.y) then:
    self.moveXY(self.pos.x,self.pos.y-10)
    continue
#if the yak is on the left then move right
if (enemy.pos.x<self.pos.x) then:
   self.moveXY(self.pos.x+10,self.pos.y)
   continue

Unfortunately this works only for specific situation. It is good enough for the tutorial levels although.

What happens if the enemy is both on top on right of you?
If you want more advanced detection you can determine the exact quadrant:

dx=enemy.pos.x-self.pos.x
dy=enemy.pos.y-self.pos.y
# top left half
   if (dy - dx >= 0 ) then:
   # top right half   
   if( dy+dx >= 0) then:
        # upper quarter
        # move down
   # else not top right = bottom left
   else:
        # top left + bottom left = left quarter
        # move to the right
# else not top left = bottom right
else:
     # top right half
      if( dy+dx >= 0) then:
            # bottom right + top right =  right quarter

Later on you’ll learn about vectors which directional reactions much easier


#3

@AdrianCgw
Uuhhhhhh… At which point in the campaign are you suppose to learn those things!? O_o


#4

You learn about vectors in Kelvintaph Glacier, specifically Circle Walking, Skating Away, and Brewball.


#5

@ChronistGilver
ok thanks for giving me a heads-up on those levels, i’m gonna practice those coding now :smiley:


#6

Yup. Hard, Hard work.


#7

Hey guys! i think i found something cool! :smiley:

loop:
    x = self.pos.x
    y = self.pos.y
    enemy = self.findNearest(self.findEnemies())
    if enemy and self.distanceTo(enemy) < 10:
        newX = self.pos.x+10
        newY = self.pos.y
        self.moveXY(newX, newY)
        self.wait(2)
    if (enemy.pos.y>self.pos.y):
        self.moveXY(self.pos.x,self.pos.y-10)
    if (enemy.pos.x<self.pos.x):
        self.moveXY(self.pos.x+10,self.pos.y)

Try it! :smiley: