from agentspace import Agent, space

class ControlAgent(Agent):

    def __init__(self,nameTarget,nameEmotion,nameForward,nameTurn):
        self.nameTarget = nameTarget
        self.nameEmotion = nameEmotion
        self.nameForward = nameForward
        self.nameTurn = nameTurn
        super().__init__()

    def init(self):
        self.attach_timer(0.1)

    def senseSelectAct(self):
        target = space[self.nameTarget] # center-x, center-y, width, height 0..1
        emotionLabel = space(default="Neutral")[self.nameEmotion+"Label"]
        if target is None: # no target, stop
            space(validity=0.2)[self.nameForward] = 0
            space(validity=0.2)[self.nameTurn] = 0
        elif target[0] < 0.4: # to left
            space(validity=0.2)[self.nameForward] = 0
            space(validity=0.2)[self.nameTurn] = -1
        elif target[0] > 0.6: # to right
            space(validity=0.2)[self.nameForward] = 0
            space(validity=0.2)[self.nameTurn] = 1
        elif emotionLabel == "Happiness": # forward
            space(validity=0.2)[self.nameForward] = 1
            space(validity=0.2)[self.nameTurn] = 0
        elif emotionLabel in ["Anger","Fear"]: # backward
            space(validity=0.2)[self.nameForward] = -1
            space(validity=0.2)[self.nameTurn] = 0
        else: # fine as is, stop
            space(validity=0.2)[self.nameForward] = 0
            space(validity=0.2)[self.nameTurn] = 0
