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
84 changes: 42 additions & 42 deletions Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
}
}
54 changes: 54 additions & 0 deletions KeyListener.java
Original file line number Diff line number Diff line change
@@ -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) {
}
}


}
Loading