// File: /workspace/github_cpp_makefile_star2_O1/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, 0xcfbf6(%rip)     # 0xd9e28
jne 0xa5ce
movq %rdi, %rbx
callq 0xdc42
movl %eax, -0x2c(%rbp)
callq 0xdc4e
movl %eax, %r15d
callq 0xdc5a
movl %eax, %r12d
callq 0xdc66
movl %eax, %r13d
callq 0x2f3b6
addss 0x2e8(%rbx), %xmm0
movss %xmm0, 0x2e8(%rbx)
ucomiss 0x8efa9(%rip), %xmm0    # 0x99220
jbe 0xa280
movb $0x1, 0x2d8(%rbx)
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jb 0xa28f
movb $0x0, 0x2d8(%rbx)
movb 0x2d9(%rbx), %al
cmpb $0x1, %al
jne 0xa2d8
cmpb $0x1, 0xcfb89(%rip)     # 0xd9e29
jne 0xa2bc
leaq 0x8(%rbx), %rdi
movl $0xffff7ac8, %esi       # imm = 0xFFFF7AC8
callq 0xe6be
movl $0x0, 0x2e8(%rbx)
jmp 0xa2d8
testb %al, %al
je 0xa2d8
cmpb $0x0, 0xcfb62(%rip)     # 0xd9e29
jne 0xa2d8
leaq 0x8(%rbx), %rdi
movl 0x2e0(%rbx), %esi
callq 0xe6be
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 0xa332
callq 0xc4f2
cmpl $0x1, %eax
je 0xa332
movl -0x38(%rbp), %eax
movl %eax, 0x2ec(%rbx)
movl -0x34(%rbp), %eax
movl %eax, 0x2f0(%rbx)
movb 0x2d9(%rbx), %al
testb %al, 0xcfaeb(%rip)      # 0xd9e29
jne 0xa34a
movss 0x8eebc(%rip), %xmm0    # 0x99204
jmp 0xa352
movss 0x8eed2(%rip), %xmm0    # 0x99224
movss %xmm0, 0x2dc(%rbx)
callq 0x2f3b6
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 0x9f9d
subl %eax, %r14d
movl %r14d, %ecx
negl %ecx
cmovsl %r14d, %ecx
movl 0x2c8(%rbx), %eax
cmpl $0x14, %ecx
jne 0xa3c6
addl 0x2d4(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x18(%rbx)
movl $0x3f800000, 0x2e4(%rbx) # imm = 0x3F800000
jmp 0xa3ec
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addl 0x2d4(%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movss 0x2e4(%rbx), %xmm2
callq 0x9d97
movss %xmm0, 0x18(%rbx)
movl 0x2cc(%rbx), %r14d
movl 0x2d0(%rbx), %edi
addl %r14d, %edi
movl $0x1b, %esi
callq 0x9f9d
subl %eax, %r14d
movl %r14d, %ecx
negl %ecx
cmovsl %r14d, %ecx
movl 0x2cc(%rbx), %eax
cmpl $0x1a, %ecx
jne 0xa43b
addl 0x2d0(%rbx), %eax
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x20(%rbx)
movl $0x3f800000, 0x2e4(%rbx) # imm = 0x3F800000
jmp 0xa461
xorps %xmm0, %xmm0
cvtsi2ss %eax, %xmm0
addl 0x2d0(%rbx), %eax
xorps %xmm1, %xmm1
cvtsi2ss %eax, %xmm1
movss 0x2e4(%rbx), %xmm2
callq 0x9d97
movss %xmm0, 0x20(%rbx)
leaq 0x2d4(%rbx), %r15
leaq 0x2d0(%rbx), %r14
movss 0x2e4(%rbx), %xmm0
ucomiss 0x8ecb6(%rip), %xmm0    # 0x99134
jb 0xa581
movl $0x0, 0x2e4(%rbx)
movl 0x2d4(%rbx), %edi
addl 0x2c8(%rbx), %edi
movl $0x15, %esi
callq 0x9f9d
movl %eax, 0x2c8(%rbx)
movl 0x2d0(%rbx), %edi
addl 0x2cc(%rbx), %edi
movl $0x1b, %esi
callq 0x9f9d
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 0xc3c4
cmpb $0x0, 0x2d9(%rbx)
jne 0xa581
movl 0x2c8(%rbx), %eax
cmpl 0x2ec(%rbx), %eax
jne 0xa581
movl 0x2cc(%rbx), %eax
cmpl 0x2f0(%rbx), %eax
jne 0xa581
leaq 0x8(%rbx), %r12
movb $0x1, 0x2d9(%rbx)
movl 0x2e0(%rbx), %esi
movq %r12, %rdi
callq 0xe6be
leaq -0x48(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x8ecee(%rip), %rsi     # 0x99238
leaq 0x8ecfe(%rip), %rdx     # 0x9924f
leaq -0x58(%rbp), %r13
movq %r13, %rdi
callq 0xa78c
movq %r12, %rdi
movq %r13, %rsi
callq 0xe554
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xa581
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x8880
cmpl $0x0, (%r15)
js 0xa593
je 0xa5dd
movss 0x8ec9b(%rip), %xmm0    # 0x9922c
jmp 0xa59b
movss 0x8ec8d(%rip), %xmm0    # 0x99228
movss %xmm0, -0x30(%rbp)
movss 0x3c(%rbx), %xmm1
movss %xmm1, -0x2c(%rbp)
callq 0x2f3b6
movaps %xmm0, %xmm2
mulss 0x8ec7a(%rip), %xmm2    # 0x99234
movss -0x2c(%rbp), %xmm0
movss -0x30(%rbp), %xmm1
callq 0x9d97
movss %xmm0, 0x3c(%rbx)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, (%r14)
js 0xa5ef
xorps %xmm0, %xmm0
jne 0xa59b
movss 0x3c(%rbx), %xmm0
jmp 0xa59b
movss 0x8ec39(%rip), %xmm0    # 0x99230
jmp 0xa59b
movq %rax, %rbx
movq -0x58(%rbp), %rdi
leaq -0x48(%rbp), %rax
cmpq %rax, %rdi
je 0xa615
movq -0x48(%rbp), %rsi
incq %rsi
callq 0x8880
movq %rbx, %rdi
callq 0x8ea0
nop