Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ public class Controller implements ActionListener{
private static boolean updateFlag= true;
JButton button = new JButton("Toggle");

//literally just a clock to count the game ticks
//increments every time the model and view are updated
private int tick_counter = 0;


public Controller(){
button.setSize(20,20);
Expand All @@ -42,9 +38,10 @@ public Controller(){

//the timer calls this method after each DRAWDELAY
public void actionPerformed(ActionEvent e) {
model.updateLocationAndDirection(tick_counter);
view.update(model);
tick_counter+=1;
if(updateFlag){
model.updateLocationAndDirection();
view.update(model);
}
}

public static void main(String[] args){
Expand Down
55 changes: 3 additions & 52 deletions Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ class Model extends KeyAdapter{
private final int xIncr = 8;
private final int yIncr = 2;
private boolean isMoving = true;
private boolean isJumping = false;
private OrcImage action = OrcImage.IDLE_S;
private Direction orcDir = Direction.SOUTHEAST;

Model(int width, int height, int imageWidth, int imageHeight){
this.width = width;
Expand All @@ -37,12 +34,6 @@ public int getX(){
public int getY(){
return this.y;
}
public OrcImage getAction() {
return action;
}
public void setAction(OrcImage action) {
this.action=action;
}
public int[] getDirect(){
int[] dir = {this.xDir, this.yDir};
return dir;
Expand All @@ -53,12 +44,6 @@ public boolean isMoving() {
public void toggleMoving() {
this.isMoving = !this.isMoving;
}
public boolean isJumping() {
return isJumping;
}
public void toggleJumping() {
this.isJumping = !this.isJumping;
}

//updates direction based on key pressed
@Override
Expand All @@ -81,9 +66,6 @@ public void keyPressed(KeyEvent e) {
if (key == KeyEvent.VK_DOWN) {
yDir = 1;
}
if (key == KeyEvent.VK_J) {
isJumping = true;
}
}

//In the future this can make the image stop moving when the keys are released
Expand All @@ -101,45 +83,14 @@ public void keyReleased(KeyEvent e) {
if (key == KeyEvent.VK_DOWN) {
}
}


private int jumpStart=-1;
public void updateLocationAndDirection(int tick_counter){
//if(!isMoving) return;
//jump action needs to pre-empt like everything else...

if(!this.isMoving) {
this.action=OrcImage.idle(this.orcDir);
return;
} else {
this.action = OrcImage.forward(this.orcDir);
}

public void updateLocationAndDirection(){
if(!isMoving) return;
if((this.getX()+this.imageWidth>this.width) || this.getX()<0)
this.xDir *= -1;
if((this.getY()+this.imageHeight>this.height) || this.getY()<0)
this.yDir *= -1;
this.x+=xIncr*this.xDir;
this.y+=yIncr*this.yDir;


if(this.xDir>0 && this.yDir>0) //x+,y+: d+r
this.orcDir=Direction.SOUTHEAST;
else if(this.xDir>0 && this.yDir<0)//x+,y-: u+r
this.orcDir=Direction.NORTHEAST;
else if(this.xDir<0 && this.yDir>0)//x-,y+: d+l
this.orcDir=Direction.SOUTHWEST;
else if(this.xDir<0 && this.yDir<0)//x-,y-: u+l
this.orcDir=Direction.NORTHWEST;

if(isJumping) {
this.action = OrcImage.jump(this.orcDir);
if(jumpStart<0) //start the animation timer
jumpStart=tick_counter;
if(tick_counter >= jumpStart+this.action.frameCount()) { //if enough time has passed for a jump
this.toggleJumping(); //toggle the animation flag
jumpStart=-1; // reset the animation timer
}
}

}
}
52 changes: 0 additions & 52 deletions OrcImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,56 +66,4 @@ public String path() { //returns the path to the respective orc image
public int frameCount() {
return this.frameCount;
}
public static OrcImage forward(Direction dir) {
switch(dir) {
case NORTH: return OrcImage.FORWARD_N;
case NORTHEAST: return OrcImage.FORWARD_NE;
case EAST: return OrcImage.FORWARD_E;
case SOUTHEAST: return OrcImage.FORWARD_SE;
case SOUTH: return OrcImage.FORWARD_S;
case SOUTHWEST: return OrcImage.FORWARD_SW;
case WEST: return OrcImage.FORWARD_W;
case NORTHWEST: return OrcImage.FORWARD_NW;
default: return OrcImage.FORWARD_S;
}
}
public static OrcImage idle(Direction dir) {
switch(dir) {
case NORTH: return OrcImage.IDLE_N;
case NORTHEAST: return OrcImage.IDLE_NE;
case EAST: return OrcImage.IDLE_E;
case SOUTHEAST: return OrcImage.IDLE_SE;
case SOUTH: return OrcImage.IDLE_S;
case SOUTHWEST: return OrcImage.IDLE_SW;
case WEST: return OrcImage.IDLE_W;
case NORTHWEST: return OrcImage.IDLE_NW;
default: return OrcImage.IDLE_S;
}
}
public static OrcImage jump(Direction dir) {
switch(dir) {
case NORTH: return OrcImage.JUMP_N;
case NORTHEAST: return OrcImage.JUMP_NE;
case EAST: return OrcImage.JUMP_E;
case SOUTHEAST: return OrcImage.JUMP_SE;
case SOUTH: return OrcImage.JUMP_S;
case SOUTHWEST: return OrcImage.JUMP_SW;
case WEST: return OrcImage.JUMP_W;
case NORTHWEST: return OrcImage.JUMP_NW;
default: return OrcImage.JUMP_S;
}
}
public static OrcImage fire(Direction dir) {
switch(dir) {
case NORTH: return OrcImage.FIRE_N;
case NORTHEAST: return OrcImage.FIRE_NE;
case EAST: return OrcImage.FIRE_E;
case SOUTHEAST: return OrcImage.FIRE_SE;
case SOUTH: return OrcImage.FIRE_S;
case SOUTHWEST: return OrcImage.FIRE_SW;
case WEST: return OrcImage.FIRE_W;
case NORTHWEST: return OrcImage.FIRE_NW;
default: return OrcImage.FIRE_S;
}
}
}
29 changes: 23 additions & 6 deletions View.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class View extends JPanel {


private OrcImage action;
private OrcImage prevAction;
private static HashMap<OrcImage, BufferedImage[]> images;

private int x, y, xDir, yDir; //need global state attribute information for the repaint method
Expand Down Expand Up @@ -93,20 +92,38 @@ public int getImageHeight(){

public void update(Model model){
this.removeAll();
this.action = model.getAction();
//this.frame.getGraphics().clearRect(0, 0, (int)this.getSize().getWidth(), (int)this.getSize().getHeight());
this.x = model.getX();
this.y = model.getY();
this.xDir = model.getDirect()[0];
this.yDir = model.getDirect()[1];

if(this.action!=this.prevAction && this.picNum!=0) { //if there is a new action to perform
picNum=0;//reset the animation
}

if(this.xDir>0 && this.yDir>0) //x+,y+: d+r
this.action=OrcImage.FORWARD_SE;
else if(this.xDir>0 && this.yDir<0)//x+,y-: u+r
this.action=OrcImage.FORWARD_NE;
else if(this.xDir<0 && this.yDir>0)//x-,y+: d+l
this.action=OrcImage.FORWARD_SW;
else if(this.xDir<0 && this.yDir<0)//x-,y-: u+l
this.action=OrcImage.FORWARD_NW;

if(!model.isMoving())
switch(this.action) {
case FORWARD_SE: this.action=OrcImage.IDLE_SE; break;
case FORWARD_NE: this.action=OrcImage.IDLE_NE; break;
case FORWARD_SW: this.action=OrcImage.IDLE_SW; break;
case FORWARD_NW: this.action=OrcImage.IDLE_NW; break;
}

// System.out.println("ACTION SHOULD BE SET HERE: "+this.action);
// System.out.println(this.picNum+","+this.action);

setBackground(Color.gray);
this.picNum = (this.picNum + 1) % this.action.frameCount();
this.frame.getGraphics().drawImage(this.images.get(this.action)[this.picNum],this.x,this.y,Color.gray, this);
this.add(this.button);
this.button.setVisible(true);
this.button.repaint();
this.prevAction = this.action;
}
}