<commit_before>package states;

import entities.Character;
import entities.Door;
import entities.Enemy;
import entities.Exit;
import entities.Player;
import entities.Room;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.addons.editors.ogmo.FlxOgmoLoader;
import flixel.group.FlxGroup.FlxTypedGroup;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.tile.FlxBaseTilemap.FlxTilemapDiagonalPolicy;
import flixel.tile.FlxTilemap;
import flixel.util.FlxPath;
import states.GameOverState;
import states.WinState;
import flixel.math.FlxMath;

class PlayState extends FlxState
{
	private var _level: Level;
	private var _player: Player;
	private var _exit: Exit;
	private var _map: FlxOgmoLoader;
	private var _mWalls: FlxTilemap;
	private var _grpEnemies: FlxTypedGroup<Enemy>;
	private var _grpDoors: FlxTypedGroup<Door>;
	private var _grpRooms: FlxTypedGroup<Room>;
	private var _nbMove: Int = 0;

	override function new(level: Level) {
		super();
		this._level = level;
	}
	
	override public function create():Void
	{
		var levelName: String = this._level != null ? this._level._name : "level_01";
		
		_map = new FlxOgmoLoader('assets/data/$levelName.oel');

		_mWalls = _map.loadTilemap(AssetPaths.tiles__png, Main.TILE_SIZE, Main.TILE_SIZE, "walls");
		_mWalls.follow();
		_mWalls.setTileProperties(1, FlxObject.NONE);
		_mWalls.setTileProperties(2, FlxObject.ANY);
		
		add(_mWalls);
		add(_grpRooms = new FlxTypedGroup<Room>());
		add(_grpDoors = new FlxTypedGroup<Door>());
		add(_grpEnemies = new FlxTypedGroup<Enemy>());
		_map.loadRectangles(function(rect: FlxRect) {
			var room: Room;
			_grpRooms.add(room = new Room(rect));
			room._onClickCallback = this.onRoomClicked;
			room._isUnlocked = function(cb : Bool -> Void) {
				cb(this.canMove());
			}
		}, "rooms");
		_map.loadEntities(placeEntities, "entities");
		super.create();
	}

	private function placeEntities(entityName:String, entityData:Xml):Void
	{
		var x:Int = Std.parseInt(entityData.get("x"));
		var y:Int = Std.parseInt(entityData.get("y"));
		if (entityName == "player")
		{
			add(_player = new Player());
			_player.path.onComplete = this.onPathComplete;
			_player.x = x;
			_player.y = y;
		}
		else if (entityName == "enemy")
		{
			var e: Enemy;
			_grpEnemies.add(e = new Enemy(x, y, Std.parseInt(entityData.get("etype"))));
			e.path.onComplete = this.onPathComplete;
		}
		else if (entityName == "door")
		{
			var door: Door = new Door(x, y, entityData.get("open") == "True");
			_grpDoors.add(door);
			this._grpRooms.forEach(function(room: Room) {
				var roomRect: FlxRect = room.getRect();
				var doorRect: FlxRect = new FlxRect(x - 1, y - 1, Main.TILE_SIZE*1.5, Main.TILE_SIZE*1.5);
				if (roomRect.overlaps(doorRect))
					room._doors.push(door);
			});
		}
		else if (entityName == "exit")
		{
			add(_exit = new Exit());
			_exit.x = x;
			_exit.y = y;
		}
	}
	
	private function canMove(): Bool {
		for (c in getCharacters())
			if (!c.isIdle())
				return false;
		return true;
	}
	
	private function getCharacters(): Array<Character> {
		var res: Array<Character> = new Array<Character>();
		this._grpEnemies.forEach(function(e: Enemy) {
			res.push(e);
		});
		res.push(this._player);
		return res;
	}
	
	private function onRoomClicked() {
		_grpDoors.forEach(function(d: Door) {
			_mWalls.setTile(Math.floor(d.x / Main.TILE_SIZE), Math.floor(d.y / Main.TILE_SIZE), d._opened ? 1 : 2, true);
		});
			
		_grpEnemies.forEach(function(e: Enemy) {
			e.move(this.findPath(e, _player)); // Destination à changer
		});
		_player.move(this.findPath(_player, _exit));  // Destination à changer
	}

	override public function update(elapsed:Float):Void
	{	
		FlxG.collide(_player, _grpEnemies, function(a, b) {
			FlxG.switchState(new GameOverState(this._level));
		});
		FlxG.collide(_player, _exit, function(a, b) {
			FlxG.switchState(new WinState(this._level));
		});
	
		super.update(elapsed);
	}
	
	private function onPathComplete(path: FlxPath): Void {
		
	}
	
	private function findPath(from: FlxSprite, to: FlxSprite): Array<FlxPoint> {
		var path: Array<FlxPoint> = _mWalls.findPath(from.getPosition(), to.getPosition(), false, false, FlxTilemapDiagonalPolicy.NONE);
		if (path != null) path = path.slice(1);
		return path;
	}

}<commit_msg>removed useless onComplete<commit_after>package states;

