// File: /workspace/github_cpp_makefile_star2_O3/KellerMartins[P]PDS2-Pacman/source/game/enemy.cpp
// Function: Enemy::OnUpdate()
void Enemy::OnUpdate(){
	if(!_isOver){
		int pac_x = Pacman::GetX();
		int pac_y = Pacman::GetY();
		int dirX =  Pacman::GetDirX();
		int dirY =  Pacman::GetDirY();

		_timerScatter += GetFrameTime();
		if(_timerScatter > SCATTER_TIME) {
			_scatter = true;
		}
		if(_timerScatter <= 0){
			_scatter = false;
		}

		if(GetAlive() && IsScared()){
			_model.SetColor(PURPLE);
			_timerScatter = 0;
		}
		else if(GetAlive() && !IsScared()){
			_model.SetColor(_color);
		}
		
		int newGoalX,newGoalY;
		SetGoal(newGoalX, newGoalY, pac_x,pac_y, dirX, dirY);


		if(newGoalX >= 0 && newGoalX < LARGURA && 
		newGoalY >= 0 && newGoalY < ALTURA && 
		Mapa::GetElementoMapa(newGoalX, newGoalY) != ElementoMapa::Parede){
			_goalX = newGoalX;
			_goalY = newGoalY;
		}

		_velocidade = _scared && _alive ? GHOST_SPEED_SCARED : GHOST_SPEED_DEFAULT;
		_timerMovimento += GetFrameTime()*_velocidade;
		
		if(abs(_x-Modulus(_x+_direcaoX, LARGURA)) != LARGURA-1){
			_model.position.x = Lerp(_x, _x+_direcaoX, _timerMovimento);
		}else{
			_model.position.x = _x+_direcaoX;
			_timerMovimento = 1;
		}

		if(abs(_y-Modulus(_y+_direcaoY, ALTURA)) != ALTURA-1){
			_model.position.z = Lerp(_y, _y+_direcaoY, _timerMovimento);
		}else{
			_model.position.z = _y+_direcaoY;
			_timerMovimento = 1;
		}

		if(_timerMovimento >= 1){
			_timerMovimento = 0;
			_x = Modulus(_x+_direcaoX, LARGURA);
			_y = Modulus(_y+_direcaoY, ALTURA);
			_model.position.x = _x;
			_model.position.z = _y;
			Mapa::ObtemDirecao(_x, _y, _goalX,_goalY, _direcaoX, _direcaoY);

			if(!_alive && _x == _goalX && _y == _goalY){
				_alive = true;
				_model.SetColor(_color);
				_model.Load3DModel("assets/models/ghost.obj");
			}
		}

		float lookAngle = _direcaoX < 0 ? -90 : 
						  _direcaoX > 0 ? 90 :
						  _direcaoY < 0 ? 180 : 
						  _direcaoY > 0 ? 0 : _model.rotationAngle;
		
		_model.rotationAngle = Lerp(_model.rotationAngle, lookAngle, GetFrameTime()*5);

	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
cmpb $0x0, 0xcfc0a(%rip)     # 0xd9e28
jne 0xa5a3
movq %rdi, %rbx
callq 0xdcee
movl %eax, -0x2c(%rbp)
callq 0xdcfa
movl %eax, %r15d
callq 0xdd06
movl %eax, %r12d
callq 0xdd12
movl %eax, %r13d
callq 0x2f43a
addss 0x2e8(%rbx), %xmm0
movss %xmm0, 0x2e8(%rbx)
ucomiss 0x8efbd(%rip), %xmm0    # 0x99220
jbe 0xa26c
movb $0x1, 0x2d8(%rbx)
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jb 0xa27b
movb $0x0, 0x2d8(%rbx)
cmpb $0x1, 0x2d9(%rbx)
jne 0xa2b2
leaq 0x8(%rbx), %rdi
cmpb $0x1, 0xcfb9a(%rip)     # 0xd9e29
jne 0xa2a7
movl $0xffff7ac8, %esi       # imm = 0xFFFF7AC8
callq 0xe76e
movl $0x0, 0x2e8(%rbx)
jmp 0xa2b2
movl 0x2e0(%rbx), %esi
callq 0xe76e
movq (%rbx), %rax
movl %r13d, (%rsp)
leaq -0x38(%rbp), %r13
leaq -0x34(%rbp), %r14
movq %rbx, %rdi
movq %r13, %rsi
movq %r14, %rdx
movl -0x2c(%rbp), %ecx
movl %r15d, %r8d
movl %r12d, %r9d
callq *0x28(%rax)
movl (%r13), %edi
cmpl $0x15, %edi
setb %al
movl (%r14), %esi
cmpl $0x1b, %esi
setb %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0xa30c
callq 0xc5b8
cmpl $0x1, %eax
je 0xa30c
movl -0x38(%rbp), %eax
movl %eax, 0x2ec(%rbx)
movl -0x34(%rbp), %eax
movl %eax, 0x2f0(%rbx)
movb 0x2d9(%rbx), %al
testb %al, 0xcfb11(%rip)      # 0xd9e29
jne 0xa324
movss 0x8eee2(%rip), %xmm0    # 0x99204
jmp 0xa32c
movss 0x8eef8(%rip), %xmm0    # 0x99224
movss %xmm0, 0x2dc(%rbx)
callq 0x2f43a
mulss 0x2dc(%rbx), %xmm0
addss 0x2e4(%rbx), %xmm0
movss %xmm0, 0x2e4(%rbx)
movl 0x2c8(%rbx), %r14d
movl 0x2d4(%rbx), %edi
addl %r14d, %edi
movl $0x15, %esi
callq 0x9f88
subl %eax, %r14d
movl %r14d, %ecx
negl %ecx
cmovsl %r14d, %ecx
movl 0x2c8(%rbx), %eax
cmpl $0x14, %ecx
jne 0xa39b
addl 0x2d4(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movl $0x3f800000, 0x2e4(%rbx) # imm = 0x3F800000
jmp 0xa3bc
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addl 0x2d4(%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movss 0x2e4(%rbx), %xmm2
callq 0x9d93
leaq 0x2d4(%rbx), %r15
movss %xmm0, 0x18(%rbx)
movl 0x2cc(%rbx), %r12d
leaq 0x2d0(%rbx), %r14
movl 0x2d0(%rbx), %edi
addl %r12d, %edi
movl $0x1b, %esi
callq 0x9f88
subl %eax, %r12d
movl %r12d, %ecx
negl %ecx
cmovsl %r12d, %ecx
movl 0x2cc(%rbx), %eax
cmpl $0x1a, %ecx
jne 0xa41e
addl 0x2d0(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x20(%rbx)
movl $0x3f800000, 0x2e4(%rbx) # imm = 0x3F800000
jmp 0xa459
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addl 0x2d0(%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movss 0x2e4(%rbx), %xmm2
callq 0x9d93
movss %xmm0, 0x20(%rbx)
movss 0x2e4(%rbx), %xmm0
ucomiss 0x8ece1(%rip), %xmm0    # 0x99134
jb 0xa556
movl $0x0, 0x2e4(%rbx)
movl 0x2d4(%rbx), %edi
addl 0x2c8(%rbx), %edi
movl $0x15, %esi
callq 0x9f88
movl %eax, 0x2c8(%rbx)
movl 0x2d0(%rbx), %edi
addl 0x2cc(%rbx), %edi
movl $0x1b, %esi
callq 0x9f88
movl %eax, 0x2cc(%rbx)
movl 0x2c8(%rbx), %edi
xorps %xmm0, %xmm0
cvtsi2ss %edi, %xmm0
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movl 0x2ec(%rbx), %edx
movss %xmm0, 0x18(%rbx)
movss %xmm1, 0x20(%rbx)
movl 0x2f0(%rbx), %ecx
movl %eax, %esi
movq %r15, %r8
movq %r14, %r9
callq 0xc48a
cmpb $0x0, 0x2d9(%rbx)
jne 0xa556
movl 0x2c8(%rbx), %eax
cmpl 0x2ec(%rbx), %eax
jne 0xa556
movl 0x2cc(%rbx), %eax
cmpl 0x2f0(%rbx), %eax
jne 0xa556
leaq 0x8(%rbx), %r12
movb $0x1, 0x2d9(%rbx)
movl 0x2e0(%rbx), %esi
movq %r12, %rdi
callq 0xe76e
leaq -0x48(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x8ed19(%rip), %rsi     # 0x99238
leaq 0x8ed29(%rip), %rdx     # 0x9924f
leaq -0x58(%rbp), %r13
movq %r13, %rdi
callq 0xa75c
movq %r12, %rdi
movq %r13, %rsi
callq 0xe604
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xa556
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x8880
cmpl $0x0, (%r15)
js 0xa568
je 0xa5b2
movss 0x8ecc6(%rip), %xmm0    # 0x9922c
jmp 0xa570
movss 0x8ecb8(%rip), %xmm0    # 0x99228
movss %xmm0, -0x30(%rbp)
movss 0x3c(%rbx), %xmm1
movss %xmm1, -0x2c(%rbp)
callq 0x2f43a
movaps %xmm0, %xmm2
mulss 0x8eca5(%rip), %xmm2    # 0x99234
movss -0x2c(%rbp), %xmm0
movss -0x30(%rbp), %xmm1
callq 0x9d93
movss %xmm0, 0x3c(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, (%r14)
js 0xa5c4
xorps %xmm0, %xmm0
jne 0xa570
movss 0x3c(%rbx), %xmm0
jmp 0xa570
movss 0x8ec64(%rip), %xmm0    # 0x99230
jmp 0xa570
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xa5ea
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x8880
movq %rbx, %rdi
callq 0x8ea0