about summary refs log blame commit diff stats
path: root/html/rogue/js/camera.js
blob: aaeeea9aaf4a78ad13f2b58e7a32954da66ccd72 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                                        

                                               
        


                                                
                                             


                                         
                                                                        








                                                                                                         

     
const Camera = {
    x: 0,
    y: 0,
    
    centerOn(hex) {
        const pixelCoord = HexGrid.toPixel(hex);
        this.x = pixelCoord.x - state.canvas.width / 2;
        this.y = pixelCoord.y - state.canvas.height / 2;
    },

    smoothFollow(target) {
        const targetPixel = HexGrid.toPixel(target);
        const screenX = targetPixel.x - this.x;
        const screenY = targetPixel.y - this.y;
        
        const centerX = state.canvas.width / 2;
        const centerY = state.canvas.height / 2;
        
        // Distance from center of the screen
        const deltaX = screenX - centerX;
        const deltaY = screenY - centerY;
        
        // Only move the camera if the player is outside of the deadzone
        if (Math.abs(deltaX) > Config.camera.DEADZONE_X) {
            const adjustX = deltaX - (deltaX > 0 ? Config.camera.DEADZONE_X : -Config.camera.DEADZONE_X);
            this.x += adjustX * Config.camera.FOLLOW_SPEED;
        }
        
        if (Math.abs(deltaY) > Config.camera.DEADZONE_Y) {
            const adjustY = deltaY - (deltaY > 0 ? Config.camera.DEADZONE_Y : -Config.camera.DEADZONE_Y);
            this.y += adjustY * Config.camera.FOLLOW_SPEED;
        }
    }
};