// File: /workspace/github_cpp_makefile_star2_O2/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 $0x48, %rsp
cmpb $0x0, 0xcee0e(%rip)     # 0xd8e48
jne 0xa37e
movq %rdi, %rbx
callq 0xd50c
movl %eax, -0x2c(%rbp)
callq 0xd518
movl %eax, %r15d
callq 0xd524
movl %eax, %r12d
callq 0xd530
movl %eax, %r13d
callq 0x2e7fe
addss 0x2e8(%rbx), %xmm0
movss %xmm0, 0x2e8(%rbx)
ucomiss 0x8e1a1(%rip), %xmm0    # 0x98220
jbe 0xa088
movb $0x1, 0x2d8(%rbx)
xorps %xmm1, %xmm1
ucomiss %xmm0, %xmm1
jb 0xa097
movb $0x0, 0x2d8(%rbx)
cmpb $0x1, 0x2d9(%rbx)
jne 0xa0cb
leaq 0x8(%rbx), %rdi
cmpb $0x1, 0xced9e(%rip)     # 0xd8e49
jne 0xa0c0
movl $0xffff7ac8, %esi       # imm = 0xFFFF7AC8
callq 0xde22
andl $0x0, 0x2e8(%rbx)
jmp 0xa0cb
movl 0x2e0(%rbx), %esi
callq 0xde22
movq (%rbx), %rax
movl %r13d, (%rsp)
leaq -0x3c(%rbp), %r13
leaq -0x38(%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 0xa125
callq 0xbefc
cmpl $0x1, %eax
je 0xa125
movl -0x3c(%rbp), %eax
movl %eax, 0x2ec(%rbx)
movl -0x38(%rbp), %eax
movl %eax, 0x2f0(%rbx)
movb 0x2d9(%rbx), %al
testb %al, 0xced18(%rip)      # 0xd8e49
jne 0xa13d
movss 0x8e0c9(%rip), %xmm0    # 0x98204
jmp 0xa145
movss 0x8e0df(%rip), %xmm0    # 0x98224
movss %xmm0, 0x2dc(%rbx)
callq 0x2e7fe
mulss 0x2dc(%rbx), %xmm0
addss 0x2e4(%rbx), %xmm0
movss %xmm0, 0x2e4(%rbx)
movl 0x2c8(%rbx), %r14d
movl 0x2d4(%rbx), %edi
addl %r14d, %edi
pushq $0x15
popq %rsi
callq 0x9e1a
subl %eax, %r14d
movl %r14d, %ecx
negl %ecx
cmovsl %r14d, %ecx
movl 0x2c8(%rbx), %eax
cmpl $0x14, %ecx
jne 0xa1af
addl 0x2d4(%rbx), %eax
cvtsi2ss %eax, %xmm0
movl $0x3f800000, 0x2e4(%rbx) # imm = 0x3F800000
jmp 0xa1ca
cvtsi2ss %eax, %xmm0
addl 0x2d4(%rbx), %eax
cvtsi2ss %eax, %xmm1
movss 0x2e4(%rbx), %xmm2
callq 0x9c37
leaq 0x2d4(%rbx), %r15
movss %xmm0, 0x18(%rbx)
movl 0x2cc(%rbx), %r12d
leaq 0x2d0(%rbx), %r14
movl 0x2d0(%rbx), %edi
addl %r12d, %edi
pushq $0x1b
popq %rsi
callq 0x9e1a
subl %eax, %r12d
movl %r12d, %ecx
negl %ecx
cmovsl %r12d, %ecx
movl 0x2cc(%rbx), %eax
cmpl $0x1a, %ecx
jne 0xa227
addl 0x2d0(%rbx), %eax
cvtsi2ss %eax, %xmm0
movss %xmm0, 0x20(%rbx)
movl $0x3f800000, 0x2e4(%rbx) # imm = 0x3F800000
jmp 0xa25c
cvtsi2ss %eax, %xmm0
addl 0x2d0(%rbx), %eax
cvtsi2ss %eax, %xmm1
movss 0x2e4(%rbx), %xmm2
callq 0x9c37
movss %xmm0, 0x20(%rbx)
movss 0x2e4(%rbx), %xmm0
ucomiss 0x8dede(%rip), %xmm0    # 0x98134
jb 0xa331
andl $0x0, 0x2e4(%rbx)
movl 0x2d4(%rbx), %edi
addl 0x2c8(%rbx), %edi
pushq $0x15
popq %rsi
callq 0x9e1a
movl %eax, 0x2c8(%rbx)
movl 0x2d0(%rbx), %edi
addl 0x2cc(%rbx), %edi
pushq $0x1b
popq %rsi
callq 0x9e1a
movl %eax, 0x2cc(%rbx)
movl 0x2c8(%rbx), %edi
cvtsi2ss %edi, %xmm0
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 0xbe0a
cmpb $0x0, 0x2d9(%rbx)
jne 0xa331
movl 0x2c8(%rbx), %eax
cmpl 0x2ec(%rbx), %eax
jne 0xa331
movl 0x2cc(%rbx), %eax
cmpl 0x2f0(%rbx), %eax
jne 0xa331
leaq 0x8(%rbx), %r12
movb $0x1, 0x2d9(%rbx)
movl 0x2e0(%rbx), %esi
movq %r12, %rdi
callq 0xde22
leaq 0x8df2b(%rip), %rsi     # 0x98238
leaq -0x60(%rbp), %r13
leaq -0x2d(%rbp), %rdx
movq %r13, %rdi
callq 0x8310
movq %r12, %rdi
movq %r13, %rsi
callq 0xdce4
leaq -0x60(%rbp), %rdi
callq 0x83a0
cmpl $0x0, (%r15)
js 0xa343
je 0xa38d
movss 0x8deeb(%rip), %xmm0    # 0x9822c
jmp 0xa34b
movss 0x8dedd(%rip), %xmm0    # 0x98228
movss %xmm0, -0x34(%rbp)
movss 0x3c(%rbx), %xmm1
movss %xmm1, -0x2c(%rbp)
callq 0x2e7fe
movaps %xmm0, %xmm2
mulss 0x8deca(%rip), %xmm2    # 0x98234
movss -0x2c(%rbp), %xmm0
movss -0x34(%rbp), %xmm1
callq 0x9c37
movss %xmm0, 0x3c(%rbx)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x0, (%r14)
js 0xa39f
xorps %xmm0, %xmm0
jne 0xa34b
movss 0x3c(%rbx), %xmm0
jmp 0xa34b
movss 0x8de89(%rip), %xmm0    # 0x98230
jmp 0xa34b
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x83a0
movq %rbx, %rdi
callq 0x8ed0
nop