import entities.Character;
import entities.Door;
import entities.Enemy;
import entities.Exit;
import entities.Player;
import entities.Room;
import flixel.FlxG;
import flixel.FlxObject;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.addons.editors.ogmo.FlxOgmoLoader;
import flixel.group.FlxGroup.FlxTypedGroup;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.tile.FlxBaseTilemap.FlxTilemapDiagonalPolicy;
import flixel.tile.FlxTilemap;
import flixel.util.FlxPath;
import states.GameOverState;
import states.WinState;
import flixel.math.FlxMath;

class PlayState extends FlxState
{
	private var _level: Level;
	private var _player: Player;
	private var _exit: Exit;
	private var _map: FlxOgmoLoader;
	private var _mWalls: FlxTilemap;
	private var _grpEnemies: FlxTypedGroup<Enemy>;
	private var _grpDoors: FlxTypedGroup<Door>;
	private var _grpRooms: FlxTypedGroup<Room>;
	private var _nbMove: Int = 0;

	override function new(level: Level) {
		super();
		this._level = level;
	}
	
	override public function create():Void
	{
		var levelName: String = this._level != null ? this._level._name : "level_01";
		
		_map = new FlxOgmoLoader('assets/data/$levelName.oel');

		_mWalls = _map.loadTilemap(AssetPaths.tiles__png, Main.TILE_SIZE, Main.TILE_SIZE, "walls");
		_mWalls.follow();
		_mWalls.setTileProperties(1, FlxObject.NONE);
		_mWalls.setTileProperties(2, FlxObject.ANY);
		
		add(_mWalls);
		add(_grpRooms = new FlxTypedGroup<Room>());
		add(_grpDoors = new FlxTypedGroup<Door>());
		add(_grpEnemies = new FlxTypedGroup<Enemy>());
		_map.loadRectangles(function(rect: FlxRect) {
			var room: Room;
			_grpRooms.add(room = new Room(rect));
			room._onClickCallback = this.onRoomClicked;
			room._isUnlocked = function(cb : Bool -> Void) {
				cb(this.canMove());
			}
		}, "rooms");
		_map.loadEntities(placeEntities, "entities");
		super.create();
	}

	private function placeEntities(entityName:String, entityData:Xml):Void
	{
		var x:Int = Std.parseInt(entityData.get("x"));
		var y:Int = Std.parseInt(entityData.get("y"));
		if (entityName == "player")
		{
			add(_player = new Player());
			_player.x = x;
			_player.y = y;
		}
		else if (entityName == "enemy")
		{
			var e: Enemy;
			_grpEnemies.add(e = new Enemy(x, y, Std.parseInt(entityData.get("etype"))));
		}
		else if (entityName == "door")
		{
			var door: Door = new Door(x, y, entityData.get("open") == "True");
			_grpDoors.add(door);
			this._grpRooms.forEach(function(room: Room) {
				var roomRect: FlxRect = room.getRect();
				var doorRect: FlxRect = new FlxRect(x - 1, y - 1, Main.TILE_SIZE*1.5, Main.TILE_SIZE*1.5);
				if (roomRect.overlaps(doorRect))
					room._doors.push(door);
			});
		}
		else if (entityName == "exit")
		{
			add(_exit = new Exit());
			_exit.x = x;
			_exit.y = y;
		}
	}
	
	private function canMove(): Bool {
		for (c in getCharacters())
			if (!c.isIdle())
				return false;
		return true;
	}
	
	private function getCharacters(): Array<Character> {
		var res: Array<Character> = new Array<Character>();
		this._grpEnemies.forEach(function(e: Enemy) {
			res.push(e);
		});
		res.push(this._player);
		return res;
	}
	
	private function onRoomClicked() {
		_grpDoors.forEach(function(d: Door) {
			_mWalls.setTile(Math.floor(d.x / Main.TILE_SIZE), Math.floor(d.y / Main.TILE_SIZE), d._opened ? 1 : 2, true);
		});
			
		_grpEnemies.forEach(function(e: Enemy) {
			e.move(this.findPath(e, _player)); // Destination à changer
		});
		_player.move(this.findPath(_player, _exit));  // Destination à changer
	}

	override public function update(elapsed:Float):Void
	{	
		FlxG.collide(_player, _grpEnemies, function(a, b) {
			FlxG.switchState(new GameOverState(this._level));
		});
		FlxG.collide(_player, _exit, function(a, b) {
			FlxG.switchState(new WinState(this._level));
		});
	
		super.update(elapsed);
	}
	
	private function findPath(from: FlxSprite, to: FlxSprite): Array<FlxPoint> {
		var path: Array<FlxPoint> = _mWalls.findPath(from.getPosition(), to.getPosition(), false, false, FlxTilemapDiagonalPolicy.NONE);
		if (path != null) path = path.slice(1);
		return path;
	}

}
{'fraction_non_alphanumeric': 0.14223580580698247, 'fraction_numerical': 0.0032914070765252147, 'mean_word_length': 4.4019047619047615, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '893888', 'n_tokens_mistral': 3444, 'n_tokens_neox': 3196, 'n_words': 633}