Old Style Game Framework How To..
Creating a Game with Old Style Game Framework.
This walk through create a side scroller game, where the player runs across
the screen and jumps over obstacles. The walk through uses the Old
Style Game Framework (1.0.2), Game Doc (1.2.3)
and Game Designer (1.2.2).
Updated versions: Old Style Framework (1.1), Game Doc (1.3) and Game Designer (1.3)
|
001 package org.deken.sidescroller.movement;
002
003 import org.deken.game.input.InputAction;
004 import org.deken.game.movement.GameVector;
005 import org.deken.game.movement.TwoKeyMovement;
006 import org.deken.game.movement.actions.KeyDirectionAction;
007 import org.deken.game.sound.Sound;
008 import org.deken.game.sprites.Actor;
009 import org.deken.sidescroller.sprites.Jumper;
010
011 /**
012 * <p>
013 * Title: JumpMovement</p>
014 *
015 * <p>
016 * Description: </p>
017 *
018 * Copyright: Copyright (c) Aug 3, 2014
019 *
020 * @author Gary Deken <gary@abberkeep.us>
021 * @version 1.0
022 */
023 public class JumpMovement extends TwoKeyMovement {
024 private static final int JUMPING_HIEGHT = 100;
025 private boolean jumping = false;
026 private boolean falling = false;
027 private int startY = 0;
028 private int maxY = 0;
029 private Jumper player;
030 private Sound jumpSound;
031
032 public JumpMovement(int rightKeyID, int leftKeyID, int jumpKeyID, Jumper actor) {
033 keyIDs = new int[3];
034 keyIDs[0] = rightKeyID;
035 keyIDs[1] = leftKeyID;
036 keyIDs[2] = jumpKeyID;
037 keyDirectionActions = new KeyDirectionAction[3];
038 keyDirectionActions[0] = new KeyDirectionAction(rightKeyID, GameVector.EAST);
039 keyDirectionActions[1] = new KeyDirectionAction(leftKeyID, GameVector.WEST);
040 keyDirectionActions[2] = new KeyDirectionAction(jumpKeyID, GameVector.NORTH);
041 player = actor;
042 }
043
044 /**
045 * Adjust the horizontal (x axis) movement to a collision.
046 */
047 @Override
048 public void collideHorizontal() {
049 }
050
051 @Override
052 public void collideVertical() {
053 if (gameVector.isSouthward()) {
054 // falling
055 falling = false;
056 gameVector.setYMagnitude(0);
057 }
058 else if (gameVector.isNorthward()) {
059 // jumping
060 jumping = false;
061 falling = true;
062 gameVector.setYMagnitude(speed);
063 }
064 }
065
066 @Override
067 public TwoKeyMovement copy(Actor actor) {
068 JumpMovement newMovement = new JumpMovement(this.keyIDs[0], this.keyIDs[1], this.keyIDs[2], (Jumper) actor);
069
070 newMovement.gameVector = this.gameVector.copy();
071 newMovement.keyDirectionActions[0].setInputAction(keyDirectionActions[0].getInputAction());
072 newMovement.keyDirectionActions[1].setInputAction(keyDirectionActions[1].getInputAction());
073 newMovement.keyDirectionActions[2].setInputAction(keyDirectionActions[2].getInputAction());
074 newMovement.currentXMovement = this.currentXMovement;
075 newMovement.currentYMovement = this.currentYMovement;
076 newMovement.speed = this.speed;
077
078 return newMovement;
079 }
080
081 public boolean isAirBorne() {
082 return falling || jumping;
083 }
084
085 public boolean isFalling() {
086 return falling;
087 }
088
089 public boolean isJumping() {
090 return jumping;
091 }
092
093 @Override
094 public void notifyListener(InputAction inputAction) {
095 if (keyDirectionActions[2].getInputAction().getId() == inputAction.getId()
096 && !isAirBorne()) {
097 jumping = true;
098 startY = (int) player.getLocation().getY();
099 maxY = startY - JUMPING_HIEGHT;
100 gameVector.setYMagnitude(-speed);
101 jumpSound.play();
102 }
103 }
104
105 public void setFalling(boolean falling) {
106 this.falling = falling;
107 if (falling) {
108 gameVector.setYMagnitude(speed);
109 }
110 }
111
112 public void setJumpSound(Sound jumpSound) {
113 this.jumpSound = jumpSound;
114 }
115
116 @Override
117 public void update(long elapseTime) {
118 super.update(elapseTime);
119 if (isAirBorne()) {
120 if (jumping) {
121 // if player is jumping, check for max jump height.
122 int playerY = (int) player.getLocation().y;
123 if (playerY < maxY) {
124 // Maximum jump height
125 jumping = false;
126 falling = true;
127 gameVector.setYMagnitude(speed);
128 }
129 }
130 }
131 }
132
133 }
|