1- /*
2- * Copyright 2014 MovingBlocks
3- *
4- * Licensed under the Apache License, Version 2.0 (the "License");
5- * you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at
7- *
8- * http://www.apache.org/licenses/LICENSE-2.0
9- *
10- * Unless required by applicable law or agreed to in writing, software
11- * distributed under the License is distributed on an "AS IS" BASIS,
12- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- * See the License for the specific language governing permissions and
14- * limitations under the License.
15- */
1+ // Copyright 2020 The Terasology Foundation
2+ // SPDX-License-Identifier: Apache-2.0
163package org .terasology .navgraph ;
174
185import com .google .common .collect .Sets ;
6+ import org .terasology .engine .world .block .BlockArea ;
197
208import java .util .Set ;
219
@@ -29,76 +17,51 @@ public enum Type {
2917 }
3018
3119 public final Set <Floor > neighborFloors = Sets .newHashSet ();
32- private Rect area ;
20+ private final BlockArea area = new BlockArea ( BlockArea . INVALID ) ;
3321 private Type type ;
34- private Floor floor ;
22+ private final Floor floor ;
3523
3624 public Entrance (Floor floor ) {
3725 this .floor = floor ;
3826 }
3927
4028 public boolean isPartOfEntrance (int x , int y ) {
41- if (area == null ) {
29+ if (! area . isValid () ) {
4230 return true ;
4331 }
4432 if (area .contains (x , y )) {
4533 return true ;
4634 }
47- int x1 = Math .min (area .x , x );
48- int y1 = Math .min (area .y , y );
49- int x2 = Math .max (area .x + area . w , x );
50- int y2 = Math .max (area .y + area . h , y );
35+ int x1 = Math .min (area .minX () , x );
36+ int y1 = Math .min (area .minY () , y );
37+ int x2 = Math .max (area .maxX () , x );
38+ int y2 = Math .max (area .maxY () , y );
5139
5240 if (type == Type .VERTICAL ) {
53- return y2 - y1 == 0 && area .w + 1 == x2 - x1 ;
41+ return y2 - y1 == 0 && area .getSizeX () == x2 - x1 ;
5442 } else if (type == Type .HORIZONTAL ) {
55- return x2 - x1 == 0 && area .h + 1 == y2 - y1 ;
43+ return x2 - x1 == 0 && area .getSizeY () == y2 - y1 ;
5644 } else {
5745 return x2 - x1 <= 1 && y2 - y1 <= 1 ;
5846 }
5947 }
6048
6149 public void addToEntrance (int x , int y ) {
62- if (area == null ) {
63- area = new Rect (x , y , 0 , 0 );
64- } else {
65- if (!area .contains (x , y )) {
66- int x1 = Math .min (area .x , x );
67- int y1 = Math .min (area .y , y );
68- int x2 = Math .max (area .x + area .w , x );
69- int y2 = Math .max (area .y + area .h , y );
70- area = new Rect (x1 , y1 , x2 - x1 , y2 - y1 );
71- if (area .w > area .h ) {
72- type = Type .VERTICAL ;
73- } else if (area .w < area .h ) {
74- type = Type .HORIZONTAL ;
75- }
50+
51+ if (!area .contains (x , y )) {
52+ area .union (x , y );
53+ if (area .getSizeX () > area .getSizeY ()) {
54+ type = Type .VERTICAL ;
55+ } else if (area .getSizeX () < area .getSizeY ()) {
56+ type = Type .HORIZONTAL ;
7657 }
7758 }
7859 }
7960
8061 public WalkableBlock getAbstractBlock () {
81- int mx = area .x + area .w / 2 ;
82- int my = area .y + area .h / 2 ;
62+ int mx = ( area .minX () + area .getSizeX ()) / 2 ;
63+ int my = ( area .minY () + area .getSizeY ()) / 2 ;
8364
8465 return floor .getBlock (mx , my );
8566 }
86-
87- private static final class Rect {
88- int x ;
89- int y ;
90- int w ;
91- int h ;
92-
93- private Rect (int x , int y , int w , int h ) {
94- this .x = x ;
95- this .y = y ;
96- this .w = w ;
97- this .h = h ;
98- }
99-
100- private boolean contains (int px , int py ) {
101- return px >= x && py >= y && px < x + h && py < y + h ;
102- }
103- }
10467}
0 commit comments