Creating Custom Components
PlayerMovement.java
public class PlayerMovement extends Component {
public float movementSpeed = 0.5f;
public PlayerMovement(float movementSpeed) {
this.name = "Player Movement";
this.movementSpeed = movementSpeed;
}
@Override
public void Update() {
if(gameObject == null)
return;
// Left key
if(Input.isKeyPressed(37)) {
gameObject.transform.translate(Vector2.left);
}
// Right key
if(Input.isKeyPressed(39)) {
gameObject.transform.translate(Vector2.right);
}
// Up key
if(Input.isKeyPressed(38)) {
gameObject.transform.translate(Vector2.up);
}
// Down key
if(Input.isKeyPressed(40)) {
gameObject.transform.translate(Vector2.down);
}
}
@Override
public void Render(Graphics g) {
}
}
SceneNameHere.java
public class SceneNameHere extends Scene {
private GameObject player = new GameObject(this, "Player");
public SceneNameHere(Game game) {
super(game);
}
@Override
public void Initialize() {
player.setScale(new Vector2(32, 32));
player.addComponent(new PlayerMovement(0.5f));
player.addComponent(new MeshRenderer());
}
@Override
public void Update() {
}
@Override
public void Render(Graphics g) {
}
}
Last updated