From 1671aef06479e27ff0bcd8022fc712981b3428cb Mon Sep 17 00:00:00 2001 From: evancaplan Date: Sun, 15 Apr 2018 10:15:00 -0400 Subject: [PATCH 1/6] Updated model without key logic --- Model.java | 329 ++++++++++++++++++++++------------------------------- 1 file changed, 137 insertions(+), 192 deletions(-) diff --git a/Model.java b/Model.java index 3e5b433..91d62cf 100644 --- a/Model.java +++ b/Model.java @@ -1,208 +1,153 @@ -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; +class Model { + private int width, height, imageWidth, imageHeight; + private int x=0; + private int y=0; + private int xDir = 1; + private int yDir = 1; + 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; + private boolean isFire; -/** - * Model: Contains all the state and logic - * Does not contain anything about images or graphics, must ask view for that - * - * has methods to - * detect collision with boundaries - * decide next direction - * provide direction - * provide location - **/ - -class Model extends KeyAdapter{ - private int width, height, imageWidth, imageHeight; - private int x=0; - private int y=0; - private int xDir = 1; - private int yDir = 1; - 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; - private boolean isFire; - - Model(int width, int height, int imageWidth, int imageHeight){ - this.width = width; - this.height = height; - this.imageWidth = imageWidth; - this.imageHeight = imageHeight; - } - public int getX(){ - return this.x; - } - 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; - } - public boolean isMoving() { - return isMoving; - } - public void toggleMoving() { - this.isMoving = !this.isMoving; - } - public boolean isJumping() { - return isJumping; - } - public void toggleJumping() { - this.isJumping = !this.isJumping; - } - - // Arvin : added numerous getters and setters - public Direction getOrcDir() { - return this.orcDir; - } - public boolean getIsMoving() { - return this.isMoving; - } - public int getImageWidth(){ - return this.imageWidth; - } - public int getImageHeight() { - return this.imageHeight; - } - public int getXDir() { - return this.xDir; - } - public int getYDir() { - return this.yDir; - } - public void setOrcDir(Direction d) { - this.orcDir = d; - } - public void setXDir(int i) { - this.xDir *= i; - } - public void setYDir(int i) { - this.yDir *= i; - } - public void setX(int i) { - this.x += i; - } - public void setY(int i) { - this.y += i; - } - public int getXIncr() { - return this.xIncr; - } - public int getYIncr() { - return this.yIncr; - } - public boolean getIsJumping() { - return this.isJumping; - } - public int getWidth() { - return this.width; - } - public int getHeight() { - return this.height; - } - public boolean getIsFire() { - return isFire; - } - - //updates direction based on key pressed - @Override - public void keyPressed(KeyEvent e) { - - int key = e.getKeyCode(); - - if (key == KeyEvent.VK_LEFT) { - xDir = -1; + Model(int width, int height, int imageWidth, int imageHeight){ + this.width = width; + this.height = height; + this.imageWidth = imageWidth; + this.imageHeight = imageHeight; } - - if (key == KeyEvent.VK_RIGHT) { - xDir = 1; + public int getX(){ + return this.x; } - - if (key == KeyEvent.VK_UP) { - yDir = -1; + public int getY(){ + return this.y; } - - if (key == KeyEvent.VK_DOWN) { - yDir = 1; + public OrcImage getAction() { + return action; } - if (key == KeyEvent.VK_J) { - isJumping = true; + public void setAction(OrcImage action) { + this.action=action; } - if(key == KeyEvent.VK_F) { - isFire = true; + public int[] getDirect(){ + int[] dir = {this.xDir, this.yDir}; + return dir; } - } - - //In the future this can make the image stop moving when the keys are released - @Override - public void keyReleased(KeyEvent e) { - - int key = e.getKeyCode(); - - if (key == KeyEvent.VK_LEFT) { + public boolean isMoving() { + return isMoving; } - if (key == KeyEvent.VK_RIGHT) { + public void toggleMoving() { + this.isMoving = !this.isMoving; } - if (key == KeyEvent.VK_UP) { + public boolean isJumping() { + return isJumping; } - if (key == KeyEvent.VK_DOWN) { + public void toggleJumping() { + this.isJumping = !this.isJumping; } - } - - public void toggleFire() { - this.isFire = !this.isFire; - } - - /* - * All logic goes in ModelUpdateLogic class now - private int jumpStart=-1; - public void updateLocationAndDirection(int tick_counter){ // move logic out of model into controller - //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); - } - 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; + // Arvin : added numerous getters and setters + public Direction getOrcDir() { + return this.orcDir; + } + public boolean getIsMoving() { + return this.isMoving; + } + public int getImageWidth(){ + return this.imageWidth; + } + public int getImageHeight() { + return this.imageHeight; + } + public int getXDir() { + return this.xDir; + } + public int getYDir() { + return this.yDir; + } + public void setOrcDir(Direction d) { + this.orcDir = d; + } + public void setXDir(int i) { + this.xDir *= i; + } + public void setYDir(int i) { + this.yDir *= i; + } + public void setX(int i) { + this.x += i; + } + public void setY(int i) { + this.y += i; + } + public int getXIncr() { + return this.xIncr; + } + public int getYIncr() { + return this.yIncr; + } + public void setIsJumping(boolean jump){ + this.isJumping=jump; + } + public boolean getIsJumping() { + return this.isJumping; + } + public int getWidth() { + return this.width; + } + public int getHeight() { + return this.height; + } + public void setIsFiring(boolean fire){ + this.isFire=fire; + } + public boolean getIsFire() { + return isFire; + } - 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 - } + public void toggleFire() { + this.isFire = !this.isFire; } + /* + * All logic goes in ModelUpdateLogic class now + private int jumpStart=-1; + public void updateLocationAndDirection(int tick_counter){ // move logic out of model into controller + //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); + } + 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 + } + } + + } + */ } - */ -} \ No newline at end of file From 86b1d490d5be8e5fbd62956c7c6dd96ab6d84a50 Mon Sep 17 00:00:00 2001 From: evancaplan Date: Sun, 15 Apr 2018 10:16:34 -0400 Subject: [PATCH 2/6] Updated controller to take new KeyListener to add to view --- Controller.java | 84 ++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/Controller.java b/Controller.java index 75a1a67..4ca5f1e 100644 --- a/Controller.java +++ b/Controller.java @@ -15,49 +15,49 @@ **/ public class Controller implements ActionListener{ - private static Model model; - private static View view; - private final static int DRAWDELAY = 50; - private static boolean updateFlag= true; - JButton button = new JButton("Toggle"); - ModelUpdateLogic mul; // Arvin: needed to give the Controller a ModelUpdateLogic - - //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); - //button.addActionListener(new ButtonClickHandler(model)); + private static Model model; + private static View view; + private final static int DRAWDELAY = 50; + private static boolean updateFlag= true; + JButton button = new JButton("Toggle"); + ModelUpdateLogic mul; // Arvin: needed to give the Controller a ModelUpdateLogic - view = new View(button); - model = new Model(view.getWidth(), view.getHeight(), view.getImageWidth(), view.getImageHeight()); - view.button.addActionListener(new ButtonClickHandler(model)); - mul = new ModelUpdateLogic(model); // Arvin: create a new ModelUpdateLogic and give it the Model + //literally just a clock to count the game ticks + //increments every time the model and view are updated + private int tick_counter = 0; - Timer t = new Timer(DRAWDELAY, this); - t.start(); - } - - - //the timer calls this method after each DRAWDELAY - public void actionPerformed(ActionEvent e) { - //model.updateLocationAndDirection(tick_counter); - mul.updateLocationAndDirection(tick_counter); // Arvin: updates called by the ModelUpdateLogic class instead of the Model itself - view.update(model); - tick_counter+=1; - } - - public static void main(String[] args){ - EventQueue.invokeLater(new Runnable(){ - public void run(){ - - new Controller(); - view.addKeyListener(model); - view.setVisible(true); - } - }); + public Controller(){ + button.setSize(20,20); + //button.addActionListener(new ButtonClickHandler(model)); + + view = new View(button); + model = new Model(view.getWidth(), view.getHeight(), view.getImageWidth(), view.getImageHeight()); + view.button.addActionListener(new ButtonClickHandler(model)); + mul = new ModelUpdateLogic(model); // Arvin: create a new ModelUpdateLogic and give it the Model + + + Timer t = new Timer(DRAWDELAY, this); + t.start(); + } + + + //the timer calls this method after each DRAWDELAY + public void actionPerformed(ActionEvent e) { + //model.updateLocationAndDirection(tick_counter); + mul.updateLocationAndDirection(tick_counter); // Arvin: updates called by the ModelUpdateLogic class instead of the Model itself + view.update(model); + tick_counter+=1; + } + + public static void main(String[] args){ + EventQueue.invokeLater(new Runnable(){ + public void run(){ + + new Controller(); + view.addKeyListener(new KeyListener(model)); + view.setVisible(true); + } + }); + } } -} From d8aeb2891b018a7bf245835838aaeb376e52e341 Mon Sep 17 00:00:00 2001 From: evancaplan Date: Sun, 15 Apr 2018 10:17:50 -0400 Subject: [PATCH 3/6] Add files via upload --- KeyListener.java | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 KeyListener.java diff --git a/KeyListener.java b/KeyListener.java new file mode 100644 index 0000000..28394bb --- /dev/null +++ b/KeyListener.java @@ -0,0 +1,54 @@ +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; + +public class KeyListener extends KeyAdapter { + Model model; + public KeyListener(Model m){ + this.model=m; + } + @Override + public void keyPressed(KeyEvent e) { + + int key = e.getKeyCode(); + + if (key == KeyEvent.VK_LEFT) { + model.setXDir(-model.getXDir()); + } + + if (key == KeyEvent.VK_RIGHT) { + model.setXDir(model.getXDir()); + } + + if (key == KeyEvent.VK_UP) { + model.setYDir(-model.getYDir()); + } + + if (key == KeyEvent.VK_DOWN) { + model.setYDir(model.getYDir()); + } + if (key == KeyEvent.VK_J) { + model.setIsJumping(true); + } + if(key == KeyEvent.VK_F) { + model.setIsFiring(true); + } + } + + //In the future this can make the image stop moving when the keys are released + @Override + public void keyReleased(KeyEvent e) { + + int key = e.getKeyCode(); + + if (key == KeyEvent.VK_LEFT) { + } + if (key == KeyEvent.VK_RIGHT) { + } + if (key == KeyEvent.VK_UP) { + } + if (key == KeyEvent.VK_DOWN) { + } + } + + +} From 78e3839d14937469fa1502692a8327c4d28837de Mon Sep 17 00:00:00 2001 From: evancaplan Date: Mon, 16 Apr 2018 18:59:01 -0400 Subject: [PATCH 4/6] Fixed Mergeconflict --- Model.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Model.java b/Model.java index bdfbb6a..81ba57e 100644 --- a/Model.java +++ b/Model.java @@ -12,6 +12,12 @@ class Model { private Direction orcDir = Direction.SOUTHEAST; private boolean isFire; + } + Model(int width, int height, int imageWidth, int imageHeight){ + this.width = width; + this.height = height; + this.imageWidth = imageWidth; + this.imageHeight = imageHeight; } public int getX(){ return this.x; From 45f20e59ff68913763d6951ce2d853b1603193e2 Mon Sep 17 00:00:00 2001 From: evancaplan Date: Mon, 16 Apr 2018 19:01:43 -0400 Subject: [PATCH 5/6] Get rid of one curly boy --- Model.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model.java b/Model.java index 81ba57e..9aa74db 100644 --- a/Model.java +++ b/Model.java @@ -12,7 +12,7 @@ class Model { private Direction orcDir = Direction.SOUTHEAST; private boolean isFire; - } + Model(int width, int height, int imageWidth, int imageHeight){ this.width = width; this.height = height; From b0bf2f7e9f57c4aa710ba3bf08bc6755f1fdf7ff Mon Sep 17 00:00:00 2001 From: evancaplan Date: Mon, 16 Apr 2018 19:31:40 -0400 Subject: [PATCH 6/6] fix errors from merge --- Model.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Model.java b/Model.java index 9aa74db..82c4523 100644 --- a/Model.java +++ b/Model.java @@ -4,15 +4,14 @@ class Model { private int y=0; private int xDir = 1; private int yDir = 1; - private final int xIncr = 8; - private final int yIncr = 2; + private int xIncr = 8; + private int yIncr = 2; private boolean isMoving = true; private boolean isJumping = false; private OrcImage action = OrcImage.IDLE_S; private Direction orcDir = Direction.SOUTHEAST; private boolean isFire; - Model(int width, int height, int imageWidth, int imageHeight){ this.width = width; this.height = height; @@ -110,6 +109,12 @@ public boolean getIsFire() { public void toggleFire() { this.isFire = !this.isFire; } + public void setXIncr(int i){ + this.xIncr=i; + } + public void setYIncr(int i){ + this.yIncr=i; + } /* * All logic goes in ModelUpdateLogic class now