Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animation system #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
94 changes: 94 additions & 0 deletions AnimationHandeler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import java.util.ArrayList;
import javax.swing.ImageIcon;
import java.awt.Graphics2D;
import java.awt.Image;

public class AnimationHandeler {
static int frame = 0;
static int numFrames = 10;
static int scalingFactor = 100;

static ArrayList<Integer[]> queue = new ArrayList<Integer[]>();
static ArrayList<ImageIcon[]> imageLookup = new ArrayList<ImageIcon[]>();

static Board board;
static LevelSelect levelSelect;
static Map map;

static boolean canvas = false;

public static void queueAnimation(int EventKey, int x, int y) {
Integer[] temp = {EventKey, x, y};
queue.add(temp);
}

public static void animate(Graphics2D g2D) {
for(Integer[] key : queue) {
switch (key[0]) {
case 0:
drawGame(g2D, key);
canvas = true;
break;
}
}

if (queue.size() > 0) {
if(frame >= numFrames) {
frame = 0;
map.map_move = new boolean[map.map.length][map.map[0].length];

Map.__mvntCache__ = 4;
queue.clear();

}
else if (canvas) {
frame++;

try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
board.repaint();
}
}

//debug
System.out.println("bruh2");
}

public static void drawGame(Graphics2D graphics, Integer[] key) {
int c = key[1];
int r = key[2];
Image img;

char type = map.map[r/board.scalingFactor][c/board.scalingFactor];

switch (type) {
case 'p':
img = Player.getImage();
break;
default:
img = Block.getImage(type);
}

c -= (scalingFactor - frame * (scalingFactor/numFrames)) * Map.dc[Map.direct];
r -= (scalingFactor - frame * (scalingFactor/numFrames)) * Map.dr[Map.direct];

graphics.drawImage(img, c, r, null);
// board.remove(GameFrame.settingsButton);
GameFrame.settingsButton.requestFocus();
if (frame >= numFrames) {
board.isAnimate = false;
// frame = 0;

}

//debug
// System.out.println("bruh3");
}

public static void setMap(Map m) { map = m; }
public static void setBoard(Board b) { board = b; }
public static void setLevelSelect(LevelSelect ls) { levelSelect = ls; }
}
Binary file removed Assets/end.png~
Binary file not shown.
Binary file added Assets/girdsquare.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/grid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/levelSelect_ButtonL.kra
Binary file not shown.
Binary file added Assets/levelSelect_ButtonL.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/levelSelect_ButtonR.kra
Binary file not shown.
Binary file added Assets/levelSelect_ButtonR.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/levelSelect_background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/levelSelect_layout.kra
Binary file not shown.
Binary file added Assets/levelSelect_layout.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/levelSelect_layout2.kra
Binary file not shown.
Binary file added Assets/levelSelect_layout2.kra~
Binary file not shown.
Binary file added Assets/levelSelect_layout2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Assets/settingsButton.kra
Binary file not shown.
Binary file added Assets/star.kra
Binary file not shown.
Binary file added Assets/star.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

