~brzeti/patat/dev

« back to all changes in this revision

Viewing changes to src/com/brackeen/javagamebook/graphics/Sprite.java

  • Committer: Arthur Skonecki
  • Date: 2008-12-02 12:59:20 UTC
  • Revision ID: admin@adb.cba.pl-20081202125920-hmvdzcrxgq1tl514
initial project's files submission

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.brackeen.javagamebook.graphics;
 
2
 
 
3
import java.awt.Image;
 
4
 
 
5
import com.brackeen.javagamebook.tilegame.sprites.Bullet;
 
6
 
 
7
public class Sprite {
 
8
 
 
9
    protected Animation anim;
 
10
    // position (pixels)
 
11
    private float x;
 
12
    private float y;
 
13
    // velocity (pixels per millisecond)
 
14
    private float dx;
 
15
    private float dy;
 
16
 
 
17
    public void setStartingFrame(int startingFrame) {
 
18
        anim.setStartingFrame(startingFrame);
 
19
    }
 
20
    /**
 
21
        Creates a new Sprite object with the specified Animation.
 
22
    */
 
23
    public Sprite(Animation anim) {
 
24
        this.anim = anim;
 
25
    }
 
26
 
 
27
    /**
 
28
        Updates this Sprite's Animation and its position based
 
29
        on the velocity.
 
30
    */
 
31
    public void update(long elapsedTime) {
 
32
        x += dx * elapsedTime;
 
33
        y += dy * elapsedTime;
 
34
        anim.update(elapsedTime);
 
35
    }
 
36
 
 
37
    /**
 
38
        Gets this Sprite's current x position.
 
39
    */
 
40
    public float getX() {
 
41
        return x;
 
42
    }
 
43
 
 
44
    /**
 
45
        Gets this Sprite's current y position.
 
46
    */
 
47
    public float getY() {
 
48
        return y;
 
49
    }
 
50
 
 
51
    /**
 
52
        Sets this Sprite's current x position.
 
53
    */
 
54
    public void setX(float x) {
 
55
        this.x = x;
 
56
    }
 
57
 
 
58
    /**
 
59
        Sets this Sprite's current y position.
 
60
    */
 
61
    public void setY(float y) {
 
62
        this.y = y;
 
63
    }
 
64
 
 
65
    /**
 
66
        Gets this Sprite's width, based on the size of the
 
67
        current image.
 
68
    */
 
69
    public int getWidth() {
 
70
        return anim.getImage().getWidth(null);
 
71
    }
 
72
 
 
73
    /**
 
74
        Gets this Sprite's height, based on the size of the
 
75
        current image.
 
76
    */
 
77
    public int getHeight() {
 
78
        return anim.getImage().getHeight(null);
 
79
    }
 
80
 
 
81
    /**
 
82
        Gets the horizontal velocity of this Sprite in pixels
 
83
        per millisecond.
 
84
    */
 
85
    public float getVelocityX() {
 
86
        return dx;
 
87
    }
 
88
 
 
89
    /**
 
90
        Gets the vertical velocity of this Sprite in pixels
 
91
        per millisecond.
 
92
    */
 
93
    public float getVelocityY() {
 
94
        return dy;
 
95
    }
 
96
 
 
97
    /**
 
98
        Sets the horizontal velocity of this Sprite in pixels
 
99
        per millisecond.
 
100
    */
 
101
    public void setVelocityX(float dx) {
 
102
        this.dx = dx;
 
103
    }
 
104
 
 
105
    /**
 
106
        Sets the vertical velocity of this Sprite in pixels
 
107
        per millisecond.
 
108
    */
 
109
    public void setVelocityY(float dy) {
 
110
        this.dy = dy;
 
111
    }
 
112
 
 
113
    /**
 
114
        Gets this Sprite's current image.
 
115
    */
 
116
    public Image getImage() {
 
117
        return anim.getImage();
 
118
    }
 
119
 
 
120
    /**
 
121
        Clones this Sprite. Does not clone position or velocity
 
122
        info.
 
123
    */
 
124
    public Object clone() {
 
125
        return new Sprite(anim);
 
126
    }
 
127
}