Skip to content
ShawYoung edited this page Dec 12, 2014 · 3 revisions
class Player
$safeLine=12
#0left 1right
$objectIndex=0
$direction=1
$damage=0
$HP=20
$beingAttacked=false
#0default 1walkForward 2walkBackward 3attack 4shoot 5pivot 6rest 7rescue
$lastAction=0
$continueShoot=0
$needRest=false
  def play_turn(warrior)
	updateState(warrior)
	$objectIndex=getObjectIndex(warrior.look)
	puts $objectIndex
	if $objectIndex==-1
		puts "clear"
		clear(warrior)
	elsif $objectIndex==0
		puts "infront"
		infront(warrior)
	else
		puts "remote"
		remote(warrior)
	
	end
  end
  
  def updateState(warrior)
	$damage=$HP-warrior.health
	if $damage>0
		$beingAttacked=true
	else 
		$beingAttacked=false
	end
	if warrior.health>=$safeLine
		$needRest=false
	else
		$needRest=true
	end
	if $lastAction==4 
		$continueShoot=$continueShoot+1
	else
		$continueShoot=0 
	end
  end
  
  def getObjectIndex(array)
	i=0
	while i<=2
		if !array[i].empty?
			return i
		end
		i=i+1
	end
	return -1
	
  end
  
  def clear (warrior)
	if $needRest
		warrior.rest!
		$lastAction=6
	else
		warrior.walk!
		$lastAction=1
	end
  end
  
  def infront(warrior)
	if warrior.feel.wall?
		turn(warrior)
		$lastAction=5
	elsif warrior.feel.enemy?
		warrior.attack!
		$lastAction=3
	elsif warrior.feel.captive?
		warrior.rescue!
		$lastAction=7 
	else
		warrior.walk!
		$lastAction=1
	end
  end
  
  def remote(warrior)
	if warrior.look[$objectIndex].captive?
		warrior.walk!
		$lastAction=1
	elsif warrior.look[$objectIndex].stairs?
		warrior.walk!
		$lastAction=1 
	elsif warrior.look[$objectIndex].enemy?
		if enemyInBack(warrior)&&$direction==1
			warrior.walk!
			$lastAction=1
		elsif captiveInBack(warrior)
			puts "cp Back"
			turn(warrior)
			
		elsif 
			warrior.shoot!
			$lastAction=4
		end
	else
		warrior.walk!
		$lastAction=1 
	end
  end
  
  def captiveInBack(warrior)
	
	array=warrior.look:backward
	for i in 0..2
		if array[i].captive?
			return true
		end
	end
	return false
  
  end
  def turn (warrior)
	if($direction==1)
		$direction=0
	else
		$direction=1
	end
	warrior.pivot!
  end
  
  def enemyInBack(warrior)
	array=warrior.look:backward
	for i in 0..2
		if array[i].enemy?
			return true
		end
	end
	return false
  end
 end