aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/camilstaps/rushhour/Car.java
blob: 4c393294164cdcae5c4a0516f251350deda500b9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package com.camilstaps.rushhour;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;

import java.util.Random;

/**
 * Created by camilstaps on 16-4-15.
 */
public class Car {

    private Coordinate startCoordinate, endCoordinate;
    private final int colour;

    private static final int SIZE = 50;
    private static final int MARGIN = 5;

    private MoveListener moveListener;

    private float widthPerCell;
    private int calculatedWidth, calculatedHeight;

    ImageView iv;

    public Car(Coordinate start, Coordinate end) {
        startCoordinate = start;
        endCoordinate = end;
        this.colour = Color.BLACK;
    }

    public Car(Coordinate start, Coordinate end, int colour) {
        startCoordinate = start;
        endCoordinate = end;
        this.colour = colour;
    }

    public Car(Car car) {
        startCoordinate = car.startCoordinate;
        endCoordinate = car.endCoordinate;
        colour = car.colour;
    }

    public void setMoveListener(MoveListener listener) {
        moveListener = listener;
    }

    public void setLayoutParams() {
        ViewGroup.MarginLayoutParams marginParams = new RelativeLayout.LayoutParams(calculatedWidth, calculatedHeight);
        marginParams.setMargins((int) (startCoordinate.getX() * (widthPerCell + MARGIN) + MARGIN), (int) (startCoordinate.getY() * (widthPerCell + MARGIN) + MARGIN), MARGIN, MARGIN);
        iv.setLayoutParams(marginParams);
    }

    public ImageView getImageView(Context context, float widthPerCell) {

        this.widthPerCell = widthPerCell - MARGIN;
        calculatedWidth = (int) ((endCoordinate.getX() - startCoordinate.getX() + 1) * (this.widthPerCell + MARGIN) - MARGIN);
        calculatedHeight = (int) ((endCoordinate.getY() - startCoordinate.getY() + 1) * (this.widthPerCell + MARGIN) - MARGIN);

        iv = new ImageView(context);

        int[] images = null;
        if(canMoveHorizontally()) {
            if(getCarLength() == 2) {
                images = new int[] {
                        R.drawable.car_1_white,
                        R.drawable.car_2_white,
                        R.drawable.car_3_white,
                        R.drawable.car_4_white};
            }else{
                images = new int[] {
                        R.drawable.truck_1_white,
                        R.drawable.truck_2_white,
                        R.drawable.truck_3_white,
                        R.drawable.truck_4_white};
            }
        }else{
            if(getCarLength() == 2) {
                images = new int[] {
                        R.drawable.car_1_white_vertical,
                        R.drawable.car_2_white_vertical,
                        R.drawable.car_3_white_vertical,
                        R.drawable.car_4_white_vertical};
            }else{
                images = new int[] {
                        R.drawable.truck_1_white_vertical,
                        R.drawable.truck_2_white_vertical,
                        R.drawable.truck_3_white_vertical,
                        R.drawable.truck_4_white_vertical};
            }
        }

        iv.setImageResource(choose(images));
        iv.setColorFilter(colour);
        iv.setMinimumWidth(calculatedWidth);
        iv.setMinimumHeight(calculatedHeight);

        setLayoutParams();

        final GestureDetector gdt = new GestureDetector(new GestureListener());
        iv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gdt.onTouchEvent(event);
                return true;
            }
        });

        return iv;
    }

    private static int choose(int[] a)
    {
        Random r = new Random();
        return a[r.nextInt(a.length)];
    }

    private float getCarDirection()
    {
        final double TWOPI = Math.PI * 2;
        double radians = ((Math.atan2(startCoordinate.getY() - endCoordinate.getY(), endCoordinate.getX() - startCoordinate.getX()) + TWOPI) % TWOPI);
        double degrees = radians / Math.PI * 180.0;

        return (float)degrees;
    }
    private int getCarLength()
    {
        if(startCoordinate.getX() == endCoordinate.getX())
        {
            return endCoordinate.getY() - startCoordinate.getY() + 1;
        }else{
            return endCoordinate.getX() - startCoordinate.getX() + 1;
        }
    }

    public boolean canMoveHorizontally() {
        return startCoordinate.getY() == endCoordinate.getY();
    }

    public boolean canMoveVertically() {
        return startCoordinate.getX() == endCoordinate.getX();
    }

    public void move(int offset) {
        if (canMoveHorizontally()) {
            moveHorizontally(offset);
        } else {
            moveVertically(offset);
        }
    }

    public void moveHorizontally(int offset) {
        startCoordinate.move(offset, 0);
        endCoordinate.move(offset, 0);

        setLayoutParams();
    }

    public void moveVertically(int offset) {
        startCoordinate.move(0, offset);
        endCoordinate.move(0, offset);

        setLayoutParams();
    }

    public Coordinate wouldMoveTo(int offset) {
        Coordinate movedCoordinate;
        if (offset < 0) {
            movedCoordinate = new Coordinate(startCoordinate);
        } else {
            movedCoordinate = new Coordinate(endCoordinate);
        }
        if (canMoveHorizontally()) {
            movedCoordinate.move(offset, 0);
        } else {
            movedCoordinate.move(0, offset);
        }
        return movedCoordinate;
    }

    public boolean occupies(Coordinate c) {
        return c.getX() >= startCoordinate.getX() && c.getX() <= endCoordinate.getX() && c.getY() >= startCoordinate.getY() && c.getY() <= endCoordinate.getY();
    }

    private class GestureListener extends GestureDetector.SimpleOnGestureListener {
        private static final int SWIPE_MIN_DISTANCE = SIZE;
        private static final int SWIPE_THRESHOLD_VELOCITY = SIZE;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY && canMoveHorizontally()) {
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
                    moveListener.onMove(Car.this, -1);
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
                    moveListener.onMove(Car.this, 1);
                }
            }

            if (Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY && canMoveVertically()) {
                if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
                    moveListener.onMove(Car.this, -1);
                } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
                    moveListener.onMove(Car.this, 1);
                }
            }

            return true;
        }
    }

}