Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / gameComponents / Move.java
NoranekoFelician 2 days ago 2 KB ・Colliderコンポーネントの追加
package gameEngine.entites.gameComponents;

import gameEngine.Time;
import gameEngine.entites.GameObject;
import gameEngine.geometry.Transform;

public class Move extends GameComponent{

    private GameObject parent;
    private final float moveSpeed = 100;
    public enum Direction {UP, DOWN, LEFT, RIGHT}
    private final Direction direction = Direction.LEFT;
    public enum MoveType{
        Straight, Wave
    }
    private MoveType moveType = MoveType.Wave;

    public Move(GameObject parent){
        this.parent = parent;
    }
    @Override
    public GameComponent copy() {
        return this;
    }

    public void update() {
        if(moveType == MoveType.Straight) moveStraight();
        if(moveType == MoveType.Wave) moveWave();
    }

    private void moveStraight(){
        Transform transform = parent.transform;
        if (transform != null) {
            float x = transform.position.x;
            float y = transform.position.y;
            float z = transform.position.z;

            if (direction == Direction.UP) {
                transform.setPosition(x, y - moveSpeed * Time.deltaTime, z);
            }
            if (direction == Direction.LEFT) {
                transform.setPosition(x - moveSpeed * Time.deltaTime, y, z);
            }
            if (direction == Direction.DOWN) {
                transform.setPosition(x, y + moveSpeed * Time.deltaTime, z);
            }
            if (direction == Direction.RIGHT) {
                transform.setPosition(x + moveSpeed * Time.deltaTime, y, z);
            }
        }
    }

    private float waveAmplitude = 2f; // 振幅
    private float waveFrequency = 4.0f; // 周波数
    private float waveTime = 0.0f;

    private void moveWave(){
        Transform transform = parent.transform;
        if (transform != null) {
            float x = transform.position.x;
            float y = transform.position.y;
            float z = transform.position.z;

            waveTime += Time.deltaTime;

            if (direction == Direction.UP || direction == Direction.DOWN) {
                float baseY = direction == Direction.UP ? y - moveSpeed * Time.deltaTime : y + moveSpeed * Time.deltaTime;
                float offsetX = (float) Math.sin(waveTime * waveFrequency) * waveAmplitude;
                transform.setPosition(x + offsetX, baseY, z);
            }

            if (direction == Direction.LEFT || direction == Direction.RIGHT) {
                float baseX = direction == Direction.LEFT ? x - moveSpeed * Time.deltaTime : x + moveSpeed * Time.deltaTime;
                float offsetY = (float) Math.sin(waveTime * waveFrequency) * waveAmplitude;
                transform.setPosition(baseX, y + offsetY, z);
            }
        }
    }

}