public class Block {

ImageIcon bBasic = new ImageIcon("Assets\\basicBlock_default.png");
ImageIcon bImmovable = new ImageIcon("Assets\\Immovable.default.png");
ImageIcon bDead = new ImageIcon("Assets\\dead.png");
ImageIcon bChainE = new ImageIcon("Assets\\chainE.png");
ImageIcon bChainH = new ImageIcon("Assets\\chainH.png");
ImageIcon bDirectLR = new ImageIcon("Assets\\directLR.png");
ImageIcon bDirectUD = new ImageIcon("Assets\\directUD.png");
ImageIcon bMoveLim2 = new ImageIcon("Assets\\moveLim2.png");
static ImageIcon bBasic = new ImageIcon("Assets\\basicBlock_default.png");
static ImageIcon bImmovable = new ImageIcon("Assets\\Immovable.default.png");
static ImageIcon bDead = new ImageIcon("Assets\\dead.png");
static ImageIcon bChainE = new ImageIcon("Assets\\chainE.png");
static ImageIcon bChainH = new ImageIcon("Assets\\chainH.png");
static ImageIcon bDirectLR = new ImageIcon("Assets\\directLR.png");
static ImageIcon bDirectUD = new ImageIcon("Assets\\directUD.png");
static ImageIcon bMoveLim2 = new ImageIcon("Assets\\moveLim2.png");

static Map map;

Expand Down Expand Up @@ -169,7 +169,7 @@ private static boolean canMoveChainH(int direct, int row, int col) {
*
* @return Image returns correct sprites for the block type.
*/
public Image getImage(char type) {
static public Image getImage(char type) {
switch(type) {
case 'u':
return bBasic.getImage();
Expand Down
130 changes: 63 additions & 67 deletions Board.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import java.awt.*;
import javax.swing.*;

import java.awt.event.*;

public class Board extends JPanel{
Expand Down Expand Up @@ -28,6 +27,8 @@ public class Board extends JPanel{
Block block;

/* variables */
Graphics2D g2D;

boolean isAnimate = false;
boolean moveFlag = false;
boolean isValid = false;
Expand All @@ -51,24 +52,31 @@ public class Board extends JPanel{
Map map;
Timer t;

public Board() {
this.map = new Map(1);
public Board(int level) {
// this.setLayout(null);
this.map = new Map(level);

player = new Player(map, map.loadPlayer(map.map));
block = new Block(map);
score = new JLabel();

this.add(player);

setFocusable(true);
requestFocus();
AnimationHandeler.setBoard(this);
AnimationHandeler.setMap(this.map);

// this.setBa
this.setFocusable(true);
this.requestFocus();

ImageIcon board = new ImageIcon("Assets\\test.png");
ImageIcon background = new ImageIcon("Assets\\background.png");
ImageIcon end = new ImageIcon("Assets\\end.png");
this.board = board.getImage();
bg = background.getImage();
this.end = end.getImage();
this.board = board.getImage().getScaledInstance(900, 500, Image.SCALE_DEFAULT);
this.bg = background.getImage().getScaledInstance(1300, 900, Image.SCALE_DEFAULT);
this.end = end.getImage().getScaledInstance(1300, 900, Image.SCALE_DEFAULT);


// t = new Timer(5, this);
// t.start();

Expand Down Expand Up @@ -100,87 +108,74 @@ public Board() {
* @param g
*/
public void paint(Graphics g) {

super.paint(g);
Graphics2D g2D = (Graphics2D) g;
g2D = (Graphics2D) g;
g2D.setFont(new Font("TimesRoman", Font.PLAIN, 20));

g2D.drawImage(bg, 0, 0, null);
g2D.drawImage(board, 100, 100, null);

for (int r = 0; r < map.map.length; r++) {
for (int c = 0; c < map.map[r].length; c++) {
// debug.print(r + " " + c);
if (!(r == 0 || c == 0 || r == map.map.length - 1 || c == map.map[0].length - 1))
g2D.drawImage(map.tile.getImage(), c*scalingFactor, r*scalingFactor, null);
switch(map.map[r][c]) {
case 'p':
if (map.map_move[r][c])
// try {
animate(player.getImage(), c*scalingFactor, r*scalingFactor, g2D);
// } catch (InterruptedException e1) {
// e1.printStackTrace();
// }
AnimationHandeler.queueAnimation(0, c*scalingFactor, r*scalingFactor);
else
g2D.drawImage(player.getImage(), c*scalingFactor, r*scalingFactor, null);
g2D.drawImage(Player.getImage(), c*scalingFactor, r*scalingFactor, null);
break;

case ' ':
// debug.print("a");
// g2D.drawImage(map.tile.getImage(), c*scalingFactor, r*scalingFactor, null); //why no print if called
break;

default:
if (map.map_move[r][c])
// try {
animate(block.getImage(map.map[r][c]), c*scalingFactor, r*scalingFactor, g2D);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
AnimationHandeler.queueAnimation(0, c*scalingFactor, r*scalingFactor);
else
g2D.drawImage(block.getImage(map.map[r][c]), c*scalingFactor, r*scalingFactor, null);
g2D.drawImage(Block.getImage(map.map[r][c]), c*scalingFactor, r*scalingFactor, null);
break;
}
}


if (isAnimate) {
frameNum++;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}

repaint();
}

starCount = getStarCount();
}
}

AnimationHandeler.animate(g2D);

if (starCount == 0) {
isLose = true;
}
starCount = getStarCount();

if (Map.blockCount == 0) {
isWin = true;
}

if (isWin) {
float alpha = 1 * 0.1f;
AlphaComposite alcom = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha);
g2D.setComposite(alcom);
g2D.drawImage(end, 0, 0, null);

} else if (isWin) {
float alpha = 1 * 0.1f;
AlphaComposite alcom = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha);
g2D.setComposite(alcom);
g2D.drawImage(end, 0, 0, null);

if (starCount == 0) {
isLose = true;
}

} else {
g2D.drawString("Num moves: " + moveCount + " Num stars: " + starCount, 900, 620);
}
if (Map.blockCount == 0) {
isWin = true;
}

if (isWin) {
float alpha = 1 * 0.5f;
AlphaComposite alcom = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha);

g2D.setComposite(alcom);
g2D.drawImage(end, 0, 0, null);
// GameFrame.buttons[4].setVisible(true);

} else if (isLose) {

float alpha = 1 * 0.5f;
AlphaComposite alcom = AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha);

g2D.setComposite(alcom);
g2D.drawImage(end, 0, 0, null);

} else {
g2D.drawString("Num moves: " + moveCount + " Num stars: " + starCount, 900, 620);
}

//debug
// debug.print("here");
}

public void animate(Image img, int c, int r, Graphics2D graphics) {
Expand All @@ -197,6 +192,9 @@ public void animate(Image img, int c, int r, Graphics2D graphics) {
frameNum = 0;

}

//debug
debug.print("bruh");
}

public int getStarCount() {
Expand Down Expand Up @@ -257,6 +255,4 @@ else if (!isRelease && !isAnimate) {
}

}


}
14 changes: 14 additions & 0 deletions CanvasHandeler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.ArrayList;
import javax.swing.ImageIcon;

class CanvasHandeler {
static ArrayList<Integer[]> queue = new ArrayList<Integer[]>();
static ArrayList<ImageIcon[]> imageLookup = new ArrayList<ImageIcon[]>();

public static void queueObj(int imageKey, int x, int y) {
Integer[] temp = {imageKey, x, y};
queue.add(temp);
}


}
Loading