Implement object system

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 3 years ago
parent 868d5ac87d
commit daf471db93
  1. 32
      src/sig/DrawLoop.java
  2. 15
      src/sig/RabiClone.java
  3. 8
      src/sig/engine/Object.java
  4. 2
      src/sig/engine/Panel.java
  5. 15
      src/sig/objects/Player.java

@ -24,8 +24,38 @@ public class DrawLoop {
int[] p = panel.pixel;
for(int X=0;X<sprite.getHeight();X++){
for(int Y=0;Y<sprite.getWidth();Y++){
p[(Y+(int)y)*panel.getWidth()+X+(int)x] = sprite.getBi_array()[Y*sprite.getWidth()+X];
int index = (Y+(int)y)*panel.getWidth()+X+(int)x;
if (index<0||index>=p.length) {
continue;
} else {
Draw(p,index,sprite.getBi_array()[Y*sprite.getWidth()+X]);
}
}
}
}
public static void Draw(int[] canvas,int index, int col) {
int alpha = col>>>24;
if (alpha==0) {
return;}
else
if (alpha==255) {
canvas[index]=col;
} else {
float ratio=alpha/255f;
int prev_col=canvas[index];
int prev_r=(prev_col&0xFF);
int prev_g=(prev_col&0xFF00)>>>8;
int prev_b=(prev_col&0xFF0000)>>>16;
int r=(col&0xFF);
int g=(col&0xFF00)>>>8;
int b=(col&0xFF0000)>>>16;
int new_r=(int)(ratio*r+(1-ratio)*prev_r);
int new_g=(int)(ratio*g+(1-ratio)*prev_g);
int new_b=(int)(ratio*b+(1-ratio)*prev_b);
canvas[index]=new_r+(new_g<<8)+(new_b<<16)+(col&0xFF000000);
}
}
}

@ -5,7 +5,6 @@ import java.util.ArrayList;
import java.util.List;
import sig.engine.Panel;
import sig.objects.Player;
import sig.engine.Object;
public class RabiClone {
@ -31,20 +30,6 @@ public class RabiClone {
p.render();
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
OBJ.add(new Player(p));
long lastGameTime = System.nanoTime();
while (true) {
long timePassed = System.nanoTime()-lastGameTime;

@ -4,6 +4,14 @@ public abstract class Object implements GameEntity{
double x,y;
Sprite spr;
Panel panel;
public Panel getPanel() {
return panel;
}
public void setPanel(Panel panel) {
this.panel = panel;
}
boolean markedForDeletion;
public boolean isMarkedForDeletion() {

@ -139,6 +139,7 @@ public class Panel extends JPanel implements Runnable,ComponentListener,KeyListe
mImageProducer.newPixels();
// draw it on panel
g.drawImage(this.imageBuffer, 0, 0, this);
updateFPSCounter();
}
/**
@ -363,7 +364,6 @@ public class Panel extends JPanel implements Runnable,ComponentListener,KeyListe
}
}
lastUpdate=newTime;
updateFPSCounter();
}
private void updateFPSCounter() {

@ -18,8 +18,19 @@ public class Player extends Object{
@Override
public void update(double updateMult) {
if (KeyHeld(KeyEvent.VK_RIGHT)) {
setX(getX()+32*updateMult);
int right = KeyHeld(KeyEvent.VK_RIGHT)?1:0;
int left = KeyHeld(KeyEvent.VK_LEFT)?1:0;
int up = KeyHeld(KeyEvent.VK_UP)?1:0;
int down = KeyHeld(KeyEvent.VK_DOWN)?1:0;
if (right-left!=0) {
setX(getX()+(right-left)*32*updateMult);
}
if (up-down!=0) {
setY(getY()+(up-down)*32*updateMult);
}
if (getX()<-this.getSprite().getWidth()||getX()>getPanel().getWidth()||
getY()<-this.getSprite().getHeight()||getY()>getPanel().getHeight()) {
setMarkedForDeletion(true);
}
}

Loading…
Cancel
Save