#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec2 aTexCoord;
layout(location = 2) in int aTexIndex;

out vec2 TexCoord;

flat out int TexIndex;
flat out float GameTime;

uniform mat4 ProjMatrix;
uniform float u_time;
uniform int u_ismoving;

const float bobbingSpeed = 18.0; // Speed of the bobbing effect
const float bobbingIntensity = 0.6; // Intensity of the bobbing effect

void main()
{
	vec3 position = aPos;
	if (u_ismoving == 1 && aTexIndex == 1) {
		 // Calculate bobbing offset based on sine waves
        float verticalBob = sin(u_time * bobbingSpeed) * bobbingIntensity;
        float horizontalBob = sin(u_time * bobbingSpeed * 0.5) * bobbingIntensity * 0.3;
        
        // Apply the bobbing offset
        position.y += verticalBob;
        position.x += horizontalBob;
        
        // Optional: Add slight rotation for more realistic head movement
        float tiltAngle = sin(u_time * bobbingSpeed * 0.7) * bobbingIntensity * 0.02;
        
        // Apply rotation around Z-axis (head tilt)
        float cosA = cos(tiltAngle);
        float sinA = sin(tiltAngle);
        
        float newX = position.x * cosA - position.y * sinA;
        float newY = position.x * sinA + position.y * cosA;
        
        position.x = newX;
        position.y = newY;
	}
	GameTime = u_time;
	gl_Position = ProjMatrix * vec4(position, 1.0);
	TexCoord = aTexCoord;
	TexIndex = aTexIndex;
}