Advertisement
Zidinjo

GameObject

Oct 23rd, 2015
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. package de.fhflensburg.manichja.barrier51;
  2.  
  3.         import android.content.Context;
  4.         import android.content.res.ColorStateList;
  5.         import android.graphics.Bitmap;
  6.         import android.graphics.BitmapFactory;
  7.         import android.graphics.Canvas;
  8.         import android.graphics.Color;
  9.         import android.graphics.Paint;
  10.         import android.graphics.Point;
  11.         import android.graphics.drawable.Drawable;
  12.         import android.util.Log;
  13.         import android.view.View;
  14.         import android.widget.ImageView;
  15.         import android.util.Size;
  16.  
  17. /**
  18.  * Created by Niklas on 22.10.2015.
  19.  */
  20. public class GameObject {
  21.     private Size size;      // A color as stated as a resource in the XML file (thus, it is an int)
  22.     private Point position; // A color as stated as a resource in the XML file (thus, it is an int)
  23.     private int fillColor;      // A color as stated as a resource in the XML file (thus, it is an int)
  24.     private int strokeColor;// A color as stated as a resource in the XML file (thus, it is an int)
  25.     private int sprite;// A color as stated as a resource in the XML file (thus, it is an int)
  26.     private Bitmap ourSprite;
  27.     private Context conny;
  28.  
  29.     public GameObject(Context context)
  30.     {
  31.         this.conny = context;
  32.         /*Paint myPaint = new Paint();
  33.         myPaint.setColor(Color.rgb(0, 0, 0));
  34.         myPaint.setStrokeWidth(10);
  35.         c.drawRect(100, 100, 200, 200, myPaint);*/
  36.     }
  37.  
  38.     /**
  39.      * Sets the position of the GameObject on the screen.
  40.      * @param position A Point instance with x and y
  41.      */
  42.     public void setPosition(Point position)
  43.     {
  44.         this.position = position;
  45.     }
  46.  
  47.     /**
  48.      * Sets the fill color of the game object. If it has a Sprite image, the assigned color will be ignored.
  49.      * @param color A color resource, defined in the XML file (e.g. R.color.limegreen)
  50.      */
  51.     public void setFillColor(int color)
  52.     {
  53.         this.fillColor = color;
  54.     }
  55.  
  56.     /**
  57.      * Sets the stroke color of the game object. If it has a Sprite image, the assigned color will be ignored.
  58.      * @param color A color resource, defined in the XML file (e.g. R.color.limegreen)
  59.      */
  60.     public void setStrokeColor(int color)
  61.     {
  62.         this.strokeColor = color;
  63.     }
  64.  
  65.     /**
  66.      * @return Delivers the current size of the GameObject
  67.      */
  68.     public Size getSize()
  69.     {
  70.         return size;
  71.     }
  72.  
  73.     /**
  74.      * @return Delivers the current position of the GameObject
  75.      */
  76.     public Point getPosition()
  77.     {
  78.         return position;
  79.     }
  80.  
  81.     /**
  82.      * @return Delivers the current position of the GameObject as stated in the XML file.
  83.      */
  84.     public int getFillColor()
  85.     {
  86.         return fillColor;
  87.     }
  88.  
  89.     /**
  90.      * @return Delivers the current stroke color of the GameObject as stated in the XML file.
  91.      */
  92.     public int getStrokeColor()
  93.     {
  94.         return strokeColor;
  95.     }
  96.  
  97.     /**
  98.      * Checks for a collision of this object with another instance of a GameObject.
  99.      * @param obj The object to be checked for collision
  100.      * @return Returns true if the objects' coordinates overlap at any pixel.
  101.      */
  102.     public boolean overlaps(GameObject obj)
  103.     {
  104.         if (getPosition().x < obj.getPosition().x + obj.getSize().getWidth() &&
  105.                 getPosition().x + getSize().getWidth() > obj.getPosition().x &&
  106.                 getPosition().y < obj.getPosition().y + obj.getSize().getHeight() &&
  107.                 getSize().getHeight() + getPosition().y > obj.getPosition().y)
  108.         {
  109.             return true;
  110.         }
  111.         else
  112.         {
  113.             return false;
  114.         }
  115.     }
  116.  
  117.     /**
  118.      * Sets the sprite image of the GameObject.
  119.      * @param sprite A bitmap resource as defined in the XML file.
  120.      */
  121.     public void setSprite(int sprite) {
  122.  
  123.         Log.i("Peter", "Inigame get started"+sprite);
  124.         this.sprite = sprite;
  125.         ourSprite = BitmapFactory.decodeResource(this.conny.getResources(),R.drawable.menu_background);
  126.     }
  127.  
  128.     /**
  129.      * @return Delivers the sprite resource id as an Integer.
  130.      */
  131.     public Bitmap getSprite() {
  132.  
  133.         return ourSprite;
  134.     }
  135.  
  136.     /**
  137.      * Renders the object with the current attributes on the screen.
  138.      */
  139.     public void render(Canvas c,Paint paint)
  140.     {
  141.         c.drawRect(0,0,120,120,paint);
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement