com.dongbat.invasion.util.PlayerInputUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.dongbat.invasion.util.PlayerInputUtil.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dongbat.invasion.util;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

/**
 *
 * @author password
 */
public class PlayerInputUtil {

    public static Vector2 aimVector;
    public static Vector2 fireVector;
    private static final OrthographicCamera camera = PhysicsCameraUtil.getCamera();

    public static void init() {
        Gdx.input.setInputProcessor(new InputAdapter() {

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                aim(screenX, screenY);
                return true;
            }

            @Override
            public boolean touchUp(int screenX, int screenY, int pointer, int button) {
                fireVector = new Vector2(aimVector);
                aimVector = null;
                return true;
            }

            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                aim(screenX, screenY);
                return true;
            }

            private void aim(int x, int y) {
                Vector3 v = new Vector3(x, y, 0);
                v = camera.unproject(v);
                aimVector = new Vector2(v.x, v.y);
                if (aimVector.len2() >= 400) {
                    aimVector.nor().scl(20);
                }
            }
        });
    }
}