Newer
Older
Cactus-CleanArchitecture / app / src / main / java / org / ntlab / radishforandroidstudio / cactusClient / models / OwnPlayer.java
package org.ntlab.radishforandroidstudio.cactusClient.models;

import android.content.res.Resources;
import android.view.MotionEvent;

import org.ntlab.radishforandroidstudio.framework.animation.Animation3D;
import org.ntlab.radishforandroidstudio.framework.event.PadEvent;
import org.ntlab.radishforandroidstudio.framework.gameMain.OvergroundActor;
import org.ntlab.radishforandroidstudio.framework.listener.PadListener;
import org.ntlab.radishforandroidstudio.framework.model3D.ModelFactory;
import org.ntlab.radishforandroidstudio.framework.model3D.Object3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Position3D;
import org.ntlab.radishforandroidstudio.framework.model3D.Universe;
import org.ntlab.radishforandroidstudio.framework.physics.Velocity3D;
import org.ntlab.radishforandroidstudio.framework.view3D.Camera3D;
import org.ntlab.radishforandroidstudio.java3d.Appearance;
import org.ntlab.radishforandroidstudio.java3d.Material;
import org.ntlab.radishforandroidstudio.java3d.Vector3d;

public class OwnPlayer implements PadListener {
    private boolean isTouched = false;
    private float touchX = 0.0f;
    private float touchY = 0.0f;
    private OvergroundActor actor;
    private Camera3D camera;
    private Player player;

    public OwnPlayer(Player player, Resources resources, Universe universe, Camera3D camera) {
        this.player = player;

        // キャラクタの作成
        Appearance ap1 = new Appearance();
        Material m = new Material();
        m.setDiffuseColor(0.0f, 0.3f, 1.0f);
        m.setAmbientColor(0.0f, 0.0f, 0.0f);
        m.setEmissiveColor(0.0f, 0.0f, 0.0f);
        m.setSpecularColor(0.0f, 0.0f, 0.0f);
        m.setShininess(5.0f);
        ap1.setMaterial(m);
        Object3D pochaBody = null;
        try {
            pochaBody = ModelFactory.loadModel(resources, "pocha.stl", ap1).createObject();
            Animation3D pochaAnimation = null;    //AnimationFactory.loadAnimation("data\\pocha\\walk.wrl");
            actor = new OvergroundActor(pochaBody, pochaAnimation);
            actor.setPosition(new Position3D(0.0, 1.0, 0.0));
            universe.place(actor);
        } catch (Exception e) {
            e.printStackTrace();
        }

        this.camera = camera;
        camera.setViewPoint(actor.getPosition().add(0.0, 1.5, 0.0));
        camera.setViewLine(actor.getDirection());
        camera.setFieldOfView(1.5);
        camera.setBackClipDistance(10000.0);
    }

    public void updateCamera() {
        Vector3d charaVector3d = actor.getDirection();
        charaVector3d.normalize();//キャラの向きを単位ベクトルに
        camera.setViewPoint(actor.getPosition()
                .add(-5.0 * charaVector3d.getX(), charaVector3d.getY() + 5.0, -5.0 * charaVector3d.getZ()));//視点
        camera.setViewLine(new Vector3d(5.0 * charaVector3d.getX(), charaVector3d.getY() - 2.5, 5.0 * charaVector3d.getZ() + touchX));//視線
    }

    public void progress(long interval) {
        updateCamera();

        if (isTouched) {
            Velocity3D vel = actor.getVelocity();
            vel.setX(touchX);
            vel.setY(touchY);
            actor.setVelocity(vel);
        }
    }

    @Override
    public boolean onEvent(PadEvent event) {
        Vector3d charaVector3d = actor.getDirection();
        charaVector3d.normalize();//キャラの向きを単位ベクトルに
        MotionEvent motionEvent = event.getMotionEvent();

        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN || motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
            isTouched = true;
            touchX = (float) (Math.cos(event.getAngle()) * event.getLength());
            touchY = (float) (Math.sin(event.getAngle()) * event.getLength());


        } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
            Velocity3D vel = actor.getVelocity();
            vel.setX(0);
            vel.setY(0);
            actor.setVelocity(vel);
            isTouched = false;
        }
        return false;
    }
}